Example of an applet displaying
an image
You may recall the Zener card images from a
previous unit. Consider this output from a simple applet:
The code for this applet is as follows:
import
java.applet.Applet;
import java.awt.*;
public class Drawimage1 extends
Applet
{
public void paint( Graphics
g )
{
// retrieve copy
of image and store in 'image'
image image =
getimage( getCodeBase(), "zenerwaves.gif" );
// draw image in
applet window
g.drawimage(image, 0, 0, this);
}
} // class
In the applet above the 2-argument version of
the method getimage() has been used. Rather than getCodeBase(), we could have
specified any URL for the directory where the image file is located. The message getCodeBase()
will result in the applet object replying with the directory in which the "
.class" object file is located.
Note that the image zenerwaves.gif is a GIF file that must be in the same directory as the
compiled applet ‘.class’ file, in order for this applet for successfully load
and display the image.
Back
to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|