1

My paintComponent() method inside my JPanel isn´t working after loading BufferedImages. The update method is running properly every 20ms (swing.timer) but the window stays white without any shapes or colors (which were there before I included the images). If I just comment on the BufferedImage testImage (see code) the paintComponent method is running again and displays the screen of my game.

I wrote the ImageLoader class several times and tried different paths. Most of them led to an error message, but now the path is working properly and the BufferedImages can be loaded. But now the window remains just white.

paintComponent() isn´t running. Update() is running and the shapes change their positions. But you can´t see anything. I tried to use System.out.println() in paintComponent() but the console remains empty as well. No output at all.

I don´t know which part of my code caused the error, but here you can say the ImageLoader as well as the paintComponent() in the JPanel class (at least the meaningful code)

ImageLoader:

public class ImageLoader {

    public static BufferedImage load(String path) {
        try {
            return ImageIO.read(ImageLoader.class.getResource(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

In my JPanel class:

private BufferedImage testImage = ImageLoader.load("/res/playerufo.png");

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    /* Draw here --> I am not drawing any images yet. Just some colored rectangles */
}

If I try to start the program there is just a white window. There is no error message in the console. If I try to output the BufferedImage there is an output. It´s not null. But if I do not load the Image and instead write

private BufferedImage testImage = null;

the game starts and paintComponent() is doing it´s job. But why does my paintComponent "have a problem" with an BufferedImage != null?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zawax
  • 11
  • 2
  • That it's a static method doing the loading is possibly causing the wrong class loader (the bootstrap class loader, intended only for core parts of the API) to be used. – Andrew Thompson Aug 20 '19 at 02:47
  • @AndrewThompson I removed the `static` modifier of the `load()-method`, created an instance of the ImageLoader and tried to load the `BufferedImage` with the ImageLoader object. But the result is the same. There is still just a white window without any error logs in the console. – Zawax Aug 21 '19 at 13:38

0 Answers0