//Yvon H. Feaster
//Lab 3
//January 31, 2006

import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
import java.awt.*;


class Draw extends JPanel
{

   private Vector<Shape> shapes;
   private Point start, end;
   

   public Draw () 
   {
      JFrame frame = new JFrame ("Draw");
      frame.setSize(400,600);
      frame.setVisible(true);
      frame.add(this);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      shapes = new Vector<Shape>();
      DrawAdapter drawing = new DrawAdapter();
      addMouseListener(drawing);
      addMouseMotionListener(drawing);
      start = new Point(0,0);
      end = new Point(0,0);
   }

   
   public void paint (Graphics g)
   {
      super.paint(g);
      g.setColor(Color.blue);

      for(Shape shape : shapes)
      {
        ((Graphics2D)g).fill (shape);
      }
         
         
         int w = Math.abs(start.x - end.x);
         int h = Math.abs(start.y - end.y);
         
          int x,y;
         if(start.x < end.x)
         {
            x = start.x;
         }
         else 
         {
            x = end.x;
         }
         if(start.y < end.y)
         {
            y = start.y;
         }
         else
         {
            y = end.y;
         }
     

         g.drawRect(x,y,w,h);
  
     
     
   }
   
   class DrawAdapter extends MouseAdapter
   {
      public void mousePressed(MouseEvent e)
      {

         start = new Point(e.getX(), e.getY());
 
      }

      public void mouseReleased(MouseEvent e)
      {
         int x,y;
         if(start.x < e.getX())
         {
            x = start.x;
         }
         else 
         {
            x = e.getX();
         }
         if(start.y < e.getY())
         {
            y = start.y;
         }
         else
         {
            y = e.getY();
         }
         int w = Math.abs(start.x - e.getX());
         int h = Math.abs(start.y - e.getY());
System.out.println ("x " + x + " y " + y + " w " + w + " h " + h);
         Rectangle rect = new Rectangle(x, y, w, h);
         shapes.add(rect);
         repaint();
      }

      public void mouseDragged(MouseEvent e)
      {
         end = new Point(e.getX(), e.getY());
         repaint();
      }

   }
   
   public static void main (String []args) {
      new Draw();
   }
}
      
      
   
