/* @(#) Lens.java 1.1 99/01/12
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package com.vistech.imageviewer;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

/** Implements a lens that can be interactively moved over an image drawn on a canvas.
 * The Lens class has several properties to set the attributes of the movable lens.
 * These include the lense size and the magnification factor. This class uses the Zoom class for
 * magnifying portion of displayed image covered under the lens. 
 * @version 1.0  1 Dec 1999
 * @author Lawrence Rodrigues
 **/
public class Lens implements MouseListener, MouseMotionListener{
   protected ImageManipulator imageCanvas;
   protected Dimension lensSize = new Dimension(60,80);
   protected Point prevPoint = new Point(0,0);
   protected boolean lensOn = false;
   protected int sizeIncrement = 10;
   protected double magIncrement = 0.5;
   protected double lensMag = 2.0;
   protected Zoom zoom;

   /** @param imagecanvas the component on which the image is drawn.
   */
   public Lens(ImageManipulator c){
      imageCanvas = c;
      zoom = new Zoom(imageCanvas);
   }
   public void init() { setLensOn(true);}

   public void setLensSize(Dimension d){ lensSize = d; }

   public Dimension getLensSize(){ return lensSize; }

   public void setLensMag(double mag){ lensMag = mag; }

   public double getLensMag(){ return lensMag; }

   public void setLensMagIncrement(int incr){ magIncrement = incr; }

   public double getLensMagIncrement(){ return magIncrement; }

   public void setLensSizeIncrement(int incr){ sizeIncrement = incr; }

   public int getLensSizeIncrement(){ return sizeIncrement; }

   public  void setLensOn(boolean onOff){ lensOn = onOff; }

   public  boolean getLensOn(){ return lensOn; }

   /** Draws an oval-shaped lens.
     * @param the x coordinate of the lens center.
     * @param the y coordinate of the lens center.
     */
   public void drawLens(int x, int y){
      int wid = lensSize.width; int ht = lensSize.height;
      Shape len = new Ellipse2D.Float(prevPoint.x-wid,prevPoint.y-ht, wid,ht);
      imageCanvas.setClip(len);
      zoom.magnify(0,0, 1);
      Shape ch = new Ellipse2D.Float(x-wid,y-ht, wid,ht);
      imageCanvas.setClip(ch);
      zoom.magnify((x-wid/2), (y-ht/2), lensMag);

      imageCanvas.draw(ch);
      prevPoint = new Point(x,y);
   }

   public void incrementLensSize(){
      lensSize.width += sizeIncrement;
      lensSize.height += sizeIncrement;
   }
   public void decrementLensSize(){
      lensSize.width -= sizeIncrement;
      lensSize.height -= sizeIncrement;
   }

   public void incrementLensMag(){
      lensMag += magIncrement;
   }

   public void decrementLensMag(){
       lensMag -= magIncrement;
   }

   public void reset() { setLensOn(false); }

   public void mousePressed(MouseEvent e) {
      if(!lensOn) return;
      if(SwingUtilities.isLeftMouseButton(e)){
          drawLens(e.getX(), e.getY());
       }
   }
   public void mouseClicked(MouseEvent e){}
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e){}

   public void mouseReleased(MouseEvent e) {
      if(!lensOn) return;
      imageCanvas.setClip(null);
      zoom.magnify(0,0,1);
   }

   public void mouseDragged(MouseEvent e){
      if(SwingUtilities.isLeftMouseButton(e)){
         if(lensOn) drawLens(e.getX(), e.getY());
      }
   }
   public void mouseMoved(MouseEvent e){}
}
