I seem to be having an issue painting a background image over a JFrame. All the other icons are painted correctly, but the background image seems to be skipped. The variable backgroundImg is an ImageIcon (declared like most of my other icons) and it should be set as a background to my JFrame, where other icons will be painted on top of it. It might be the case that the Graphics function is Java does not paint over multiple icons, but i am not sure. Here is the code:
private ImageIcon backgroundImg = new ImageIcon("image/back.PNG");
private String name;
private long score=0;
private static final Dimension backgroundSz = new Dimension(1024,768);
...................................................
public GamePanel()
{
setPreferredSize(backgroundSz);
setBackground(Color.black);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.backgroundImg.paintIcon(this, g, backgroundImg.getIconWidth(),
backgroundImg.getIconHeight());
}
To explain a bit further the problem: the background image is supposed to be of a star field and it is supposed to be painted over the main game panel. However, the result is everytime a black background (as set in the background color). The image size is the same as the frame size. All the other images are drawn correctly after the game starts, the only issue is the background picture. After removing most of the code which paints the other images, the background image would still not load.
Now paintIcon has x, y parameters not width/height, which seems to be the error.
In general I somewhat dislike the paintIcon method, and would store the Image and do:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImg.getImage(), 0, 0, null);
}