FileDocCategorySizeDatePackage
ImageIcon.javaAPI DocJava SE 5 API18599Fri Aug 26 14:57:54 BST 2005javax.swing

ImageIcon

public class ImageIcon extends Object implements Serializable, Icon, Accessible
An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL, filename or byte array are preloaded using MediaTracker to monitor the loaded state of the image.

For further information and examples of using image icons, see How to Use Icons in The Java Tutorial.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.53 12/19/03
author
Jeff Dinkins
author
Lynn Monsanto

Fields Summary
private transient String
filename
private transient URL
location
transient Image
image
transient int
loadStatus
ImageObserver
imageObserver
String
description
protected static final Component
component
protected static final MediaTracker
tracker
private static int
mediaTrackerID
Id used in loading images from MediaTracker.
int
width
int
height
private AccessibleImageIcon
accessibleContext
--- Accessibility Support ---
Constructors Summary
public ImageIcon(String filename, String description)
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image.

param
filename the name of the file containing the image
param
description a brief textual description of the image
see
#ImageIcon(String)


                                                     
         
	image = Toolkit.getDefaultToolkit().getImage(filename);
        if (image == null) {
            return;
        }
	this.filename = filename; 
        this.description = description;
	loadImage(image);
    
public ImageIcon(String filename)
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path. When specifying a path, use the Internet-standard forward-slash ("/") as a separator. (The string is converted to an URL, so the forward-slash works on all systems.) For example, specify:
new ImageIcon("images/myImage.gif") 
The description is initialized to the filename string.

param
filename a String specifying a filename or path
see
#getDescription

        this(filename, filename);
    
public ImageIcon(URL location, String description)
Creates an ImageIcon from the specified URL. The image will be preloaded by using MediaTracker to monitor the loaded state of the image.

param
location the URL for the image
param
description a brief textual description of the image
see
#ImageIcon(String)

	image = Toolkit.getDefaultToolkit().getImage(location);
        if (image == null) {
            return;
        } 
	this.location = location; 
        this.description = description;
	loadImage(image);
    
public ImageIcon(URL location)
Creates an ImageIcon from the specified URL. The image will be preloaded by using MediaTracker to monitor the loaded state of the image. The icon's description is initialized to be a string representation of the URL.

param
location the URL for the image
see
#getDescription

        this(location, location.toExternalForm());
    
public ImageIcon(Image image, String description)
Creates an ImageIcon from the image.

param
image the image
param
description a brief textual description of the image

        this(image);
        this.description = description;
    
public ImageIcon(Image image)
Creates an ImageIcon from an image object. If the image has a "comment" property that is a string, then the string is used as the description of this icon.

param
image the image
see
#getDescription
see
java.awt.Image#getProperty

	this.image = image;
        Object o = image.getProperty("comment", imageObserver);
        if (o instanceof String) {
            description = (String) o;
        }
	loadImage(image);
    
public ImageIcon(byte[] imageData, String description)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG. Normally this array is created by reading an image using Class.getResourceAsStream(), but the byte array may also be statically stored in a class.

param
imageData an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
param
description a brief textual description of the image
see
java.awt.Toolkit#createImage

	this.image = Toolkit.getDefaultToolkit().createImage(imageData);
        if (image == null) {
            return;
        }
        this.description = description;
	loadImage(image);
    
public ImageIcon(byte[] imageData)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG. Normally this array is created by reading an image using Class.getResourceAsStream(), but the byte array may also be statically stored in a class. If the resulting image has a "comment" property that is a string, then the string is used as the description of this icon.

param
imageData an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
see
java.awt.Toolkit#createImage
see
#getDescription
see
java.awt.Image#getProperty

	this.image = Toolkit.getDefaultToolkit().createImage(imageData);
        if (image == null) {
            return;
        }
        Object o = image.getProperty("comment", imageObserver);
        if (o instanceof String) {
            description = (String) o;
        }
	loadImage(image);
    
public ImageIcon()
Creates an uninitialized image icon.

    
Methods Summary
public javax.accessibility.AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this ImageIcon. For image icons, the AccessibleContext takes the form of an AccessibleImageIcon. A new AccessibleImageIcon instance is created if necessary.

return
an AccessibleImageIcon that serves as the AccessibleContext of this ImageIcon
beaninfo
expert: true description: The AccessibleContext associated with this ImageIcon.


                                                                            
       
        if (accessibleContext == null) {
            accessibleContext = new AccessibleImageIcon();
        }
        return accessibleContext;
    
