/*
 * @(#) ImageCanvas.java 1.0 99/9/18
 * Copyright (c) 1999 Lawrence Rodrigues
 */
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.lang.Math;

 /** ImageCanvas Class
   * @version 1.0  18 sep 1999
   * @author Lawrence Rodrigues
   * The ImageCanvas class provides the basic functionality to draw images.
   **/

  public class ImageCanvas extends Canvas implements ImageLoadedListener{
     public int width, height;
     protected Image offScrImage;
     protected Graphics offScrGc;
     protected Image pixImage;
     private ImageBuffer scnImage;

     /** ImageCanvas Constructor
       * @param wd the width of the ImageCanvas
       * @param ht the height of the ImageCanvas
       */
     public  ImageCanvas(int wid, int ht){
        width = wid;
        height = ht;
        setSize(wid, ht);
     }

     /** Sets width and height
       * @param wd the width of the ImageCanvas
       * @param ht the *
       **/
     public void setWidHt(int wd, int hgt){
        width  = wd; height = hgt;
     }

     /** Creates OffScreen image. The ImageCanvas has to be visible
       * for the offscreen image to be visible.
       **/
     public  void createOffScrImage(){
        Rectangle r = getBounds();
        if((r.width ==0) || (r.height ==0)){
                return;
        }
        setWidHt(r.width, r.height);
        //If the component is visible, offScreenImage may return null;
        if((offScrImage = createImage(width, height)) == null){
        }
        setOffScrGc();
     }

     /** Sets off screen Graphics. The ImageCanvas has to be visible
       * for the off screen screen to be available
       **/
     public  void setOffScrGc(){
        if(offScrImage == null) return;
        offScrGc = offScrImage.getGraphics();
        //Default Attributes
        offScrGc.setColor(Color.cyan);
        Font fnt = new Font("Helvetica", Font.BOLD,9);
        offScrGc.setFont(fnt);
        offScrGc.setColor(Color.black);
        offScrGc.fillRect(0,0, width, height);
     }

     /** Sets ImageBuffer.
       * @param im the ImageBuffer.
       */
     public void setImageBuffer(ImageBuffer ib){
        scnImage = ib;
        pixImage = ib.image;
     }

     /** @return the ImageBuffer.
       */
     public ImageBuffer getImageBuffer() {
        return scnImage;
     }

     /** Sets the image
       * @param im the image.
       */
     public void setImage(Image im) {
        pixImage = im;
        imagePaint();
     }

     /** @return the ImageBuffer.
       */
     public Image getImage() {
        return pixImage;
     }

     /** Called by the ImageBuffer object whenever image related
       * status information is available.
       * @param img the ImageBuffer.
       * @param action the status.
       */
     public void imageLoaded(ImageLoadedEvent e){
        ImageLoader il = (ImageLoader)e.getSource();
        int loadStatus = il.getImageBuffer().getLoadStatus();
         if(loadStatus == ImageLoadedListener.IMAGE_READY){
            pixImage = il.getImageBuffer().getImage();
            imagePaint(pixImage);
         }
     }

     /**
       * Paints an image on the ImageCanvas.
       * It is the responsibilty of the caller to check whether
       * the image is loaded.
       * @param img the image to be painted
       **/
     public  boolean imagePaint(Image img){
        if(img == null) return false;
        pixImage = img;
        drawOffScreen(img);
        return true;
     }
     /**
      * Paints an image on the ImageCanvas.
      * It is the responsibilty of the caller to check whether
      * the image is loaded.
      */
     public  boolean imagePaint(){
        return imagePaint(pixImage);
     }

     /**
      * Overrides the Canvas paint method.
      * Not expected to be called directly.
      * Called by the system whenever the visibility status
      * of the ImageCanvas changes.
      **/
     public void paint(Graphics paintGc){
        Rectangle  border = getBounds();
        width = border.width;
        height = border.height;
        paintGc.setColor(Color.cyan);
        if(offScrImage != null){
           paintGc.drawImage(offScrImage,0,0,width,height, this);
        }
        repaint();
     }
     /**
      * Overrides Component update method.
      * The subclass can override this method. Not expected
      * to be called directly. Use repaint(..) to call this method.
      **/
     public  void update(Graphics paintGc){
        Rectangle  border = getBounds();
        width = border.width;
        height = border.height;

	  paintGc.setColor(Color.cyan);
  	  if(offScrImage != null){
           paintGc.drawImage(offScrImage,0,0,width,height, this);
        }
        paintGc.setColor(Color.white);
        paintGc.drawRect(1,1,width-2, height-2);
     }

     /** Draws an image off screen
      * @param img the to  be drawn off screen.
      **/
     protected void drawOffScreen(Image img){
        if(img == null) return;
        Rectangle r = getBounds();
        if((r.width <=0) ||(r.height <=0)){
            System.out.println("Component not visible. Will draw image later");
            return;
        }
        if((width != r.width) ||(height != r.height))createOffScrImage();
        if(offScrImage == null ) createOffScrImage();
        if(offScrGc == null)setOffScrGc();
        offScrGc.setColor((Color.black));
        offScrGc.setFont(new Font("Helvetica", Font.BOLD, 9));
        offScrGc.fillRect(0,0,width,height);
        if(offScrGc.drawImage(img,0 ,0, width,height,this)){
           if(scnImage.imageName !=null){
              offScrGc.setColor(Color.white);
              offScrGc.drawString(scnImage.imageName,12,12);
           }
           repaint();
        }
        else {
           offScrGc.setColor(Color.white);
           if(scnImage.imageName !=null)
              offScrGc.drawString(scnImage.imageName,12,12);
        }
        repaint();
     }


     public void cineDraw(ImageBuffer img){
        setImageBuffer(img);
        imagePaint();
     }

     public  void cineImagePaint(Image img){
        pixImage = img;
        drawOffScreen(img);
     }
     public void resetDisplay(){


    }
 }












































