HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Using the Screen Image Class to Simplify
Your Programming for Image Loading Synchronization"
Vol. 1, Issue 3, P.54

	

Listing 1: The imageUpdate() method.

public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height){
           if((infoflags & ImageObserver.ERROR) != 0){
                // Error  handling code.
               return false;
           }
        if((infoflags & (ImageObserver.WIDTH | ImageObserver.HEIGHT))!= 0){
             //Invoke methods that needs width and Height
          }
        if((infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS))!= 0)  {
            //Invoke methods that are waiting for image to be completely loaded.
       }
        return true;
  }

Listing 2: The grabPixels() method

        try{
            if(pg.grabPixels() != true){
              // Pixels not grabbed.
             }
          } catch (InterruptedException e){
             // Handle Exception 
        }

Listing 3. ScreenImage Class

/*
 *  @# ScreenImage.java 1.0 96/9/20
 *  Copyright (c) 1996 Lawrence Rodrigues
 */
import java.io.*;
import java.awt.*;
import java.util.*;
import java.net.URL;
import java.awt.image.*;

 public class  ScreenImage extends Object implements ImageObserver{
       static  public final int EMPTY = 0;
       static  public final int ABORTED = 1;
       static  public final int ERRORED = 2;
       static  public final int LOAD_REQUESTED = 4;
       static  public final int HT_WID_KNOWN = 8;
       static  public final int IMAGE_READY = 16;
       static  public final int PIXELS_AVAILABLE = 32;
       public Image  image;
       public int width = -1, height = -1;
       public int origPixels[];
       public ColorModel cm;
       public URL url= null;
       public String fileName = null;
       public String dir;
       public String imageName;
       public int imageId;
       public int loadStatus = EMPTY;
       private Vector clientObjs = new Vector();
       private Callback  ClientObj;
  public ScreenImage(){
         loadStatus |= EMPTY;
  }
  public void setWidHt(int wd,  int ht){
         width = wd; height = ht;
         loadStatus |= HT_WID_KNOWN;
  }
  public void setParams(int rwIm[], int wd, int ht, ColorModel colCm){
         width = wd; height = ht;
         cm = colCm;
         origPixels = rwIm;
         loadStatus |= (LOAD_REQUESTED | HT_WID_KNOWN| PIXELS_AVAILABLE);
   }
   public void setFileInfo(String path, String name, int id){
          if(path == null || name == null) return;
          dir = path;
          imageName = name;
          imageId = id;
          fileName = new String(dir+File.separatorChar+imageName);
          url = null;
   }
  public  void setUrlInfo(URL ul, String name, int id){
          if(ul == null || name == null) return;
          url = ul;
          imageName = name;
          imageId = id;
          fileName = null;
   }
  public  boolean requestImageLoad(){
          if((url == null) && (dir == null)) return false;
          if(url != null) image = Toolkit.getDefaultToolkit().getImage(url);
          if(fileName != null) image = Toolkit.getDefaultToolkit().getImage(fileName);
          if(image == null) return false;
          return true;
  }
  public  boolean fetchWidHt(){
          if(image == null) return false;
          int wid = image.getWidth(this);
          int ht = image.getHeight(this);
          if((wid == -1) || (ht == -1)) return false;
          setWidHt(wid, ht);
          return true;
  }
  public  void load(){
          if(loadStatus == EMPTY){
             if(!requestImageLoad()) return ;
          }
          fetchWidHt();
  }
  public boolean fetchPixels(){
         if((width  <0) || (height <0)){
             load();
             return false;
         }
         int pixMap[] = new int[width*height];
         PixelGrabber pg = new PixelGrabber(image, 
						     0,0,width,height, pixMap, 0, width);
         try {
	       pg.grabPixels();
          } catch (InterruptedException e){return false;}
         if((pg.status()  & ImageObserver.ABORT)!=0){
             notifyClientObj(ERRORED);
             return false;
         }
         setParams(pixMap, width, height, cm);
         notifyClientObj(PIXELS_AVAILABLE);
         return true;
     }
  /** 
    * Prepares the image to be constructed with its original 
						width & height
    */ 
  public boolean prepareImage(){
         return prepareImage(width, height);
  }

  /** 
    * Prepares the image to be constructed with a given width and 
						height.
    */ 
  public boolean prepareImage(int wid, int ht ){
         if(image == null) return false;
         if(Toolkit.getDefaultToolkit().prepareImage(image, wid, 
						  ht, this)){
            loadStatus |= IMAGE_READY;
         }
         return true; 
  }
    /**
      * Implements ImageObserver interface. Called at regular 
						  intervals when
      * image related operations are taking place. This methods 
						  notifies the
      * clients when a certain status becomes avaialale.
      */
  public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height){
         if((infoflags & ImageObserver.ERROR) != 0){
             notifyClientObj(ERRORED);
             return false;
         }
         if((infoflags & (ImageObserver.WIDTH | ImageObserver.HEIGHT))!= 0){
             setWidHt(width, height);
             notifyClientObj(HT_WID_KNOWN);
         }
        if((infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS))!= 0)           {
            loadStatus |= IMAGE_READY;
            notifyClientObj(IMAGE_READY);
            return false;
          }
        return true;
   }
   /**
     * A callback  object can register itself here. It gets added 
						 to the callback list.
     */
   public void registerForCallback(Callback cbObj){
          clientObjs.addElement(cbObj);
   }
   /**
     * Callback object can remove itself from the callback list.
     */
   public void unregisterCallback(Callback cbObj){
          clientObjs.removeElement(cbObj);
   }
   /**
     * Notifies all the registered client objects.
     */
   public void  notifyClientObj(int status){
          for (Enumeration e = clientObjs.elements() ; e.hasMoreElements() ;) {
               ((Callback)(e.nextElement())).performCallback(this, status);
          }
   }
}

