/**
   @version 1.30 2000-06-03
   @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;

public class MouseTestContiD
{
   public static void main(String[] args)
   {
      MouseFrame frame = new MouseFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
   }
}

/**
   A frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
   public MouseFrame()
   {
      setTitle("MouseTest");
      setSize(WIDTH,HEIGHT);

      // add panel to frame

      MousePanel panel = new MousePanel();
      Container contentPane = getContentPane();
      contentPane.add(panel);
   }

   public static final int WIDTH = 1200;
   public static final int HEIGHT = 800;
}

/**
   A panel with mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
   public MousePanel()
   {
      squares = new ArrayList();
      current = null;

      addMouseListener(new MouseHandler());
      addMouseMotionListener(new MouseMotionHandler());
   }

   public void paintComponent(Graphics g)
   {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;

      // draw all squares
      for (int i = 0; i < squares.size(); i++)
         //g2.draw((Rectangle2D)squares.get(i));
         int d = MousePanel.getDistance();
         int width = MouseFrame.WIDTH;
         int height = MouseFrame.HEIGHT;
         double maxd = 0.5*(Math.sqrt(Math.pow(width,2)+Math.pow(height,2)));
         int intensity = (int)(128*(1-d/maxd));
         g2.setPaint(new Color(intensity,intensity,intensity));
         g2.fill((Rectangle2D)squares.get(i));
   }

   /**
      Finds the first square containing a point.
      @param p a point
      @return the index of the first square that contains p
   */
   public Rectangle2D find(Point2D p)
   {
      for (int i = 0; i < squares.size(); i++)
      {
         Rectangle2D r = (Rectangle2D)squares.get(i);
         if (r.contains(p)) return r;
      }

      return null;
   }
   /**
      Adds a square to the collection.
      @param p the center of the square
   */
   public void add(Point2D p)
   {
      double x = p.getX();
      double y = p.getY();

      current = new Rectangle2D.Double(
         x - SIDELENGTH / 2,
         y - SIDELENGTH / 2,
         SIDELENGTH,
         SIDELENGTH);
      squares.add(current);
      repaint();
   }

   /**
      Removes a square from the collection.
      @param s the square to remove
   */
   public void remove(Rectangle2D s)
   {
      if (s == null) return;
      if (s == current) current = null;
      squares.remove(s);
      repaint();
   }


   static  int SIDELENGTH = 10;
   //int l = SIDELENGTH;
   private ArrayList squares;
   private Rectangle2D current;
   // the square containing the mouse cursor

   private class MouseHandler extends MouseAdapter
   {
      public void mousePressed(MouseEvent event)
      {
         // add a new square if the cursor isn't inside a square
         current = find(event.getPoint());
         if (current == null)
            add(event.getPoint());
      }

      public void mouseClicked(MouseEvent event)
      {
         // remove the current square if double clicked
         current = find(event.getPoint());
         if (current != null && event.getClickCount() >= 2)
            remove(current);
      }
   }

   private class MouseMotionHandler
      implements MouseMotionListener
   {
      public void mouseMoved(MouseEvent event)
      {
         // set the mouse cursor to cross hairs if it is inside
         // a rectangle

         if (find(event.getPoint()) == null)
            setCursor(Cursor.getDefaultCursor());
         else
            setCursor(Cursor.getPredefinedCursor
               (Cursor.CROSSHAIR_CURSOR));
      }

      public void mouseDragged(MouseEvent event)
      {
         if (current != null)
         {
            int x = event.getX();
            int y = event.getY();
            int xd = x-(MouseFrame.WIDTH/2);
            int yd = y-(MouseFrame.HEIGHT/2);
            System.out.println(xd+" "+yd);
             d = (int)Math.sqrt((Math.pow(xd,2))+(Math.pow(yd,2)));
            // drag the current rectangle to center it at (x, y)
            int sidelength=d*SIDELENGTH/100;
            current.setFrame(
               x -sidelength  / 2,
               y - sidelength / 2,
               sidelength,
            sidelength);
            repaint();

         }
      }

   }
     public int getDistance()
      {
		  return d;
      }
      int d;
      }