home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 applet’s window. The last parameter, observer, takes an object that implements the ImageObserver interface. This design enables your code to make decisions based on an image’s 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.


TROUBLESHOOTING
The image doesn’t load into the applet. What do you do?
Make sure that the image is in the proper folder—either in the same folder as the Java code or in the same folder as the HTML document, depending on the method your program uses. See the section, “Loading Images over the Web,” later in this chapter if the problem is due to slow or faulty network connections.

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.


NOTE:  Place the Earth1.gif image file into the same folder as the DrawImageOne.class file before you try to run the DrawImageOne applet.

Listing 39.14 DrawImageOne.javaRetrieve an Image from the Applet’s 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 applet’s window.
     g.drawImage(getImage(getCodeBase(), “Earth1.gif”), 0, 0, this);
   }
}


FIGURE 39.15  You can draw any JPG or GIF image in a Java applet window by using the drawImage() method.

The code in Listing 39.14 is simple—too simple. It is poor coding practice to put actions not directly related to putting something onscreen inside your applet’s paint() method. Every time your applet’s window needs updating, the paint method is called. In this case, the applet is forced to reload the image every time your applet’s 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 applet’s 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.


NOTE:  Place the Earth1.gif image file into the same folder as the DrawImageTwo.class file before you try to run the DrawImageTwo applet.

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 applet’s 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 image’s native size. You can stretch and shrink an image by using these parameters; the actual disk size of the image remains the same.


CAUTION:  

Changing the size of the image at runtime can degrade the quality of your image. For best results, use your graphics tools to scale the image to the desired size, and don’t attempt to use Java for further scaling.


Adding Animation

As you develop your user interfaces—particularly user interfaces for applets—you may find a need for animation. The previous section explored drawImage(); it is the graphical analog of drawString() that we’ve 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.


Previous Table of Contents Next


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.