0

I'm doing a tutorial and would add a background image. I've tried to do this:

public void run() {
    JFrame frame = new JFrame("VisLibDemo");
    try{
         frame.setContentPane(new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
    }catch(IOException e){
         System.out.println("Error");
    }
    frame.setMinimumSize(new Dimension(500, 400));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new VisLibDemo());
    frame.pack();
    frame.setVisible(true);

}

However I am not getting.

Or not showing the background image, or when it appears is on top and the rest will not appear.

This is what I have:

And I wanted to add image to background...

I do this:

  public void run() {
        ImageIcon icon = new ImageIcon(
        getClass().getResource("/Images/Brg800.jpg"));
        JLabel label = new JLabel(icon);
        label.setLayout(new BorderLayout());

        JFrame frame = new JFrame("VisLibDemo");
        //frame.add(new VisLibDemo());
        frame.setContentPane(label);
        VisLibDemo demo = new VisLibDemo(); 
        demo.setOpaque(false); frame.add(demo);
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

But I can only see a little bit of the background image ("/Images/Brg800.jpg")

Sorry for the bad explanation. But I need some help!

Can help me Please!!

strange_098
  • 1,261
  • 6
  • 24
  • 44

1 Answers1

1

Set the layout to the JLabel

JLabel label = new JLabel( new ImageIcon(ImageIO.read(new File("C:/Users/RPR1BRG/Pictures/Brg800.jpg")))));
label.setLayout(new BorderLayout());
frame.setContentPane(label);

Side Notes

When dealing with images that will be embedded into your program, you will want to load by resource and not by File which will not work on other systems beside the one you are developing on. To load by resource use

getClass().getResource("/path/to/image.png");

Your image should be in your src somewhere, where it will be built into the class path. So with this file structure

ProjectRoot
         src
            Pictures
                  Brg800.jpg

you would use this

ImageIcon icon = new ImageIcon(getClass().getResource("/Pictures/Brg800.jpg"));
JLabel label = new JLabel(icon);
label.setLayout(new BorderLayout());
frame.setContentPane(label);

See more at the link at the bottom of embedded resources tag wiki


Here's an example of all the above fore-mentioned

import java.awt.*;
import javax.swing.*;

public class ASimpleExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                ImageIcon icon = new ImageIcon(
                        getClass().getResource("/images/stackoverflow5.png"));
                JLabel label = new JLabel(icon);
                label.setLayout(new BorderLayout());

                JPanel panel = new JPanel(new GridBagLayout());
                panel.setOpaque(false);
                panel.add(new JButton("Button"));

                JFrame frame = new JFrame();
                frame.setContentPane(label);
                frame.add(panel);
                frame.setSize(300, 300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

enter image description here

enter image description here

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks for the reply.Is very explicit. But when I adapt the code did you say on mine, the image overlap the rest. I don't know how to solve this... – strange_098 Mar 03 '14 at 13:43
  • set the label as the content pane, but also set the layout of the label to `BorderLayout`. Then you can just add component to the frame as you normally would. – Paul Samsotha Mar 03 '14 at 13:46
  • I tried this: public void run() { ImageIcon icon = new ImageIcon( getClass().getResource("/Images/Brg800.jpg")); JLabel label = new JLabel(icon); label.setLayout(new BorderLayout()); JFrame frame = new JFrame("VisLibDemo"); frame.setContentPane(label); frame.setSize(300, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationByPlatform(true); frame.setVisible(true); } But the image Brg800.jpg overlap – strange_098 Mar 03 '14 at 13:59
  • Sorry how was the response :S – strange_098 Mar 03 '14 at 14:00
  • Overlaps what? You don't add anything else to the frame for it to overlap anything. – Paul Samsotha Mar 03 '14 at 14:00
  • Your `new VisLibDemo()`, **Don't** set it as the content pane. Just **`add()`** to the frame. `frame.add(new VisLibDemo());` – Paul Samsotha Mar 03 '14 at 14:07
  • You probably need to set the `new VisLibDemo()` to `setOpaque(false)` so you can see the background image. So you need to `VisLibDemo demo = new VisLibDemo(); demo.setOpaque(false); frame.add(demo);` – Paul Samsotha Mar 03 '14 at 14:10
  • Thanks again for the help. I tried what you said and I adapted my doubt upon. Now i can see a litle bit of background image on the left side... – strange_098 Mar 03 '14 at 14:27
  • The problem is most likely in the `VisLibDemo`. If you are using other `JPanels` inside the `VisLibDemo` `JPanel` you need to set all those opaque to false also. – Paul Samsotha Mar 03 '14 at 14:30
  • You need to post a complete runnable example for more help. It is only guessing without it. – Paul Samsotha Mar 03 '14 at 14:33
  • I posted a picture with what is happening!! I hope it helps to understand my difficulty. – strange_098 Mar 03 '14 at 14:44