| Constructor: |
public ImageIcon() {
}
Creates an uninitialized image icon. |
public ImageIcon(String filename) {
this(filename, 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.Parameters:
filename - a String specifying a filename or path
Also see:
- getDescription
|
public ImageIcon(URL location) {
this(location, location.toExternalForm());
}
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. |
public ImageIcon(Image image) {
this.image = image;
Object o = image.getProperty("comment", imageObserver);
if (o instanceof String) {
description = (String) o;
}
loadImage(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
Also see:
- getDescription
- java.awt.Image#getProperty
|
public ImageIcon(byte[] imageData) {
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);
}
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. Parameters:
imageData - an array of pixels in an image format supported by
the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
Also see:
- java.awt.Toolkit#createImage
- getDescription
- java.awt.Image#getProperty
|
public ImageIcon(String filename,
String description) {
image = Toolkit.getDefaultToolkit().getImage(filename);
if (image == null) {
return;
}
this.filename = filename;
this.description = description;
loadImage(image);
}
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
description - a brief textual description of the image
Also see:
- ImageIcon(String)
|
public ImageIcon(URL location,
String description) {
image = Toolkit.getDefaultToolkit().getImage(location);
if (image == null) {
return;
}
this.location = location;
this.description = description;
loadImage(image);
}
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
description - a brief textual description of the image
Also see:
- ImageIcon(String)
|
public ImageIcon(Image image,
String description) {
this(image);
this.description = description;
}
Creates an ImageIcon from the image. Parameters:
image - the image
description - a brief textual description of the image
|
public ImageIcon(byte[] imageData,
String description) {
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
if (image == null) {
return;
}
this.description = description;
loadImage(image);
}
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. Parameters:
imageData - an array of pixels in an image format supported
by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG
description - a brief textual description of the image
Also see:
- java.awt.Toolkit#createImage
|
| Method from javax.swing.ImageIcon Detail: |
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleImageIcon();
}
return accessibleContext;
}
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. |
public String getDescription() {
return description;
}
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. |
public int getIconHeight() {
return height;
}
Gets the height of the icon. |
public int getIconWidth() {
return width;
}
Gets the width of the icon. |
public Image getImage() {
return image;
}
Returns this icon's Image. |
public int getImageLoadStatus() {
return loadStatus;
}
Returns the status of the image loading operation. |
public ImageObserver getImageObserver() {
return imageObserver;
}
Returns the image observer for the image. |
protected void loadImage(Image image) {
MediaTracker mTracker = getTracker();
synchronized(mTracker) {
int id = getNextID();
mTracker.addImage(image, id);
try {
mTracker.waitForID(id, 0);
} catch (InterruptedException e) {
System.out.println("INTERRUPTED while loading Image");
}
loadStatus = mTracker.statusID(id, false);
mTracker.removeImage(image, id);
width = image.getWidth(imageObserver);
height = image.getHeight(imageObserver);
}
}
Loads the image, returning only when the image is loaded. |
public synchronized void paintIcon(Component c,
Graphics g,
int x,
int y) {
if(imageObserver == null) {
g.drawImage(image, x, y, c);
} else {
g.drawImage(image, x, y, imageObserver);
}
}
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. |
public void setDescription(String description) {
this.description = 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. |
public void setImage(Image image) {
this.image = image;
loadImage(image);
}
Sets the image displayed by this icon. |
public void setImageObserver(ImageObserver observer) {
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);
|
public String toString() {
if (description != null) {
return description;
}
return super.toString();
}
Returns a string representation of this image. |