Drawing Images at Actual Size

Once the image is loaded draw it in the paint() method using the drawImage() method like this

g.drawImage(img, x, y, io)

img is a member of the Image class which you should have already loaded in your init() method. x is the x coordinate of the upper left hand corner of the image. y is the y coordinate of the upper left hand corner of the image. io is a member of a class which implements the ImageObserver interface. The ImageObserver interface is how Java handles the asynchronous updating of an Image when it's loaded from a remote web site rather than directly from the hard drive. java.applet.Applet 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 may look like this

public void paint(Graphics g) {
  g.drawImage(img, 0, 0, this);
}
This draws the image at the actual size of the picture.
Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified June 15, 1998