简体   繁体   中英

`Graphics.drawImage()` won't draw

Very simply, I could not draw this image.

public class RenderMap extends JPanel {

    static BufferedImage brick;
    static BufferedImage groundb;

    public static void main(String[] args) {
        JFrame window = new JFrame("Super Mario");
        RenderMap content = new RenderMap();
        window.setContentPane(content);
        window.setBackground(Color.WHITE);
        window.setSize(1200, 800);
        window.setLocation(100,0);
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        window.setResizable(false);
        window.setVisible(true);

        try {
            brick = ImageIO.read(new File("SuperMario/brick.png"));
        } catch (IOException e) {
        }

        try {
            URL url = new URL("SuperMario/brick.png");
            brick = ImageIO.read(url);
        } catch (IOException e) {
        }
        try {
            groundb = ImageIO.read(new File("SuperMario/ground.png"));
        } catch (IOException e) {
        }
        try {
            URL url = new URL("SuperMario/ground.png");
            groundb = ImageIO.read(url);
        } catch (IOException e) {
        }
    }

    public Ground ground;

    public RenderMap() {}

    public void paintComponent(Graphics g) {

        if(ground == null) {
            ground = new Ground();
        }
        ground.draw(g);
    }

    public class Ground implements ImageObserver {

        Ground(){}

        void draw(Graphics g) {
            g.drawImage(groundb, 0, 0, 1200, 800, this);
            g.fillOval( 8,  8, 16, 16);
        }

        @Override
        public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
            // TODO Auto-generated method stub
            return false;
        }
    }
}

This draws the oval, but not the image. I tried changing to another image(that is functioning in another program) and it still won't work, so I know it's not the image's problem.

As suspected, the images are not being loaded properly.

java.net.MalformedURLException: no protocol: SuperMario/brick.png 
at java.net.URL.<init>(URL.java:586) 
at java.net.URL.<init>(URL.java:483) 
at java.net.URL.<init>(URL.java:432) 
at RenderMap.main(RenderMap.java:40) 

java.net.MalformedURLException: no protocol: SuperMario/ground.png 
at java.net.URL.<init>(URL.java:586) 
at java.net.URL.<init>(URL.java:483) 
at java.net.URL.<init>(URL.java:432) 
at RenderMap.main(RenderMap.java:51)

It's failing at the load-by-URL function calls because you passed in an invalid URL. Correct URL format should be like how you access them from a web browser, like: http://someplace.com/SuperMarioFolder/brick.png

To load the images, choose only one way to read them. Either by:

  1. URL - Remove the File blocks and make the file accessible via full URL.

     try { URL url = new URL("http://www.proper-url.com/SuperMario/ground.png"); groundb = ImageIO.read(url); } catch (IOException e) { } 
  2. File - Remove the URL blocks. This will only allow the program to access local files.

     try { groundb = ImageIO.read(new File("SuperMario/ground.png")); } catch (IOException e) { } 

For your project's purpose, I believe option#2 would suffice.

Moving forward, you may want to use an IDE's debugging feature to go through code execution step-by-step. If you're not using one, you may want to IntelliJ and Eclipse .

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