ImageIconpublic class ImageIcon extends Object implements Serializable, Icon, AccessibleAn 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}. |
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 | mediaTrackerIDId 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.
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.
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.
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.
this(location, location.toExternalForm());
| public ImageIcon(Image image, String description)Creates an ImageIcon from 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.
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.
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.
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.AccessibleContext | getAccessibleContext()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.
if (accessibleContext == null) {
accessibleContext = new AccessibleImageIcon();
}
return accessibleContext;
| public java.lang.String | getDescription()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 description;
| public int | getIconHeight()Gets the height of the icon.
return height;
| public int | getIconWidth()Gets the width of the icon.
return width;
| public java.awt.Image | getImage()Returns this icon's Image .
return image;
| public int | getImageLoadStatus()Returns the status of the image loading operation.
return loadStatus;
| public java.awt.image.ImageObserver | getImageObserver()Returns the image observer for the image.
return imageObserver;
| private int | getNextID()Returns an ID to use with the MediaTracker in loading an image.
synchronized(tracker) {
return ++mediaTrackerID;
}
| protected void | loadImage(java.awt.Image image)Loads the image, returning only when the image is loaded.
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 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.
if(imageObserver == null) {
g.drawImage(image, x, y, c);
} else {
g.drawImage(image, x, y, imageObserver);
}
| private void | readObject(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 void | setDescription(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.
this.description = description;
| public void | setImage(java.awt.Image image)Sets the image displayed by this icon.
this.image = image;
loadImage(image);
| 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);
imageObserver = observer;
| public java.lang.String | toString()Returns a string representation of this image.
if (description != null) {
return description;
}
return super.toString();
| private void | writeObject(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);
|
|