Drawing Images

Once you've downloaded an image from a URL, it's quite simple to draw it. Once the image is loaded draw it in the paint() method using the drawImage() method of the Graphics class

public boolean drawImage(Image img, int x, int y, ImageObserver io) 
img is a member of the Image class which you should have already loaded. x and y are the coordinates of the point where the upper left hand corner of the Image will be drawn. io is an instance of a class which implements the ImageObserver interface.

The ImageObserver interface is how Java handles the asynchronous updating of an Image. java.awt.Component implements ImageObserver so for now just pass the keyword this to drawImage to indicate that the current applet is the ImageObserver that should be used.

A paint() method that does nothing more than draw an Image starting at the upper left hand corner of the applet might look like this

public void paint(Graphics g) {
  g.drawImage(img, 0, 0, this);
}
Presumably, img is a field of the object which was initialized elsewhere.


Previous | Next | Top
Last Modified March 26, 1997
Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu