1

I am trying to add a background image to a JPanel. The paint Component doesn't seem to paint the image imported for the background . Could anyone point out why? I have imported all the necessary libraries.

public class ImagePanel  extends JPanel {
    public static BufferedImage image;

    public ImagePanel() {
        try {
            image = ImageIO.read(new File("cards/background.png"));
            System.out.println("Image Import Succesful");
        } catch (IOException ex) {
            System.out.println("IMAGE IMPORT ERROR");
        }
        ImageIcon icon = new ImageIcon(image);
        icon.setImage(image);
        JLabel imageLabel = new JLabel(icon);
        add(imageLabel);

    }


    @Override
    protected void paintComponent(Graphics g) {
        System.out.println("painted");
        super.paintComponent(g);
        g.drawImage(image, 100, 100,
                    this); 
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 2
    Your paintComponent override is the place to paint the background. You should remove the ImageIcon / JLabel code – ControlAltDel Nov 03 '14 at 17:25
  • Yes, why _do_ you add the label _and_ paint the image? Are you getting "IMAGE IMPORT ERROR"? – Paul Samsotha Nov 03 '14 at 17:34
  • No I am not getting the image import error. And it does not work even after removing the code related to ImageIcons and Labels. The paint component does not work – Sarthak Haribhakti Nov 03 '14 at 20:35
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 2) 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 Nov 03 '14 at 21:51
  • Are you certain your ImagePanel instance has a nonzero width and height? – VGR Nov 04 '14 at 23:02

1 Answers1

0

Where are you loading your image from? A file system or classpath?

If you are loading you image from classpath you can use the class loader to load the image.If you are loading the image from a file system the path to that file has to be an absolute path.

You can paint the background of a panel if you are loading the image from classpath like this:

public class ImagePanel  extends JPanel {

   private ImageIcon imageIcon;

    public ImagePanel() {
       prepareBackground();
    }

    @Override
    protected void paintComponent(Graphics g) {
           super.paintComponent(g);
           if (null!= imageIcon)
               g.drawImage(image.getImage(), 100, 100, this);
           else
               System.err.println("Background image is NULL!!");
        }
    }

    private void prepareBackground() {
        final java.net.URL imgURL = ImagePanel.class.getResource("cards/background.png");
        imageIcon = new ImageIcon(imgURL);
    }
}

If you are loading from a file system, you can correct your code here:

image = ImageIO.read(new File("COMPLETE_PATH_TO_background.png"));

always_a_rookie
  • 4,515
  • 1
  • 25
  • 46