2

I'm trying to make my panel show image as background. I already can do that in NetBeans, but when I build my jar and run it image doesn't show there. I know I have to access it differently. I have seen many tutorials but every one of them shows how to do it with ImageIcon, but I don't need that, I need just Image. Can anyone point out what piece of code do I need to do this? Thanks.

This is my code for backgrounded JPanel:

public class JPanelWB extends JPanel { // Creates JPanel with given image as background.

    private Image backgroundImage;

    public JPanelWB(String fileName){
        try {
            backgroundImage = ImageIO.read(new File(fileName));
        } catch (IOException ex) {
            new JDialog().add(new Label("Could not open image."+ex.getMessage()));
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Draw the background image.
        g.drawImage(backgroundImage, 0, 0, getWidth(),getHeight(),this);
    }
}
Rohit Malish
  • 3,209
  • 12
  • 49
  • 67

2 Answers2

1

Yeah, you're trying to read in the image as a file -- don't do that since files don't exist within a Jar file. Instead read it in as a resource.

Something like so:

public JPanelWB(String resourceName){
   try {
       // backgroundImage = ImageIO.read(new File(resourceName));
      backgroundImage = ImageIO.read(getClass().getResource(resourceName));
   } catch (IOException ex) {
       new JDialog().add(new Label("Could not open image."+ex.getMessage()));
   }
}

But note that resource path is different from file path. The resource path is relative to the location of your class files.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • See code above. But note that resource path is different from file path. The resource path is relative to the location of your class files. – Hovercraft Full Of Eels May 09 '12 at 16:22
  • @RohitMalish : Hopefully this Thread might help you : [Loading Images for your Project](http://stackoverflow.com/questions/9864267/load-icon-image-exception/9866659#9866659), +1 for the answer though :-) – nIcE cOw May 09 '12 at 16:28
  • so if my image.jpg is located in default package lik "src/image.jpg" what is resource path for that after i have built my project? – Rohit Malish May 09 '12 at 16:34
  • found out myself :) Created new package and moved my image there after that path was mypackage/image.jpg. thanks for solution :) – Rohit Malish May 09 '12 at 16:37
  • Myself, I'd get it out of the default package and instead put it in a package called "image" that is an offshoot of your code package. – Hovercraft Full Of Eels May 09 '12 at 16:37
  • Glad you've got it figured out! – Hovercraft Full Of Eels May 09 '12 at 16:38
  • 2
    @RohitMalish : When you access your resource folder, you always do that with `/` a forward slash. So what ever is inside your resource folder will be accessed something like this `getClass().getResource("/image/myImage.extension")`. In that link provided by me, I had added stuff as to how to add images to NetBeans too, do have a look again :-) – nIcE cOw May 09 '12 at 16:41
0

If you want to read new image and import it as background, people smarter than me already answered your question.

But, if your problem is similar to mine, then this migh help:

If you already have images to show, then the point is to call them from absolute path. Executable class form JAR will read drive created inside virtual machine, not the physical drive in your computer.

Put images in short-pathed folder like

C:\J\proj\img\

and call them with absolute path like

"C:\\J\\proj\\img\\your_image.png" // (Don't forget the double backslashes.)

(If you don't mind path lenght, leave them in image folder inside your project package, and call them from there.)

NetBeans will pack them into JAR with absolute path. On execution JRE will create JVM with that path in it, take the images from JAR and put them to that virtual path. Class will be able to find them, because it doesn't read path from physical drive, but from own virtual one newly created inside JVM.

In that case avoiding ImageIcon is just more clutter, not less.

You can add "blackBoard" as JLabel to be background to your JFrame, set its layout to null, something like this:

    private JLabel blackBoard;
    private JLabel noteToSelf;
    //.....
    blackBoard = new JLabel();
    noteToSelf = new JLabel();
    //.....

    // putting JLabel "blackBoard" as background into JFrame
    blackBoard.setIcon(new ImageIcon("c:\\Java\\images\\MarbleTable.png"));
    getContentPane().add(blackBoard);
    blackBoard.setBounds(1, 1, 400, 440);
    blackBoard.setLayout(null);

and then add components into "blackBoard" instead of your JFrame, like this.

    // putting JLabel "noteToSelf" onto background
    noteToSelf.setIcon(new ImageIcon("c:\\Java\\images\\Sticker_a1.png"));
    // or:  noteToSelf.setText("Remind me at 6:30am...");
    blackBoard.add(noteToSelf);
    noteToSelf.setBounds(noteX, noteY, 64, 48);

Now your JFrame is table board and "blackBoard" is table sheet on it.

Hope this helps.

Sinisa
  • 45
  • 5