Swing provides a concrete implementation of the Icon
interface that is considerably more
useful than our OvalIcon
class.
ImageIcon
uses a java.awt.Image
object to store and display any
graphic and provides synchronous image loading (i.e., the Image
is loaded completely before returning),
making ImageIcon
s very powerful and
easy to use. You can even use an ImageIcon
to display an animated GIF89a,
making the ubiquitous “animation applet” as simple as this:
// AnimationApplet.java // import javax.swing.*; // A simple animation applet public class AnimationApplet extends JApplet { public void init( ) { ImageIcon icon = new ImageIcon("images/rolling.gif"); // Animated gif getContentPane( ).add(new JLabel(icon)); } }
All we did here was load an animated GIF in the init( )
method and then add it to the applet.
For more information on JApplet
, see
Chapter 8.
ImageIcon
currently supports
the JPEG, GIF (including animation and transparency), PNG, and XBM image
formats. TIFF support should be coming soon. SVG might be supported
eventually.
The ImageIcon
class defines
the properties listed in Table
4-4. The description
property
allows an arbitrary description of the image to be specified. One
possible use of this property might be to give a blind user an audio
description of the image.
The iconHeight
and iconWidth
properties default to -1
if no
image is loaded by the constructor, while the image
property
simply contains the Image
object
rendered by the icon. ImageLoadStatus
indicates the success or failure of the image load
process using the constants defined in java.awt.MediaTracker
(ABORTED
, ERRORED
, or COMPLETE
). The default for this property is
0
, which does not map to any of
these constants.
The imageObserver
property contains the ImageObserver
that should receive
notifications of changes to the image. If this property is null
(as it is by default), the component
containing the icon will be treated as the image observer when the
image is painted.
Figure 4-9 shows a
class diagram for ImageIcon
and the
classes related to it.
Like most Swing classes, ImageIcon
implements Serializable
. The keen observer may see a
problem with this: the java.awt.Image
class used by ImageIcon
is not
serializable. By default, this would keep ImageIcon
objects from serializing properly.
The good news is that ImageIcon
implements its own readObject( )
and writeObject( )
methods so that the pixel representation of the image is stored and
retrieved correctly.
- ImageIcon( )
Create an uninitialized
ImageIcon
.- ImageIcon(Image image)
ImageIcon(Image image, String description) Create
ImageIcon
objects from an existing image. A textual description of the image may be provided. If no description is provided, an attempt is made to retrieve the “comment” property from the inputImage
. If this is a non-null
string, it is used as the description.- ImageIcon(String filename)
ImageIcon(String filename, String description) Create
ImageIcon
objects from the contents of the specified JPEG, PNG, GIF, or XBM file. The image is guaranteed to be completely loaded (unless an error occurs) when the constructor returns.- ImageIcon(URL location)
ImageIcon(URL location, String description) Create
ImageIcon
objects from the contents of the specifiedjava.net.URL
. The image is guaranteed to be completely loaded (unless an error occurs) when the constructor returns.- public ImageIcon(byte imageData[])
public ImageIcon(byte imageData[], String description) Create
ImageIcon
objects from an array of bytes containing image data in a supported format, such as JPEG, PNG, GIF, or XBM.
- public void paintIcon(Component c, Graphics g, int x, int y)
Paint the
Image
at the specified location on the suppliedGraphics
. The givenComponent
is passed to theGraphics
’sdrawImage( )
method as theImageObserver
(recall thatjava.awt.Component
implementsImageObserver
) if no image observer has been explicitly set.
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.