Java Reference
In-Depth Information
storing information about images. As might be expected, this platform-dependent
subclass is inaccessible by application programmers.) Method getImage of class
Applet is used to download images. This method returns a reference to an Image
object and takes two arguments:
the URL of the image's location;
the fi le name of the image.
If the image fi le is in the same directory as the applet's HTML fi le, then method
getDocumentBase (of class Applet ) can conveniently provide the required URL
without infringing any security restrictions. For example:
Image image = getImage(getDocumentBase(),"pic.gif");
Method getImage uses a separate thread of execution, allowing the program to
continue while the image is being loaded.
In order to display the image on the applet once it has been downloaded, we use
the drawImage method of class Graphics . This method takes four arguments:
￿ a reference to the image;
￿ the x-coordinate of the upper-left corner of the image;
￿ the y-coordinate of the upper left corner of the image;
￿ a reference to an ImageObserver .
The last argument specifi es an object upon which the image is to be displayed
(usually = this , for the current applet). An ImageObserver is any object that imple-
ments the ImageObserver interface. Since this interface is implemented by class
Component , one of Applet 's (and JApplet 's) indirect superclasses, we do not need to
specify ' implements ImageObserver' for our applets.
Example (Pre-Swing)
This example simply loads and displays an image that is located in the same folder
on the Web server as that holding the applet's associated HTML fi le.
import java.awt.*;
import java.applet.*;
public class ImageTest1a extends Applet
private Image image;
public void init()
image =
public void paint(Graphics g)
Search WWH ::

Custom Search