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
?