/*
 * @(#) ImagePanel.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;

public class ImagePanel extends JComponent {
   protected  Image image,origImage;
   protected  Image offScrImage;
   protected  Graphics offScrGc;
   protected  Image brightImage =null;
   protected  int imWidth = 0, imHeight = 0;
   protected  int viewerWid, viewerHt;
   private int  offsetX = 0, offsetY = 0;
   public ImagePanel(Image img, int wid, int ht){
      origImage = img;
      image = img;
      imWidth = wid;
      imHeight = ht;
      setSize(wid,ht);
   }
     //Create off screen image only after peer is created
   public void addNotify(){
      super.addNotify();
      createOffScreenImage();
   }
    //Creates an off screen image
   public void createOffScreenImage(){
      Rectangle rect = getBounds();
      viewerWid = rect.width;
      viewerHt = rect.height;
      offScrImage = this.createImage(rect.width, rect.height);
      offScrGc = offScrImage.getGraphics();
      offScrGc.setColor(Color.black);
      offScrGc.fillRect(0,0,rect.width, rect.height);
      if(image != null) offScrGc.drawImage(image, 0,0,this);
   }

   public void setBrightness(int br) {
      BrightnessFilter filter = new BrightnessFilter(br);
      ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
      brightImage = this.createImage(ip);
      prepareImage(brightImage, this);
      repaint();
   }

   public void setImage(Image image){
      this.image = image;
      createOffScreenImage();
      prepareImage(image, this);
   }

   public void drawImage(Image image, int x, int y){
      this.image = image;
      Rectangle rect = getBounds();
      viewerWid = rect.width;
      viewerHt = rect.height;
      offScrImage = this.createImage(rect.width, rect.height);
      offScrGc = offScrImage.getGraphics();
      offScrGc.setColor(Color.black);
      offScrGc.fillRect(0,0,rect.width, rect.height);
      offsetX = x; offsetY = y;
      prepareImage(image, this);
   }

   private void drawOffScreen(Image img, int x, int y) {
      offScrGc.setColor(Color.black);
      offScrGc.fillRect(0,0,viewerWid, viewerHt);
      offScrGc.drawImage(img, x,y, this);
   }

   public Image getImage(){ return image;}

   public Graphics getDisplayedImageGC() { return offScrGc; }
   public void paintComponent(Graphics g){
      Rectangle rect = this.getBounds();
      g.setColor(Color.black);
      g.fillRect(0,0,rect.width, rect.height);
      if(offScrImage != null){
      g.drawImage(offScrImage, 0,0, this);
      }
   }
   public boolean imageUpdate(Image img,
                           int infoflags,
                           int x,
                           int y,
                           int width,
                           int height){

     if((infoflags & ImageObserver.ERROR) != 0){
        System.out.println("ERROR in image load or image draw");
        return false;
     }
     if((infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS))!= 0) {
        drawOffScreen(img, offsetX,offsetY);
        repaint();
        image = img;
        return false;
     }
     return true;
   }

   public void resetImage(){
      offsetX =0; offsetY = 0;
      setImage(origImage);
      repaint();
   }
}
