Java Graphics  «Prev  Next»

Lesson 7Drawing images
ObjectiveDraw images.

Drawing Java Images using the legacy AWT

Drawing images in Java is very straightforward. To draw an image, you use one of the drawImage() methods defined in the Graphics class. Following is the simplest of the drawImage() methods:

Java Graphics AWT
  1. The first three parameters to drawImage() specify the Image object to be drawn, along with its XY position. The image is drawn with its upper left corner at this position.
  2. The last parameter, observer, is a special object used to keep track of the image as it is being loaded.
Draw Images

The Applet class implements the ImageObserver interface, so you can pass it along as the last parameter, as the following example demonstrates:

public void paint(Graphics g) {
  g.drawImage(img, 20, 20, this);
}

Draw Images Exercise

Click the Exercise link to load and draw images.
Draw Images Exercise

Slideshow Applet - Exercise

Once you mastered loading and drawing images, one will be ready to start building your interactive slide show applet.
Slideshow Applet - Exercise