Java Reference
In-Depth Information
Figure 14.15
ContextDemo applet displays the document entered in the text field.
Displaying Images
An applet can display images of the format GIF, JPEG, BMP, and others. To
display an image within the applet, you use the drawImage() method found in
the java.awt.Graphics class. This is done using the Graphics object passed in to
the applet's paint() method. The Graphics class has six overloaded versions,
which take in a variation of the following parameters:
Image image. The java.awt.Image object to be displayed. This is obtained
using the getImage() method of the applet's context.
int x and int y. The (x,y) coordinate of the upper-left corner of the image.
int width and int height. The width and height of the rectangular region
that the image is to be displayed in.
Color color. The background color of the image. This only shows through
if the image does not fill the rectangular region or is opaque in places.
ImageObserver observer. An optional parameter that allows a separate
object to be notified of changes to the Image object.
The image passed in to the drawImage() method must be of type
java.awt.Image. You do not instantiate the Image object, though. Instead, you
obtain the Image instance by invoking the getImage() method of the Applet-
Context interface:
public Image getImage(URL url)
The getImage() method associates an Image object with the given URL. The
following ImageDemo applet demonstrates displaying a JPEG image within
an applet. Notice that the Image is a field in the class, initialized in the init()
method from an applet parameter named image. It is then drawn onscreen in
Search WWH ::




Custom Search