public java.lang.StringgetDescription()
Gets the description of the image. This is meant to be a brief textual description of the object. For example, it might be presented to a blind user to give an indication of the purpose of the image. The description may be null.

return
a brief textual description of the image

	return description;
    
public intgetIconHeight()
Gets the height of the icon.

return
the height in pixels of this icon

	return height;
    
public intgetIconWidth()
Gets the width of the icon.

return
the width in pixels of this icon

	return width;
    
public java.awt.ImagegetImage()
Returns this icon's Image.

return
the Image object for this ImageIcon

	return image;
    
public intgetImageLoadStatus()
Returns the status of the image loading operation.

return
the loading status as defined by java.awt.MediaTracker
see
java.awt.MediaTracker#ABORTED
see
java.awt.MediaTracker#ERRORED
see
java.awt.MediaTracker#COMPLETE

        return loadStatus;
    
public java.awt.image.ImageObservergetImageObserver()
Returns the image observer for the image.

return
the image observer, which may be null

        return imageObserver;
    
private intgetNextID()
Returns an ID to use with the MediaTracker in loading an image.

        synchronized(tracker) {
            return ++mediaTrackerID;
        }
    
protected voidloadImage(java.awt.Image image)
Loads the image, returning only when the image is loaded.

param
image the image

	synchronized(tracker) {
            int id = getNextID();

	    tracker.addImage(image, id);
	    try {
		tracker.waitForID(id, 0);
	    } catch (InterruptedException e) {
		System.out.println("INTERRUPTED while loading Image");
	    }
            loadStatus = tracker.statusID(id, false);
	    tracker.removeImage(image, id);

	    width = image.getWidth(imageObserver);
	    height = image.getHeight(imageObserver);
	}
    
public synchronized voidpaintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
Paints the icon. The top-left corner of the icon is drawn at the point (x, y) in the coordinate space of the graphics context g. If this icon has no image observer, this method uses the c component as the observer.

param
c the component to be used as the observer if this icon has no image observer
param
g the graphics context
param
x the X coordinate of the icon's top-left corner
param
y the Y coordinate of the icon's top-left corner

        if(imageObserver == null) {
           g.drawImage(image, x, y, c);
        } else {
	   g.drawImage(image, x, y, imageObserver);
        }
    
private voidreadObject(java.io.ObjectInputStream s)

	s.defaultReadObject();
    
	int w = s.readInt();
	int h = s.readInt();
	int[] pixels = (int[])(s.readObject());

        if (pixels != null) {
	    Toolkit tk = Toolkit.getDefaultToolkit();
	    ColorModel cm = ColorModel.getRGBdefault();
	    image = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w));
	    loadImage(image);
        } 
    
public voidsetDescription(java.lang.String description)
Sets the description of the image. This is meant to be a brief textual description of the object. For example, it might be presented to a blind user to give an indication of the purpose of the image.

param
description a brief textual description of the image

	this.description = description;
    
public voidsetImage(java.awt.Image image)
Sets the image displayed by this icon.

param
image the image

	this.image = image;
	loadImage(image);
    
public voidsetImageObserver(java.awt.image.ImageObserver observer)
Sets the image observer for the image. Set this property if the ImageIcon contains an animated GIF, so the observer is notified to update its display. For example:
icon = new ImageIcon(...)
button.setIcon(icon);
icon.setImageObserver(button);

param
observer the image observer

        imageObserver = observer;
    
public java.lang.StringtoString()
Returns a string representation of this image.

return
a string representing this image

        if (description != null) {
            return description;
        }
        return super.toString();
    
private voidwriteObject(java.io.ObjectOutputStream s)

	s.defaultWriteObject();

	int w = getIconWidth();
	int h = getIconHeight();
	int[] pixels = image != null? new int[w * h] : null;

        if (image != null) {
	    try {
	        PixelGrabber pg = new PixelGrabber(image, 0, 0, w, h, pixels, 0, w);
	        pg.grabPixels();
	        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
		    throw new IOException("failed to load image contents");
	        }
	    }
	    catch (InterruptedException e) {
	        throw new IOException("image load interrupted");
	    }
        }
    
	s.writeInt(w);
	s.writeInt(h);
	s.writeObject(pixels);