1

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.

  • 4
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Dec 05 '14 at 11:00
  • MadProgrammer: The only example i can post with my problem is a screenshot of the window. Would that be more helpful? – Danut Niculae Dec 05 '14 at 11:09
  • mKorbel: where is the missing paintIcon? – Danut Niculae Dec 05 '14 at 11:09
  • 1
    *"..screenshot of the window. Would that be more helpful?"* No, not really. What @MadProgrammer asked you to post is well described in the link. One way to get images for an example, is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Dec 05 '14 at 11:38
  • I have edited the question, hopefully this is a bit more explicit. – Danut Niculae Dec 05 '14 at 13:37
  • I have found the problem. I misread the Java documentation when it comes to paintIcon method. My coordinates for drawing the X and Y of the image are out of the frame. I changed the coordinates to 0 and 0 and now it works perfectly. – Danut Niculae Dec 05 '14 at 13:43

1 Answers1

1

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);
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138