![]() |
|||
![]() ![]() |
![]() |
|
![]() |
IconsIn the AWT, bitmap graphics were drawn by using the Image class. In the JFC, components that use bitmap graphics require a class that implements the Icon interface. Thus, in the JFC, icons replace images for component decorations. However, the methods typically used to load or create a bitmap are the Toolkit image methods, which still return an image. To aid in this conversion, the JFC provides the ImageIcon class. Icon InterfaceThe Icon interface defines the methods that define an icon. Any class can implement this interface and be used wherever an icon is wanted. The methods defined in the Icon interface are used to query the size of the icon and to do the actual icon rendering. The following shows these methods in the Icon interface. public interface Icon { public abstract void paintIcon(Component c, Graphics g, int x, int y) public abstract int getIconWidth(); public abstract int getIconHeight(); } ImageIcon ClassThe ImageIcon class is the Icon workhorse class in the JFC. This class contains constructors to create an icon from a file, URL, byte array, or an Image. The image can be set after creation to a new Image instance. The class also contains a method to obtain the image from the class. This provides a conversion between images and icons. A description can be specified with the image. Listing 5.5 presents a utility class that can be used to load images and create icons. Since the ImageIcon class implements the Icon interface, it can be used for creating icons. The ImageLoader class extends Component. This allows it to be an ImageObserver during image loading. The class contains two static methods for image manipulation. The first, loadImage, attempts to load an image contained in the named resource. The method tries to get a URL to the resource. This allows the method to find images in JAR files as well as from a file system. If the resource is found, a MediaTracker is used to load the image. The method waits for the image to load, so the calling code can use the image immediately. The loadImage method returns an image. The second static method, loadIcon, returns an icon. This method is used more in JFC programming. The loadIcon method calls the loadImage method to load the requested image. If the image is successfully loaded, an ImageIcon is created from the image. Because ImageIcon implements Icon, returning the ImageIcon is, in effect, returning an icon. The ImageLoader class is used in the border example application presented in the last section and shown in Listing 5.4. If the image is stored in a file and loaded from an application, or if the URL pointing to the image data is already created, the ImageIcon class itself can be used to load the image. This is done by passing the filename or URL to the constructor of the ImageIcon class. During construction, the ImageIcon class will load the image by using a MediaTracker. This ensures that the image is available after the constructor has completed. Listing 5.5 ImageLoader Class for Loading Images and Creating Icons /** * A class containing static methods which can * be used to load Images and Icons. This class will * find the named resource in the local file system, * or a jar file, specified in the CLASSPATH. * <p> * @author Mike Foley * @version 1.2 **/ public class ImageLoader extends Component { private static ImageLoader imageLoader; /** * ImageLoader, constructor. * <p> * There should not be any instances of this * class created outside the private one used * in the class. **/ private ImageLoader() { super(); } /** * loadImage * <p> * Load the image with the given name. * <p> * @param imageName The name of the image to load. * @return The Image for the image with the given name. * @see #loadIcon **/ public static Image loadImage( String imageName ) throws java.lang.InterruptedException { // // get the image, and wait for it to be loaded. // URL url = Object.class.getResource( imageName ); // // If the URL could not be located above, try // prepending a / to the images ResourceName. This // will cause the search to begin at the top of the // CLASSPATH. // if( url == null ) url = Object.class.getResource( / + imageName ); if( url == null ) { RuntimeException e = new RuntimeException( Image + imageName + not found ); e.printStackTrace(); throw e; } // // get the image, and wait for it to be loaded. // Image image = Toolkit.getDefaultToolkit().getImage( url ); MediaTracker tracker = new MediaTracker( imageLoader ); tracker.addImage( image, 0 ); tracker.waitForID( 0 ); return( image ); } // loadImage /** * loadIcon * <p> * Load the Icon with the given name. * <p> * @param imageName The name of the image to load. * @return The Icon for the image with the given name. * @see #loadImage **/ public static Icon loadIcon( String imageName ) throws java.lang.InterruptedException { Image image = loadImage( imageName ); return( new ImageIcon( image ) ); } // loadIcon /** * static initialization. * <p> * Create an instance of this class that can be * used as the parameter to the MediaTracker while * loading images. **/ static { imageLoader = new ImageLoader(); } // static } // ImageLoader
|
![]() |
|