The ImageIcon Class
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 ImageIcons 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.
Properties
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.
Table 4-4. ImageIcon properties
Property | Data type | get | is | set | Default value |
|---|---|---|---|---|---|
description | String | · | · | null | |
| int | · | -1 | ||
| int | · | -1 | ||
image | Image | · | · | null | |
imageLoadStatus | int | · | 0 | ||
imageObserver ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access