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

/*
 * Draws a circle, polygon and rectangle using Graphics2D
 * and lets the user move these objects using the mouse.
 */
public class MyMovingShapes extends JFrame implements MouseInputListener
{
  Ellipse2D.Double circle;
  Polygon polygon;
  Rectangle rectangle;
  int xpoints[] = {100, 150, 170, 160, 130};
  int ypoints[] = {100,  80, 120, 150, 160};

  Point startDrag;
  boolean dragCircle, dragPolygon, dragRectangle;

  public MyMovingShapes()
  {
    super("MyMovingShapes");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setSize(300, 300);
    addMouseListener( this );
    addMouseMotionListener( this );

    //init shapes
    circle = new   Ellipse2D.Double( 50, 50, 40, 40 );
    polygon = new Polygon( xpoints, ypoints, xpoints.length );
    rectangle = new Rectangle( 200, 200, 50, 50 );

    show();
  }

  public void paint( Graphics g )
  {
    //use Graphics2D
    Graphics2D g2d = (Graphics2D)g;

    //clear screen
    g2d.setColor( g2d.getBackground() );
    g2d.fillRect( 0, 0, getWidth(), getHeight() );

    //draw the objects based on the contained values
    g2d.setColor( Color.black );
    g2d.fill( circle );
    g2d.fill( polygon );
    g2d.fill( rectangle );

  }

 //methods from MouseInputListener

  public void mouseClicked(MouseEvent e)
  {
    Point p;

    /* Note that if you move the mouse while clicking, 
     * it is not registered as a click but a drag.
     * To some users, this might seem a bit too sensitive.
     * So to solve this, move this code to the mouseReleased method.
     */

    //get the point that was clicked
    p = e.getPoint();

    //check which object was clicked
    //if-else is deliberately avoided, so overlapping figures may be detected.
    if( circle.contains( p ) )
    {
      System.out.println("The circle was hit");
    }

    if( polygon.contains( p ) )
    {
      System.out.println("The polygon was hit");
    }

    if( rectangle.contains( p ) )
    {
      System.out.println("The rectangle was hit");
    }
    
  }


  public void mouseEntered(MouseEvent e)
  {
  }


  public void mouseExited(MouseEvent e)
  {
  }


  public void mousePressed(MouseEvent e)
  {
    Point p;

    //register start of drag
    System.out.println("Started drag");

    //get the point that was pressed
    p =  e.getPoint();
    startDrag = p;

    //check which object was clicked
    //if-else is deliberately avoided, so overlapping figures may be detected.
    if( circle.contains( p ) )
    {
      dragCircle = true;
    }

    if( polygon.contains( p ) )
    {
      dragPolygon = true;
    }

    if( rectangle.contains( p ) )
    {
      dragRectangle = true;
    }
       

  }


  public void mouseReleased(MouseEvent e)
  {
    //end drag
    startDrag = null;
    dragCircle = dragPolygon = dragRectangle = false;
    System.out.println("End dragging");
  }


  public void mouseDragged(MouseEvent e)
  {
    Point p;
    int dX, dY;

    //get the point that was clicked
    p = e.getPoint();

    //ensure that we have a start dragging point
    //startDrag = p;

    //calculate difference between start and current drag poing
    dX = p.x - startDrag.x;
    dY = p.y - startDrag.y;
    
    //check which object(s) we are dragging and move them
    if( dragCircle )
    {
      circle.x += dX;
      circle.y += dY;
    }

    if( dragPolygon )
    {
      polygon.translate( dX, dY );
    }

    if( dragRectangle )
    {
      rectangle.translate( dX, dY );
    }

    //reset startdrag point
    startDrag = p;

    //redraw the screen
    repaint();
        
  }

  public void mouseMoved(MouseEvent e)
  {
  }

  public static void main(String args[])
  {
    new MyMovingShapes();
  }
}