/** CompositeCanvasJAI Class
   * @version 1.0  18 apr 2000
   * @author Lawrence Rodrigues
   **/
package com.vistech.jai.render;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.awt.geom.*;
import java.awt.image.renderable.*;
import javax.swing.*;
import javax.media.jai.*;
import com.vistech.imageviewer.*;
import com.vistech.util.*;

/** A canvas class that allows drawing graphics over images.
  * @version 1.0 18 Apr 2000
  * @author Lawrence Rodrigues
  **/
public class CompositeCanvasJAI extends ImageManipulatorCanvas
                   implements DrawingCanvas{
   protected BufferedImage offScrImage;
   protected Graphics2D offScrGc;
   protected BufferedImage destImage;
   protected Graphics2D destGc;
   protected boolean drawOn = false;

   public CompositeCanvasJAI() { createOffScreenImage();}

   public Graphics2D getDisplayedImageGC(){return offScrGc;}

   public void setImage(PlanarImage image){
      if(image instanceof TiledImage){
      panX =0; panY =0;
      displayImage = (PlanarImage)image;
      sampleModel = displayImage.getSampleModel();
      colorModel = displayImage.getColorModel();
      getTileInfo(displayImage);
      fireTilePropertyChange();
      imageDrawn = false;
      repaint();
      }
      else super.setImage(image);
   }

   public void paintImage(PlanarImage image) {
      setImage(image);
      applyTransform(image, atx);
   }

   public boolean getDrawOn(){ return drawOn;}
   public void setDrawOn(boolean onOrOff){ drawOn = onOrOff;}

   public void createOffScreenImage() {
      Rectangle r = getBounds();
      if((r.width ==0) || (r.height ==0)){  return;}
      offScrImage = new BufferedImage(r.width,r.height,BufferedImage.TYPE_INT_RGB);
      offScrGc = offScrImage.createGraphics();
      destImage = new BufferedImage(r.width,r.height,BufferedImage.TYPE_INT_RGB);
      destGc = destImage.createGraphics();
  }

   public void paintComponent(Graphics gc){
      Graphics2D g = (Graphics2D)gc;
      Rectangle rect = this.getBounds();
      if((viewerWidth != rect.width) || (viewerHeight != rect.height)){
         viewerWidth = rect.width;
         viewerHeight = rect.height;
         createOffScreenImage();
      }
      g.setColor(Color.black);
      g.fillRect(0,0,viewerWidth, viewerHeight);
      if(displayImage == null) return;
      g.setColor(Color.black);
      g.fillRect(0,0,rect.width, rect.height);
      if(displayImage != null)
         g.drawRenderedImage(displayImage, new AffineTransform());
      if(drawOn) {
         g.setColor(Color.white);
         g.setXORMode(Color.black);
         if(offScrImage != null) g.drawImage(offScrImage, 0,0, this);
      }
      imageDrawn = true;
   }
   public void eraseShapes() {
      offScrImage.flush();
      if(image != null) setImage(image);
      createOffScreenImage();
      repaint();
   }
   public void reset(){
      super.reset();
      if(image != null) setImage(image);
      createOffScreenImage();
      repaint();
   }
}
