-6

I would like to use an image as the background of a JPanel. It needs to be loaded from a relative file path.

    private void createBackground() {
    try {
        BufferedImage backgroundImage = ImageIO.read(new File("C:/Users/Developer/workspace/Java/BSC_Project/Application/src/resources/background.jpg"));
        JLabel background = new JLabel(new ImageIcon(backgroundImage));
        this.add(background);
    } catch(IOException e) {
        System.out.println(e.toString());
    }
}

My current code is not working. Any help would be appreciated.

  • 3
    What do you mean "is not working". What's the problem? Please read this sscce.org – reggaeguitar Apr 29 '15 at 18:14
  • That's not a relative path. To use a relative path you need to know what the current path of the process is. Note, that's not the same as the path to the program. – stark Apr 29 '15 at 18:18
  • The image is not loading into the JPanel. – user2941348 Apr 29 '15 at 18:23
  • possible duplicate of [How to add an image to a JPanel?](http://stackoverflow.com/questions/299495/how-to-add-an-image-to-a-jpanel) – Turing85 Apr 29 '15 at 18:26
  • 1
    1-, Don't hardcode an absolute path. You never know where the image will be on other machines. Read the section from the Swing tutorial on [How to Use Icons](http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html) for the better way. Also, look at the questions under the `Related` heading on the right side of the page. In other words search the forum before asking a question. – camickr Apr 29 '15 at 18:27
  • Thanks! I read the article but I'm still having trouble placing the image within the JPanel implementation. – user2941348 Apr 29 '15 at 19:07
  • If it is a background image that is effectively an application resource that will become an [tag:embedded-resource] by the time of deployment. As such, it must be accessed by URL rather than File. – Andrew Thompson Apr 29 '15 at 22:31

3 Answers3

0

So can't comment(need 50 rep) but your file path is completely wrong

you need it to be something like this

new File("C:/Users/"Insert Username"/Desktop/workspace/Java/BSC_Project/Application/src/resources/background.jpg")

except I'm not on your computer so you need to figure out your own file path, This would be assuming your workspace folder is on your Desktop which it almost certainly isn't, do you understand?

Brennan Adler
  • 895
  • 2
  • 8
  • 18
0

Well if you want your code to have a panel this is what you would do...

private void createBackground() {
    try {
        BufferedImage backgroundImage = ImageIO.read(new File("C:/Users/Developer/workspace/Java/BSC_Project/Application/src/resources/background.jpg"));
        JPanel panel = new JPanel();
        JLabel background = new JLabel(new ImageIcon(backgroundImage));
        panel.add(background);
        this.add(panel);
    } catch(IOException e) {
        System.out.println(e.toString());
    }
}

but in your case it looks to me like you want a frame background as image... to which you can set the image background to which for that one you can try this code

JFrame f = new JFrame ("SettingBackGround");
    try{
        f.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("Med.jpg")))));
    }catch (IOException e){
        System.out.println("Image Doesnt Exist");
    }
    f.setVisible(true);
    f.setResizable(false);
    f.pack();
}

}

I hope this helps though.

Madona wambua
  • 1,408
  • 1
  • 21
  • 37
  • I have a class which extends JPanel. In the constructor I call a function called "createBackground". – user2941348 Apr 29 '15 at 18:35
  • Oh i see then what is the era? try my second code and see what it does. :) – Madona wambua Apr 29 '15 at 18:36
  • I have a class which extends JPanel. That class is used in a JTabbedPane. The class has a function called "createBackground". And in this function I would like to display an image within the JPanel. – user2941348 Apr 29 '15 at 18:41
  • Its hard to picture that without seeing but you can try to add the code in my comment and put the Jpanel instead of frame and see if it works.. just try the code and see. it prints a background image. – Madona wambua Apr 29 '15 at 18:45
0

    public WelcomeView() {
    initComponents();
    try {
        image = ImageIO.read(new File("C:\\Users\\Developer\\workspace\\Java\\BSC_Project\\Application\\src\\application\\resources\\background.png"));
    } catch (IOException ex) {
        System.err.println(ex.toString());
    }
}

    private BufferedImage image;

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, 2, 0, null); // see javadoc for more info on the parameters            
}