Java: Images - ImageIcon

javax.swing.ImageIcon is used for images, both to use on buttons and labels, and to draw in a graphics panel. The supported formats are .gif, .jpg, and .png.

Wait until loaded or overlap loading with execution

Wait until loaded (recommended where timing is not critical)
This simple approach loads the image "synchronously", meaning that your program waits until the image loading is finished. If you do this, you don't have the extra complication of using ImageObserver. For a small number of small images from disk, this is the best choice. The examples here are written in this style.
Overlap (use only for performance problems)
Start loading each image in its own thread, and proceed with other initialization. To check the status of the load (disks, and the Internet are so slow compared to the CPU), use ImageObserver. Unless you are loading many images, or a large image, over the Internet, I don't recommend the extra complication.

To load an ImageIcon from a URL

java.net.URL where = new URL("http://www.yahoo.com/logo.jpeg");
ImageIcon anotherIcon = new ImageIcon(where);

To load an ImageIcon from a file

A filename in an ImageIcon constructor specifies the filename relative to the location of the class file. This constructor doesn't return until the ImageIcon is completely loaded.

Warning: Just putting the file name or path in the ImageIcon constructor won't work in general for applets or executable jar files. See below.

ImageIcon myIcon = new ImageIcon("images/myPic.gif");

Warning: Just putting the file name or path in the ImageIcon constructor won't work in general for applets, WebStart applications, and executable jar files. See below.

Bundling images in your .jar file using NetBeans 4.0

Let's say you have a directory (cards) of images (cards/ad.gif, ...). And let's assume your program is in a package called cardplayer, and you're trying to load the image within the class Card.

  1. Your Java source files should already be in the src/cardplayer directory. Add your image directory (cards) to this directory, so you have src/cardplayer/cards/ad.gif, etc.
  2. Use the following code to load the images:
    ClassLoader cldr = this.getClass().getClassLoader();
    
    java.net.URL imageURL   = cldr.getResource("cardplayer/cards/ad.gif");
    ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
    
  3. Build Project (or perhaps Clean and Build Project).
  4. The double-clickable jar file (dist/cardplayer.jar) can now be run, or the program can be executed in NetBeans.

To use an ImageIcon in a JButton

ImageIcon leftArrow = new ImageIcon("leftarrow.gif");
JButton left = new JButton(leftArrow);

To draw (paint) an ImageIcon

An ImageIcon, img, can be drawn on components (usually a JPanel) using

   img.paintIcon(Component c, Graphics g, int x, int y);

Display the image on a subclass of JPanel used for graphics. Put the paintIcon call in the paintComponent method of that panel. To paint the ImageIcon img on the current panel (ie, this), use a call like:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    img.paintIcon(this, g, 100, 100);
}

Other ImageIcon methods

You can find the width and height of an image with

int w = img.getIconWidth();
int h = img.getIconHeight();

Where to find icons

Image webliography