/* @(#) DrawSimpleShapes.java 1.1 99/7/12
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package app;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import com.vistech.graphics.*;
/** An application to draw simplae shapes interactively.
  * @version 1.0 12 july 1999
  * @author Lawrence Rodrigues
  **/
public class DrawSimpleShapes implements  MouseListener,MouseMotionListener{
   final static int RECTANGLE = 2;
   final static int ELLIPSE = 4;
   final static int LINE = 1;
   protected int shapeType = LINE;

   protected SimpleGraphicsCanvas2D grCanvas;
   protected Point diff = new Point(0,0);
   protected Point shapeAnchor  = new Point(0,0);
   protected boolean scrollOn = true;
   protected boolean mousePressed = false;
   protected Shape currentShape, prevShape;

   public DrawSimpleShapes(SimpleGraphicsCanvas2D c){
      grCanvas = c;
   }

   public void startDraw(int x, int y) {
      shapeAnchor = new Point(x,y);
      diff = new Point(0,0);
   }

   public void draw(int x, int y) {
      switch(shapeType) {
        case LINE:
           currentShape =
           new Line2D.Float((Point2D)shapeAnchor, (Point2D)new Point(x,y));
           break;
        case RECTANGLE:
        case ELLIPSE:
           diff.x = x - shapeAnchor.x;
           diff.y = y - shapeAnchor.y;
           int wid = diff.x;
           int ht = diff.y;
           Point ulhc = new Point(shapeAnchor);
           if(diff.x <0) {
              wid = -diff.x;
              ulhc.x = x;
           }
           if(diff.y <0){
              ht = -diff.y;
              ulhc.y = y;
           }
           if(shapeType == RECTANGLE)
              currentShape = new Rectangle(ulhc.x, ulhc.y, wid, ht);
           else currentShape = new Ellipse2D.Double(ulhc.x, ulhc.y, wid, ht);

           break;
      }
      Graphics2D g = grCanvas.getDisplayedImageGC();
      g.setColor(Color.black);
      g.setXORMode(Color.blue);
      if(prevShape != null) grCanvas.draw(prevShape);
      grCanvas.draw(currentShape);
      prevShape = currentShape;
   }

   public void mousePressed(MouseEvent e) {
      if(!scrollOn) return;
      if(mousePressed) return;
      mousePressed = true;

      if(SwingUtilities.isLeftMouseButton(e)){
         startDraw(e.getX(), e.getY());
      }else {
          popupMenu((JComponent)e.getSource(), e.getX(), e.getY());
       }
   }

   public void mouseReleased(MouseEvent e) {
      if(SwingUtilities.isLeftMouseButton(e)){
         Graphics2D g = grCanvas.getDisplayedImageGC();
         draw(e.getX(), e.getY());
         prevShape = null;
      }
      mousePressed = false;
   }


   public void mouseClicked(MouseEvent e){}
   public void mouseEntered(MouseEvent e){}
   public void mouseExited(MouseEvent e){}

   public void mouseDragged(MouseEvent e){
      if(SwingUtilities.isLeftMouseButton(e)){
          draw(e.getX(), e.getY());
      }
   }
   public void mouseMoved(MouseEvent e){}
   protected void popupMenu(JComponent comp,int x, int y){
      JPopupMenu jp = new JPopupMenu("");
      jp.setLightWeightPopupEnabled(true);
      comp.add(jp);
      JMenuItem line = new JMenuItem("Line");
      line.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              shapeType = LINE;
           }
         }
      );
      JMenuItem rect= new JMenuItem("Rectangle");
      rect.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              shapeType = RECTANGLE;
           }
         }
      );

      JMenuItem eli = new JMenuItem("Ellipse");
      eli.addActionListener(
         new ActionListener(){
           public void actionPerformed(ActionEvent e){
              shapeType = ELLIPSE;
           }
         }
      );

      jp.add(line);
      jp.add(rect);
      jp.add(eli);
      jp.show(comp,x,y);
   }

   public static void  main(String[] args){
      SimpleGraphicsCanvas2D gc = new SimpleGraphicsCanvas2D();
      DrawSimpleShapes dss = new DrawSimpleShapes(gc);
      gc.addMouseListener(dss);
      gc.addMouseMotionListener(dss);
      gc.setSize(600,400);
      JFrame fr = new JFrame();
      fr.addWindowListener(
        new WindowAdapter(){
          public void windowClosing(WindowEvent e){
             System.exit(0);
          }
        }
      );
      fr.setTitle("Graphics Canvas");
      fr.getContentPane().add((Component)gc);
      fr.pack();
      fr.setSize(400,300);
      fr.show();
      fr.repaint();
  }
}