4
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class Picture extends JFrame{

    public static void main(String[] args){

        new Picture();

    }

    public Picture(){

        this.setTitle("Picture");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p = new JPanel();
        ImageIcon pic = new ImageIcon("TestImage.jpg");
        p.add(new JLabel(pic));
        this.add(p);
        this.pack();
        this.setVisible(true);
        System.out.println(pic);
    }

}

This code as it is just displays a collapsed frame. I would expect that the pack(); method call would size the frame around my .jpg image, correct?

I can already hear the first question, and yes the image is in the same folder as my project package folder.

I am using the Eclipse IDE.

I also added the println(); statement to see what the Image reference returned and it returns "TestImage.jpg". I thought it would return an address of the image in memory.??

I have been reading the forum all morning and I can't really find anything that helps me there without copying and using someone else's more complex code. I am trying to keep this as simple as I can so that I don't confuse myself.

Thanks in advance for any help.

2 Answers2

6

Try:

ImageIcon image = new ImageIcon(getClass().getResource("TestImage.jpg"));

See How to Use Icons tutorial for more details, in particular Loading Images Using getResource section.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
5

the image is in the same folder as my project package folder

That's it.

As written, your program looks for the image in the current working directory, not the package hierarchy.

From the Javadoc for the constructor taking String, it reads the image from the specified filename, as desired. However, when you specify a relative path, that means to read relative to the working directory that the application is running in.

To work around this, you have 2 options:

  • Specify the image filename relative to the working directory your IDE runs your program in. I believe Eclipse runs applications in the project root directory, and the source package hierarchy is rooted at src. In this case it'll work if you specify src/TestImage.jpg. The disadvantage is that if you ever run your program from a different directory, you'll have to move the image file along with it. This is inconvenient for distribution/packaging, because you can't just drop the JAR file and have it run.

  • Use Java's resource loader to load the image file from the package hierarchy. To do this, first use

    getClass().getResource("TestImage.jpg")
    

    to get a URL for the image (relative to the package root). See that ImageIcon has a constructor that accepts a URL to read the image from. So you should read the image using

    new ImageIcon(getClass().getResource("TestImage.jpg"))
    

    The advantage of being relative to he package hierarchy is that the program can be run from any location, and the image can be bundled with your app in a single JAR file.

    Aside: it's best practice to create a package in which you place both code and resources (rather than just placing them in the package root). In that case, pass "com/example/someapp/TestImage.jpg" instead.

Mechanical snail
  • 29,755
  • 14
  • 88
  • 113