Listing 4:  Callback  Interface

    public interface Callback{
           public void performCallback(ScreenImage img, int action);
    }

Listing 5: The loadAnImage() method

private boolean loadAnImage(int num){
           ScreenImage im = new ScreenImage();
           if(im == null) return false;
           imageTable.put(Integer.toString(num), im);
           im.setFileInfo(path, fileList[num], num);
           im.load();
           return true;
   }

Listing 6: The displayImage() method

ImageCanvas CurScreen = new ImageCanvas(width, height);

public void displayImage(int num){
               ScreenImage img = (ScreenImage)imageTable.get(Integer.toString(num));
            if(img == null){
               if(!loadAnImage(num)){
                    return;
                 }
           curScreen.setScreenImage(img);
           curScreen.imagePaint();
}

Listing 7: The setScreenImage() method

public void setScreenImage(ScreenImage im){
             if(scnImage != null){
                scnImage.unregisterCallback(this);
                }
             scnImage = im;
             scnImage.registerForCallback(this);
             pixImage = im.image;
        }

Listing 8: The imagePaint() methods

   public  boolean imagePaint(Image img){
            if(img == null) return false;
            pixImage = img;
            drawOffScreen(img);
            return true;
    }
   public  boolean imagePaint(){
            return imagePaint(pixImage);
   }

Listing 9: The ScreenImage() method

 public void performCallback(ScreenImage img, int action){
             if(action == ScreenImage.IMAGE_READY){
                imagePaint(pixImage);
             }
  }

Listing 10: The loadPixels() method

   public void loadPixels(int id){
           ScreenImage im;
           im = (ScreenImage)imageTable.get(Integer.toString(id));
           if(im== null){
              //Not loaded
              im = new ScreenImage();
              imageTable.put(Integer.toString(id), im);
              im.registerForCallback(this); 
              im.setFileInfo(path, fileList[id], id);
              im.load();
               try{
                   mainThread.sleep(100);
              }
              im.fetchPixels();
          }
          else {
              //Already loaded.
               im.registerForCallback(this); 
               im.fetchPixels();
           }
     }

Listing 11: The ScreenImage() method

public void performCallback(ScreenImage img, int status){
           if(status == ScreenImage.PIXELS_AVAILABLE){
              generateAndDisplayStats();
         }

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.