Using the Return Value of drawImage()

The drawImage() methods returns a boolean. Many people don't realize this, since the return value of drawImage() is often ignored. However, that boolean tells you whether or not the image was drawn successfully. If it was, drawImage() returns true. Otherwise drawImage() returns false.

For example, the following applet loads an image from the net. Until the image is finished loading, the applet displays the words "Loading Picture. Please hang on." Once the image has fully loaded, the applet displays it.

import java.awt.*;
import java.applet.*;
import java.awt.image.*;

public class ImageDisplay extends Applet {

  Image picture;
  
  public void init() {

    this.picture = this.getImage(this.getDocumentBase(), "sun.gif");
  
  }
  
  
  public void paint(Graphics g) {
   
     if(!g.drawImage(this.picture, 0, 0, this)) {
       g.drawString("Loading Picture. Please hang on", 25, 50);
     }
     
  }
    
}

Previous | Next | Top
Last Modified April 10, 2000
Copyright 1997, 1999, 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu