Overriding imageUpdate()

The default imageUpdate() method in java.awt.Component repaints the component when the image has changed. However, you can override it to make it do something different. For example, the following applet prints a message in the status bar of the browser as the image loads.


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


public class ImageStatus extends Applet implements ImageObserver {

  private Image picture;
  
  public void init() {
  
    String filename = this.getParameter("imagefile");
    if (filename != null) {
      this.picture = this.getImage(this.getDocumentBase(), filename);
    }
  
  }
  
  
  public void paint(Graphics g) {
   
    if (this.picture != null) {
      g.drawImage(this.picture, 0, 0, this);
    }
    else {
      g.drawString("Missing Picture", 20, 20);
    }
    
  }
  
  public boolean imageUpdate(Image img, int infoflags, int x, int y, 
   int width, int height) {
   
     if ((infoflags & ImageObserver.ALLBITS) != 0) {
       showStatus("Image Complete");
       this.repaint();
       return false;
     }
     else {
       showStatus("x: " + x + " y: " + y + " width: " + width 
        + " height: " + height);
       this.repaint();
     }
    
     return true;
  
  } 
  
}

Previous | Next | Top
Last Modified April 24, 1998
Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu