/*
 * @(#) 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.*;

   public class ImagePanel extends JComponent {
      Graphics2D offScrGc;
      BufferedImage image, offScrImage;
      int imageWidth = 0;
      int imageHeight = 0;
      int viewerWid =400;
      int viewerHt =300;
      int  offsetX = 0, offsetY = 0;
      AffineTransform atx = new AffineTransform();
      Shape currentShape;
      public final static int ORIG =1;
      public final static int TO_FIT =2;
      int dispMode = TO_FIT;
      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 setDisplayMode(int mode){
         dispMode = mode;
      }

      public BufferedImage getImage(){
         return image;
      }

      public BufferedImage getDisplayImage(){
         return offScrImage;
      }

      public AffineTransform getTransform(){
         return atx;
      }

      public BufferedImage getSubImage(Shape shape){

         try {
            AffineTransform invtx = atx.createInverse();
            Rectangle currentShape = (invtx.createTransformedShape(shape)).getBounds();
            return image.getSubimage((int)currentShape.getX(),
                                                        (int)currentShape.getY(),
                                                        (int)currentShape.getWidth(),
                                                        (int)currentShape.getHeight());

         }catch(Exception e) {
           e.printStackTrace();

         return null;}
      }
 
      public void drawShape(Shape s){
         System.out.println(s.getBounds().width+ "   "+
                           s.getBounds().height+ "  "+ s.getBounds().x + "  " +
                           s.getBounds().y);
         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 setMask(Point[] points, int maskValue){
        WritableRaster ra = offScrImage.getRaster();
        int num =  ra.getNumBands();
        int mask[] = new int[num];
        for(int i=0;i<num;i++) mask[i] = maskValue;
        for(int i=0; i<points.length;i++){
            ra.setPixel(points[i].x, points[i].y,mask);
        }
        repaint();
     }
      public Graphics2D getDisplayedImageGC() {
         return  offScrImage.createGraphics();
      }

      public BufferedImage getDisplayedImage() {
         return offScrImage;
      }

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