public class ImageIcon extends Object implements Icon, Serializable, 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 JavaBeans™ has been added to the java.beans
package. Please see XMLEncoder
.
Modifier and Type | Class and Description |
---|---|
protected class |
ImageIcon.AccessibleImageIcon This class implements accessibility support for the |
@Deprecated protected static final Component component
Deprecated. since 1.8
Do not use this shared component, which is used to track image loading. It is left for backward compatibility only.
@Deprecated protected static final MediaTracker tracker
Deprecated. since 1.8
Do not use this shared media tracker, which is used to load images. It is left for backward compatibility only.
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.
filename
- the name of the file containing the imagedescription
- a brief textual description of the imageImageIcon(String)
@ConstructorProperties(value="description") 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. filename
- a String specifying a filename or pathgetDescription()
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.
location
- the URL for the imagedescription
- a brief textual description of the imageImageIcon(String)
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.
location
- the URL for the imagegetDescription()
public ImageIcon(Image image, String description)
Creates an ImageIcon from the image.
image
- the imagedescription
- a brief textual description of the imagepublic 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.
image
- the imagegetDescription()
, Image.getProperty(java.lang.String, java.awt.image.ImageObserver)
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.
imageData
- an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNGdescription
- a brief textual description of the imageToolkit.createImage(java.lang.String)
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.
imageData
- an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNGToolkit.createImage(java.lang.String)
, getDescription()
, Image.getProperty(java.lang.String, java.awt.image.ImageObserver)
public ImageIcon()
Creates an uninitialized image icon.
protected void loadImage(Image image)
Loads the image, returning only when the image is loaded.
image
- the imagepublic int getImageLoadStatus()
Returns the status of the image loading operation.
MediaTracker.ABORTED
, MediaTracker.ERRORED
, MediaTracker.COMPLETE
public Image getImage()
Returns this icon's Image
.
Image
object for this ImageIcon
public void setImage(Image image)
Sets the image displayed by this icon.
image
- the imagepublic 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.
public void setDescription(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.
description
- a brief textual description of the imagepublic void paintIcon(Component c, 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.
paintIcon
in interface Icon
c
- the component to be used as the observer if this icon has no image observerg
- the graphics contextx
- the X coordinate of the icon's top-left cornery
- the Y coordinate of the icon's top-left cornerpublic int getIconWidth()
Gets the width of the icon.
getIconWidth
in interface Icon
public int getIconHeight()
Gets the height of the icon.
getIconHeight
in interface Icon
public void setImageObserver(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);
observer
- the image observerpublic ImageObserver getImageObserver()
Returns the image observer for the image.
public String toString()
Returns a string representation of this image.
public 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.
getAccessibleContext
in interface Accessible
© 1993–2017, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.