Copyright 2012-02-25 Werner Randelshofer

ch.randelshofer.gui
Class ImageIconAWT

java.lang.Object
  extended by ch.randelshofer.gui.ImageIconAWT
All Implemented Interfaces:
IconAWT, javax.swing.Icon

public class ImageIconAWT
extends java.lang.Object
implements IconAWT

An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL or filename 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 XMLEncoder.

Version:
1.49 12/03/01
Author:
Jeff Dinkins, Lynn Monsanto

Field Summary
protected static java.awt.Component component
           
protected static java.awt.MediaTracker tracker
           
 
Constructor Summary
ImageIconAWT()
          Creates an uninitialized image icon.
ImageIconAWT(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 or JPEG.
ImageIconAWT(java.awt.Image image)
          Creates an ImageIcon from an image object.
ImageIconAWT(java.lang.String filename)
          Creates an ImageIcon from the specified file.
ImageIconAWT(java.net.URL location)
          Creates an ImageIcon from the specified URL.
 
Method Summary
 int getIconHeight()
          Gets the height of the icon.
 int getIconWidth()
          Gets the width of the icon.
 java.awt.Image getImage()
          Returns this icon's Image.
 int getImageLoadStatus()
          Returns the status of the image loading operation.
 java.awt.image.ImageObserver getImageObserver()
          Returns the image observer for the image.
protected  void loadImage(java.awt.Image image)
          Loads the image, returning only when the image is loaded.
 void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
          Paints the icon.
 void setImage(java.awt.Image image)
          Sets the image displayed by this icon.
 void setImageObserver(java.awt.image.ImageObserver observer)
          Sets the image observer for the image.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

component

protected static final java.awt.Component component

tracker

protected static final java.awt.MediaTracker tracker
Constructor Detail

ImageIconAWT

public ImageIconAWT(java.lang.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.

Parameters:
filename - the name of the file containing the image

ImageIconAWT

public ImageIconAWT(java.net.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.

Parameters:
location - the URL for the image

ImageIconAWT

public ImageIconAWT(java.awt.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.

Parameters:
image - the image
See Also:
Image.getProperty(java.lang.String, java.awt.image.ImageObserver)

ImageIconAWT

public ImageIconAWT(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 or JPEG. Normally this array is created by reading an image using Class.getResourceAsStream(), but the byte array may also be statically stored in a class.

Parameters:
imageData - an array of pixels in an image format supported by the AWT Toolkit, such as GIF or JPEG.
See Also:
Toolkit.createImage(java.lang.String)

ImageIconAWT

public ImageIconAWT()
Creates an uninitialized image icon.

Method Detail

loadImage

protected void loadImage(java.awt.Image image)
Loads the image, returning only when the image is loaded.

Parameters:
image - the image

getImageLoadStatus

public int getImageLoadStatus()
Returns the status of the image loading operation.

Returns:
the loading status as defined by java.awt.MediaTracker
See Also:
MediaTracker.ABORTED, MediaTracker.ERRORED, MediaTracker.COMPLETE

getImage

public java.awt.Image getImage()
Returns this icon's Image.

Returns:
the Image object for this ImageIcon

setImage

public void setImage(java.awt.Image image)
Sets the image displayed by this icon.

Parameters:
image - the image

paintIcon

public void paintIcon(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.

Specified by:
paintIcon in interface IconAWT
Specified by:
paintIcon in interface javax.swing.Icon
Parameters:
c - the component to be used as the observer if this icon has no image observer
g - the graphics context
x - the X coordinate of the icon's top-left corner
y - the Y coordinate of the icon's top-left corner

getIconWidth

public int getIconWidth()
Gets the width of the icon.

Specified by:
getIconWidth in interface IconAWT
Specified by:
getIconWidth in interface javax.swing.Icon
Returns:
the width in pixels of this icon

getIconHeight

public int getIconHeight()
Gets the height of the icon.

Specified by:
getIconHeight in interface IconAWT
Specified by:
getIconHeight in interface javax.swing.Icon
Returns:
the height in pixels of this icon

setImageObserver

public void setImageObserver(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);
 

Parameters:
observer - the image observer

getImageObserver

public java.awt.image.ImageObserver getImageObserver()
Returns the image observer for the image.

Returns:
the image observer, which may be null

Copyright 2012-02-25 Werner Randelshofer