Home Button
Contents
Swing Logo

Swinging Duke
Feedback Button
Left ArrowRight Arrow

 

Icons and Fills in Swing Components

Modern GUI toolkits generally allow programmers to specify the appearance of a button or a menu item with a string or an image, or both. Most toolkits also allow the developer to fill a region, such as the background of a toolbar, either with a solid color or with a pattern created by tiling an image.

Goals

In the prototype version of Swing, we used java.awt.Image properties for this purpose, but found them difficult to use for the following reasons:

The IFC (Internet Foundation Class) collection of classes from Netscape deals with these issues by providing a subclass of the Image class named Bitmap. This subclass loads images synchronously by default, and supports the IFC persistence interface. The IFC Bitmap class provides draw methods that let you render an image as part of a label if that is what you want to do. This is a particularly useful feature when you want to create a component with a label that uses both a string and an image. That is a convenient technique because it lets the component take care of rendering the string and computing the position of the image.

How Swing Implements Icons

Swing provides this kind of capability in a slightly different way, but in a way that has similar benefits. In Swing, a type named Icon is used for any property of a UI Component icon or label whose value is a small fixed-size image. Icon is defined as follows:

interface Icon extends java.io.Serializable
{
    void paint(Graphics g, int x, int y);
    int getWidth();
    int getHeight();
}

In Swing, an Icon is a small, fixed-size, output-only object. It is implemented as a property whose value is effectively a picture or an icon. A method named Icon.paint() renders the icon's upper left corner at x, y. The paint() method used by icons draws only within a getWidth()-by-getHeight() rectangle.

A Swing component that displays a icon computes the icon's position and then paints, as shown here:

public class Foo extends JComponent
{
     protected Icon labelIcon;

     public void paint(Graphics g) {
         int x, y;
         // ... 
         labelIcon.paint(g, x, y);
     }
}

An implementation of Icon called ImageIcon encapsulates an Image and deals with synchronous loading and serialization:

public class ImageIcon implements Icon
{
    public ImageIcon(Image x);
    public ImageIcon(URL x);
    public ImageIcon(String x);

    public static ImageIcon createImageIcon(Image x);
    public static ImageIcon createImageIcon(URL x);
    public static ImageIcon createImageIcon(String x);

}

The basic idea here is that the constructors load an image from the default toolkit. The create methods construct new icons as necessary.

Applications that use icons can install the corresponding image files in a directory that's easy to find if you know the directory containing the class that uses the icon. For example, suppose you install an icon named icon.gif in the same directory as the class that uses it -- for example, Foo.class. Subsequenly, at runtime, you can then construct the icon using Class.getResource(). For example:

URL = Foo.class.getResource("icon.gif");
ImageIcon icon = new ImageIcon(url);


Version 0.4. Last modified 09/04/97.
Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.

Sun's Home Page