
/**
 * PumpProbeImagePanel.java
 * A simple plugin for generating more custormized data plots from X.Y data
 * Created: July 17, 2005
 *
 * @author Nathan Stevens
 * @version 0.1 7-18-2005
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PumpProbeImagePanel extends JPanel {
  private Rectangle currentRect = null;
  private Rectangle rectToDraw = null;
  private Rectangle previousRectDrawn = new Rectangle();
  private Image image;
  private PumpProbeImageDialog pumpProbeImageDialog;
  private int w;
  private int h;
  private int[] minmax = null; // location of minimum and maximum points
  
  // default constructor
  public PumpProbeImagePanel() { }
  
  public PumpProbeImagePanel(PumpProbeImageDialog pumpProbeImageDialog,
  Image image, int w, int h, int[] minmax) {
    this.pumpProbeImageDialog = pumpProbeImageDialog;
    this.image = image;
    this.w = w;
    this.h = h;
    this.minmax = minmax;
    
    // set the preferred size to the size of the image
    setPreferredSize(new Dimension(this.w, this.h));
    
    MyListener myListener = new MyListener();
    addMouseListener(myListener);
    addMouseMotionListener(myListener);
  }
  
  class MyListener extends MouseInputAdapter {
    public void mousePressed(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();
      currentRect = new Rectangle(x, y, 0, 0);
      updateDrawableRect(getWidth(), getHeight());
      repaint();
    }
    
    public void mouseDragged(MouseEvent e) {
      updateSize(e);
    }
    
    public void mouseReleased(MouseEvent e) {
      updateSize(e);
    }
    
    /*
    * Update the size of the current rectangle
    * and call repaint.  Because currentRect
    * always has the same origin, translate it
    * if the width or height is negative.
    *
    * For efficiency (though
    * that isn't an issue for this program),
    * specify the painting region using arguments
    * to the repaint() call.
    *
    */
    public void updateSize(MouseEvent e) {
      int x = e.getX();
      int y = e.getY();
      currentRect.setSize(x - currentRect.x, y - currentRect.y);
      updateDrawableRect(getWidth(), getHeight());
      Rectangle totalRepaint = rectToDraw.union(previousRectDrawn);
      repaint(totalRepaint.x, totalRepaint.y,
      totalRepaint.width, totalRepaint.height);
    }
  }
  
  public void paintComponent(Graphics g) {
    super.paintComponent(g); //paint background
    
    //Draw image at half it's natural size
    g.drawImage(image, 0, 0, w, h, this);
    
    //If currentRect exists, paint a box on top.
    if (currentRect != null) {
      //Draw a rectangle on top of the image.
	    g.setColor(Color.white);
      g.drawRect(rectToDraw.x, rectToDraw.y,
      rectToDraw.width - 1, rectToDraw.height - 1);
    }
    
    // tell the streak view dialog what was drawn
    if(rectToDraw != null) {
      pumpProbeImageDialog.updateROI(rectToDraw);
    }
    
     // mark the location of the min and maximum intensity
     int x1 = 0;
     int y1 = 0;
     int x2 = 0;
     int y2 = 0;
     g.setColor(Color.green);
     
     // the minimum line
     x1 = minmax[0];
     y1 = minmax[1] - 5;
     x2 = minmax[0];
     y2 = minmax[1] + 5;
     g.drawLine(x1, y1, x2, y2);
     
     x1 = minmax[0] - 5;
     y1 = minmax[1];
     x2 = minmax[0] + 5;
     y2 = minmax[1];
     g.drawLine(x1, y1, x2, y2);
     
     // the maximum line
     x1 = minmax[2];
     y1 = minmax[3] - 5;
     x2 = minmax[2];
     y2 = minmax[3] + 5;
     g.drawLine(x1, y1, x2, y2);
     
     x1 = minmax[2] - 5;
     y1 = minmax[3];
     x2 = minmax[2] + 5;
     y2 = minmax[3];
     g.drawLine(x1, y1, x2, y2);
  }
  
  public void updateDrawableRect(int compWidth, int compHeight) {
    int x = currentRect.x;
    int y = currentRect.y;
    int width = currentRect.width;
    int height = currentRect.height;
    
    //Make the width and height positive, if necessary.
    if (width < 0) {
      width = 0 - width;
      x = x - width + 1;
      if (x < 0) {
        width += x;
        x = 0;
      }
    }
    if (height < 0) {
      height = 0 - height;
      y = y - height + 1;
      if (y < 0) {
        height += y;
        y = 0;
      }
    }
    
    //The rectangle shouldn't extend past the drawing area.
    if ((x + width) > compWidth) {
      width = compWidth - x;
    }
    if ((y + height) > compHeight) {
      height = compHeight - y;
    }
    
    //Update rectToDraw after saving old value.
    if (rectToDraw != null) {
      previousRectDrawn.setBounds(
      rectToDraw.x, rectToDraw.y,
      rectToDraw.width, rectToDraw.height);
      rectToDraw.setBounds(x, y, width, height);
    } else {
      rectToDraw = new Rectangle(x, y, width, height);
    }
  }
  
  // Method to set the lines to be drawn either vertical or horizontal
  public void drawROI(Rectangle rectangle) {
    rectToDraw = rectangle;
    repaint();
  }
}//--end class PumpProbeImagePanel
