Loading, Displaying and Scaling Images
Java's multimedia capabilities include graphics, images, animations, sounds and video. We begin our discussion with images. We will use a few different images in this chapter. Developers can create such images with any image software, such as Adobe® Photoshop™, Jasc® Paint Shop Pro™ or Microsoft® Paint.
The applet of Fig. 21.1 demonstrates loading an Image (package java.awt) and loading an ImageIcon (package javax.swing). Both classes are used to load and display images. The applet displays the Image in its original size and scaled to a larger size, using two versions of Graphics method drawImage. The applet also draws the ImageIcon, using the icon's method paintIcon. Class ImageIcon implements interface Serializable, which allows ImageIcon objects to be easily written to a file or sent across the Internet. Class ImageIcon is also easier to use than Image, because its constructor can receive arguments of several different formats, including a byte array containing the bytes of an image, an Image already loaded in memory, and a String or a URL object, which both can be used to represent the location of the image. A URL object represents a Uniform Resource Locator that serves as a pointer to a resource on the World Wide Web, on your computer or on any networked machine. A URL object is more often used when accessing data over the Internet, while a simple string is more often used when accessing data on the current machine. Using an URL object also enables the programmer to access information from the Web, such as searching for information from a database or through a search engine.
Figure 21.1. Loading and displaying an image in an applet.
(This item is displayed on pages 980 - 981 in the print version)
1 // Fig. 21.1: LoadImageAndScale.java 2 // Load an image and display it in its original size and twice its 3 // original size. Load and display the same image as an ImageIcon. 4 import java.awt.Graphics; 5 import java.awt.Image; 6 import javax.swing.ImageIcon; 7 import javax.swing.JApplet; 8 9 public class LoadImageAndScale extends JApplet 10 { 11 private Image image1; // create Image object 12 private ImageIcon image2; // create ImageIcon object 13 14 // load image when applet is loaded 15 public void init() 16 { 17 image1 = getImage( getDocumentBase(), "redflowers.png" ); 18 image2 = new ImageIcon( "yellowflowers.png" ); 19 } // end method init 20 21 // display image 22 public void paint( Graphics g ) 23 { 24 super.paint( g ); 25 26 g.drawImage( image1, 0, 0, this ); // draw original image 27 28 // draw image to fit the width and the height less 120 pixels 29 g.drawImage( image1, 0, 120, getWidth(), getHeight() - 120, this ); 30 31 // draw icon using its paintIcon method 32 image2.paintIcon( this, g, 180, 0 ); 33 } // end method paint 34 } // end class LoadImageAndScale
|
Lines 11 and 12 declare an Image reference and an ImageIcon reference, respectively. Class Image is an abstract classthe applet cannot create an object of class Image directly. Rather, we must call a method that causes the applet container to load and return the Image for use in the program. Class Applet (the direct superclass of JApplet) provides method getImage (line 17, in method init) that loads an Image into an applet. This version of getImage takes two argumentsthe location of the image file and the file name of the image. In the first argument, Applet method getDocumentBase returns a URL representing the location of the image on the Internet (or on your computer if the applet was loaded from your computer). Method getdocumentBase returns the location of the HTML file as an object of class URL. The second argument specifies an image file name. The two arguments together specify the unique name and path of the file being loaded (in this case, the file redflowers.png stored in the same directory as the HTML file that invoked the applet). Java supports several image formats, including Graphics Interchange Format (GIF), Joint Photographic Experts Group (JPEG) and Portable Network Graphics (PNG). File names for these types end with .gif, .jpg (or .jpeg) and .png, respectively.
Portability Tip 21.1
Class Image is an abstract classas a result, programs cannot instantiate class Image to create objects. To achieve platform independence, the Java implementation on each platform provides its own subclass of Image to store image information. |
Line 17 begins loading the image from the local computer (or downloading it from the Internet). When the image is required by the program, it is loaded in a separate thread of execution. Remember that a thread is a parallel activity, and that threads will be discussed in detail in Chapter 23, Multithreading. By using a separate thread to load an image, the program can continue execution while the image loads. [Note: If the requested file is not available, method getImage does not throw an exception. An Image object is returned, but when this Image is displayed using method drawImage, nothing will be displayed.]
Class ImageIcon is not an abstract classa program can create an ImageIcon object. At line 18, in method init creates an ImageIcon object that loads yellowflowers.png. Class ImageIcon provides several constructors that enable programs to initialize ImageIcon objects with images from the local computer or stored on the Internet.
The applet's paint method (lines 2233) displays the images. Line 26 uses Graphics method drawImage to display an Image. Method drawImage accepts four arguments. The first is a reference to the Image object to display (image1). The second and third are the x-and y-coordinates at which to display the image on the appletthe coordinates specify the location of the upper-left corner of the image. The last argument is a reference to an ImageObserveran interface implemented by class Component. Since class JApplet indirectly extends Component, all JApplets are ImageObservers. This argument is important when displaying large images that require a long time to download from the Internet. It is possible that a program will attempt to display the image before it has downloaded completely. The ImageObserver receives notifications as the Image is loaded and updates the image on the screen if the image was not complete when it was displayed. When executing this applet, watch carefully as pieces of the image display while the image loads. [Note: On faster computers, you might not notice this effect.]
Line 29 uses another version of Graphics method drawImage to output a scaled version of the image. The fourth and fifth arguments specify the width and height of the image for display purposes. Method drawImage scales the image to fit the specified width and height. In this example, the fourth argument indicates that the width of the scaled image should be the width of the applet, and the fifth argument indicates that the height should be 120 pixels less than the height of the applet. The width and height of the applet are determined by calling methods getWidth and getHeight (inherited from class Component).
Line 32 uses ImageIcon method paintIcon to display the image. The method requires four argumentsa reference to the Component on which to display the image, a reference to the Graphics object that will render the image, the x-coordinate of the upper-left corner of the image and the y-coordinate of the upper-left corner of the image.