简体   繁体   中英

Image in Java applet won't show up

I have a project due Sunday (in school) and I am having problems showing images.

Everything else but the image is working, I used code examples from many people but none of them worked.

I put the image saved as "car.png" in the project directory.

Can you tell me where I'm wrong?

It's a project where I need to make a Java Applet that shows a street with a few buildings, a road and a car on the road, but I can't get the car image to show.

Thank you very much for your help!

import java.applet.*;
import java.awt.color.*;
import java.awt.Graphics;
import java.awt.*;



public class App_Main extends Applet
    {
    House[] h;
    private Image img;

    public void init()
    {
        Private v1 = new Private(120,300,80);
        Private v2 = new Private(180,300,80);
        Building b1 = new Building(300,300,80,5);
        img = getImage(getCodeBase(),"car.png");


        h= new House[3];

        h[0] = v1;
        h[1] = v2;
        h[2] = b1;
    }
    public void paint (Graphics g) {
        Private v1 = new Private(120, 300, 50);
        Private v2 = new Private(180, 300, 50);
        Building b1 = new Building(300, 300, 80, 5);


        House[] h = new House[3];

        h[0] = v1;
        h[1] = v2;
        h[2] = b1;

        for (int i = 0; i < 3; i++) {
            g.setColor(Color.BLUE);
            h[i].paintHouse(g);
        }

        //start road

        g.setColor(Color.BLACK);

        g.drawLine(0, 370, 1000, 370);
        g.setColor(Color.GRAY);
        g.fillRect(0, 371, 1000, 150);
        g.setColor(Color.BLACK);
        g.drawLine(0, 520, 1000, 520);
        for (int x=0; x < 1000; x += 100)
        {
            g.setColor(Color.WHITE);
            g.fillRect(x+25, 430, 50, 20);
        }

        //end road

        g.drawImage(img,50,50,this);



    }
}

There are a couple of problems in your code:

  1. You're using an Applet

    Applets are an advanced and specialized type of app. that has little place in the real world around us

    See Andrew Thompson's post Why CS teachers should stop teaching Java Applets to read more about this.

    Instead you should try using Java Swing also covered on the above recommended post.

  2. You're overriding paint() (w/o adding the @Override annotation) method of the Applet , instead you should have a JPanel and @Override it's paintComponent() method and then let the Applet display the JPanel I said before, as stated in this Hovercraft's answer

  3. Related to the above point, you're not calling super.paint() method of the Applet class

  4. You don't know if your img is in your code base, so debug your application (with a debugger or some System.out.println() calls and check if the path for car.png is correct.

For better help sooner please post a valid Minimal, Complete and Verifiable Example (MCVE) which includes the House class if needed or simply draws an image.

Also as a side recommendation don't use underscores ( _ ) to separate words in your classes' names ( App_Main could be written as AppMain , it's not a big deal, but it's a Java Naming Convention )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM