This page was saved using WebZIP 6.0.8.918 (Unregistered) on 01/20/05 오후 3:27:47.
Address: http://www.leepoint.net/notes-java/30GUI/20graphics/45imageicon.html
Title: Java: Images - ImageIcon  •  Size: 4273  •  Last Modified: Fri, 14 Jan 2005 00:25:21 GMT

Java: Images - ImageIcon

Use javax.swing.ImageIcon is commonly 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)
In this simple approach load 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)
You can start the image loading (in a separate thread), and proceed with other initialization. To check the status of the load (disks, and the Internet are so slow compared to the CPU), you use an ImageObserver. Unless you are loading many images, or a large image, over the Internet, I don't recommend this extra complication.

To load an ImageIcon from a file

A filename in an ImageIcon constructor specifies the filename relative to the location of the class file [not always true. See TODO note below]. This constructor doesn't return until the ImageIcon is completely loaded.

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

Note: Java may not always use the current directory of the executing program to make relative file references. A simple, but completely non-portable fix, is to give the entire file path. The better solution, calling getResource() should be used, but it isn't described here yet.

[TODO: The correct solution, compatible with executable jar files, applets, and Web Start, packages, etc, is to use MyClass.class.getResource(relativePath). This works in most (all?) combinations as of Java 1.4. The Java Tutorial has a description of this at http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html#getresource]

To load an ImageIcon from a URL

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

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();