/*
 * @(#) ImagePanel.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
package com.vistech.imagedata;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import com.vistech.imageviewer.*;

public class ImagePanel extends ImageCanvas2D implements DrawingCanvas{
   public final static int ORIG =1;
   public final static int TO_FIT =2;
   protected BufferedImage image, offScrImage;
   protected Graphics2D offScrGc;
   protected int imageWidth = 0, imageHeight = 0;
   protected int viewerWid =400, viewerHt =300;
   protected int  offsetX = 0, offsetY = 0;
   protected AffineTransform atx = new AffineTransform();
   protected Shape currentShape = null;
   protected int dispMode = TO_FIT;
   protected Point roiPoints[];

   public ImagePanel(BufferedImage img, int wid, int ht){
      image = img;
      viewerWid = wid;
      viewerHt = ht;
      setSize(wid,ht);
      createOffScreenImage();
   }

   protected void createOffScreenImage() {
      imageWidth = image.getWidth();
      imageHeight = image.getHeight();
      double magx = viewerWid/(double)imageWidth;
      double magy =  viewerHt/(double)imageHeight;
      if(dispMode == TO_FIT) atx.setToScale(magx,magy );
      else atx =  new AffineTransform();
      AffineTransformOp atop = new AffineTransformOp(atx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
      offScrImage = atop.createCompatibleDestImage(image, image.getColorModel());// new BufferedImage(viewerWid, viewerHt, BufferedImage.TYPE_INT_RGB);
      offScrImage = atop.filter(image, offScrImage);
      offScrGc = offScrImage.createGraphics();
   }

   public void setImage(BufferedImage img){
      if(img == null) return;
      image = img;
      createOffScreenImage();
      repaint();
   }

   public void setDisplayImage(BufferedImage img){
      offScrImage = img;
      repaint();
   }

   public void setDisplayMode(int mode){ dispMode = mode;}
   public BufferedImage getImage(){return image;}
   public BufferedImage getDisplayImage(){ return offScrImage;}

   public void setTransform( AffineTransform atx){ this.atx = atx;}
   public AffineTransform getTransform(){return atx;}

   public void drawShape(Shape s){
      currentShape = s;
      offScrGc = offScrImage.createGraphics();
      offScrGc.setColor(Color.red);
      offScrGc.draw(s);
      repaint();
   }

   public void fillShape(Shape s) {
      offScrGc = offScrImage.createGraphics();
      offScrGc.setColor(Color.red);
      offScrGc.fill(s);
      repaint();
   }

   public void eraseShapes() {
      reset();
   }

   public Graphics2D getDisplayedImageGC() {
      return offScrImage.createGraphics();
   }

   public BufferedImage getDisplayedImage() {return offScrImage;}

   public void reset() {
      createOffScreenImage();
      repaint();
   }

   public void paintComponent(Graphics gc){
      Graphics2D g = (Graphics2D)gc;
      Rectangle rect = this.getBounds();
      if(currentShape != null) g.setClip(currentShape);
      g.setColor(Color.black);
      if((viewerWid != rect.width) || (viewerHt != rect.height)){
          viewerWid = rect.width;
          viewerHt = rect.height ;
          createOffScreenImage();
          repaint();
          return;
      }
      g.fillRect(0,0,rect.width, rect.height);
      if(offScrImage != null) g.drawImage(offScrImage,0,0, this);
     }
}
