|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
The first parameter, img, takes an Image object. Often, you will get an Image object by using the getImage() method, as discussed previously. The second, x, and third, y, parameters set the position of the upper-left corner of the image in the applets window. The last parameter, observer, takes an object that implements the ImageObserver interface. This design enables your code to make decisions based on an images loading status. It is usually enough to know that the Applet class inherits the ImageObserver interface from the Component class. Just passing the applet itself (this) as the observer parameter is usually sufficient. You might use an alternative object with an ImageObserver interface if you are tracking the asynchronous loading of many images.
Basic image display is easier than it sounds. The DrawImageOne applet in Listing 39.14 displays an image residing in the same directory or folder as the applet itself. Figure 39.15 shows the output of the DrawImageOne applet.
Listing 39.14 DrawImageOne.javaRetrieve an Image from the Applets Server and Display It import java.awt.Graphics; public class DrawImageOne extends java.applet.Applet { public void paint(Graphics g) { // Load Earth image and draw it in the applets window. g.drawImage(getImage(getCodeBase(), Earth1.gif), 0, 0, this); } }
The code in Listing 39.14 is simpletoo simple. It is poor coding practice to put actions not directly related to putting something onscreen inside your applets paint() method. Every time your applets window needs updating, the paint method is called. In this case, the applet is forced to reload the image every time your applets window is refreshed. You should override the init() method to take actions such as loading images that are only done one time, at the beginning of the applets life. The init() method takes no arguments and returns void. Listing 39.15 shows a well-behaved applet that displays the same output as the applet in Listing 39.14.
Listing 39.15 DrawImageTwo.javaImprove Performance by Loading Images Only Once, in init() import java.awt.Graphics; import java.awt.Image; public class DrawImageTwo extends java.applet.Applet { private Image fImage; public void init() { // Load image. fImage = getImage(getCodeBase(), Earth1.gif); } public void paint(Graphics g) { // Draw image in the applets window. g.drawImage(fImage, 0, 0, this); } } Notice that in Listing 39.15, the image from the Earth1.gif file loads only one time, in the init() method. When the applet window is refreshed, the Image object assigned to the fImage variable is passed to the drawImage() method without reloading the image from the Earth1.gif file. Another version of the drawImage() method is similar to the one you have already seen and used, but it includes two additional parameters. These enable you to determine the size of the image displayed in an applet window. This version of the drawImage() method is as follows: public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) The width and height parameters take the width and height, in pixels, of the display area for the image regardless of the images native size. You can stretch and shrink an image by using these parameters; the actual disk size of the image remains the same.
Adding AnimationAs you develop your user interfacesparticularly user interfaces for appletsyou may find a need for animation. The previous section explored drawImage(); it is the graphical analog of drawString() that weve been using for a while. It may occur to you to put up an animation using a technique similar to that shown in Listing 39.16.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |