javax.swing
Class ImageIcon
java.lang.Object
|
+--javax.swing.ImageIcon
- All Implemented Interfaces:
- Accessible, Icon, Serializable
- public class ImageIcon
- extends Object
- implements Icon, Serializable, Accessible
Safe: 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 java.beans.XMLEncoder.
- Version:
- 1.49 12/03/01
- Author:
- Jeff Dinkins, Lynn Monsanto
- See Also:
- Serialized Form
Field Summary
private javax.swing.ImageIcon.AccessibleImageIcon
accessibleContext
--- Accessibility Support ---
private static int
mediaTrackerID
Id used in loading images from MediaTracker.
(package private) int
width
Constructor Summary
ImageIcon()
Enabled: Creates an uninitialized image icon.
ImageIcon(byte[] imageData)
Enabled: 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.
ImageIcon(byte[] imageData,
String description)
Enabled: 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.
ImageIcon(Image image)
Enabled: Creates an ImageIcon from an image object.
ImageIcon(String filename)
Suppressed: Creates an ImageIcon from the specified file.
ImageIcon(URL location)
Enabled: Creates an ImageIcon from the specified URL.
ImageIcon(URL location,
String description)
Enabled: Creates an ImageIcon from the specified URL.
Method Summary
java.awt.image.ImageObserver
getImageObserver()
Enabled: Returns the image observer for the image.
private int
getNextID()
Returns an ID to use with the MediaTracker in loading an image.
protected void
loadImage(Image image)
Loads the image, returning only when the image is loaded.
static ImageIcon
run(File file)
Added: Instead of the security-breaking suppressed 'new(String)'.
static ImageIcon
run(File file,
String description)
Added: Instead of the security-breaking suppressed 'new(String, String)'.
void
setImage(Image image)
Enabled: Sets the image displayed by this icon.
void
setImageObserver(java.awt.image.ImageObserver observer)
Enabled:
Sets the image observer for the image.
String
toString()
Suppressed: Returns a string representation of this image.
Methods inherited from class java.lang.Object
Field Detail
filename
private transient String filename
location
private transient URL location
image
transient Image image
loadStatus
transient int loadStatus
imageObserver
java.awt.image.ImageObserver imageObserver
description
String description
component
protected static final Component component
tracker
protected static final MediaTracker tracker
mediaTrackerID
private static int mediaTrackerID
- Id used in loading images from MediaTracker.
width
int width
height
int height
accessibleContext
private javax.swing.ImageIcon.AccessibleImageIcon accessibleContext
- --- Accessibility Support ---
Constructor Detail
ImageIcon
public ImageIcon(String filename,
String description)
- Suppressed: 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 imagedescription - a brief textual description of the image- See Also:
ImageIcon(String)
ImageIcon
public ImageIcon(String filename)
- Suppressed: 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- See Also:
getDescription()
ImageIcon
public ImageIcon(URL location,
String description)
- Enabled: 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 imagedescription - a brief textual description of the image- See Also:
ImageIcon(String)
ImageIcon
public ImageIcon(URL location)
- Enabled: 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.
- Parameters:
location - the URL for the image- See Also:
getDescription()
ImageIcon
public ImageIcon(Image image,
String description)
- Enabled: Creates an ImageIcon from the image.
- Parameters:
image - the imagedescription - a brief textual description of the image
ImageIcon
public ImageIcon(Image image)
- Enabled: 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:
getDescription(),
java.awt.Image#getProperty
ImageIcon
public ImageIcon(byte[] imageData,
String description)
- Enabled: 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.description - a brief textual description of the image- See Also:
java.awt.Toolkit#createImage
ImageIcon
public ImageIcon(byte[] imageData)
- Enabled: 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.
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 or JPEG- See Also:
java.awt.Toolkit#createImage,
getDescription(),
java.awt.Image#getProperty
ImageIcon
public ImageIcon()
- Enabled: Creates an uninitialized image icon.
Method Detail
loadImage
protected void loadImage(Image image)
- Loads the image, returning only when the image is loaded.
-
- Parameters:
image - the image
getNextID
private int getNextID()
- Returns an ID to use with the MediaTracker in loading an image.
-
-
getImageLoadStatus
public int getImageLoadStatus()
- Enabled: Returns the status of the image loading operation.
-
- Returns:
- the loading status as defined by java.awt.MediaTracker
- See Also:
java.awt.MediaTracker#ABORTED,
java.awt.MediaTracker#ERRORED,
java.awt.MediaTracker#COMPLETE
getImage
public Image getImage()
- Enabled: Returns this icon's
Image.
-
- Returns:
- the
Image object for this ImageIcon
setImage
public void setImage(Image image)
- Enabled: Sets the image displayed by this icon.
-
- Parameters:
image - the image
getDescription
public String getDescription()
- Enabled: 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.
-
- Returns:
- a brief textual description of the image
setDescription
public void setDescription(String description)
- Enabled: 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.
-
- Parameters:
description - a brief textual description of the image
paintIcon
public void paintIcon(Component c,
Graphics g,
int x,
int y)
- Suppressed: 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 Icon
- Parameters:
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 corner
getIconWidth
public int getIconWidth()
- Enabled: Gets the width of the icon.
- Specified by:
getIconWidth in interface Icon
- Returns:
- the width in pixels of this icon
getIconHeight
public int getIconHeight()
- Enabled: Gets the height of the icon.
- Specified by:
getIconHeight in interface Icon
- Returns:
- the height in pixels of this icon
setImageObserver
public void setImageObserver(java.awt.image.ImageObserver observer)
- Enabled:
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()
- Enabled: Returns the image observer for the image.
-
- Returns:
- the image observer, which may be null
toString
public String toString()
- Suppressed: Returns a string representation of this image.
- Overrides:
toString in class Object
- Returns:
- a string representing this image
readObject
private void readObject(ObjectInputStream s)
throws ClassNotFoundException,
IOException
-
ClassNotFoundException
IOException
writeObject
private void writeObject(ObjectOutputStream s)
throws IOException
-
IOException
getAccessibleContext
public AccessibleContext getAccessibleContext()
- Suppressed:
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.
- Specified by:
getAccessibleContext in interface Accessible
- Returns:
- an AccessibleImageIcon that serves as the
AccessibleContext of this ImageIcon
run
public static ImageIcon run(File file,
String description)
- Added: Instead of the security-breaking suppressed 'new(String, String)'.
-
-
run
public static ImageIcon run(File file)
- Added: Instead of the security-breaking suppressed 'new(String)'.
-
-