// This is the code from lecture with lines added for circle/shape selection. 

// Test it, and add Rectangles. 

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

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public double distanceTo(Point other) {                   // [ 1] 
    int dx = this.x - other.x;                              // [ 2]
    int dy = this.y - other.y;                              // [ 3]
    return Math.sqrt( Math.pow(dx, 2) + Math.pow(dy, 2) );  // [ 4]
  } 
}

abstract class Shape {
  Point location; 
  Shape(Point where) {
    this.location = where; 
  } 
  abstract void draw(Graphics g); 
  abstract boolean contains(Point somewhere);               // [ 5] 
  void moveTo(int x, int y) {
    this.location = new Point(x, y); 
  }
}

class Circle extends Shape {
  int radius;
  Circle(Point center, int radius) {
    super(center); 
    this.radius = radius;
  } 
  void draw(Graphics g) {
    g.setColor(Color.YELLOW); 
    g.fillOval(location.x - radius, location.y - radius, 2 * radius, 2 * radius);
    g.setColor(Color.BLUE); 
    g.drawOval(location.x - radius, location.y - radius, 2 * radius, 2 * radius);
  }   
  boolean contains(Point that) {                            // [ 6] 
    return that.distanceTo(this.location) <= this.radius;   // [ 7] 
  }                                                         // [ 8] 
}

class One extends JFrame {
  One(int howMany) {
    Two screen = new Two(howMany); 
    this.add( screen ); 
    this.setSize(400, 400); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true); 
  } 
  public static void main(String[] args) {
    int howMany = Integer.parseInt(args[0]); 
    One a = new One(howMany); 
   
  } 
}

class Two extends JComponent implements MouseMotionListener
                                      , MouseListener        // [ 9]  
{
  public void mouseEntered (MouseEvent e) { } 
  public void mouseExited  (MouseEvent e) { } 
  public void mousePressed (MouseEvent e) { 
    int x = e.getX(), y = e.getY();                          
    Point mouse = new Point(x, y);                           
    for (Shape shape : circles)                              // [10] 
      if (shape.contains(mouse)) {                           // [11] 
        current = shape;                                     // [12] 
        break;                                               // [13] 
      }                                                      // [14] 
  } 
  public void mouseReleased(MouseEvent e) { 
    current = null;                                          // [15] 
  } 
  public void mouseClicked (MouseEvent e) { } 

  Shape current; 
  public void mouseMoved(MouseEvent e) {
    // System.out.println("Mouse moved at (" + e.getX() + ", " + e.getY() + ")"); 
  } 
  public void mouseDragged(MouseEvent e) {
    // System.out.println("Mouse dragged at (" + e.getX() + ", " + e.getY() + ")"); 
    int x = e.getX(), y = e.getY(); 
    if (current != null) {                                   // [16] 
      this.current.moveTo(x, y); 
      this.repaint(); 
    }                                                        // [17] 
  }     
  Two(int size) {
    this.addMouseMotionListener(this); 
    this.addMouseListener(this);                             // [18]
    this.circles = new ArrayList<Shape>(); 
    for (int i = 0; i < size; i++)
      circles.add(new Circle(new Point((int) (Math.random()* 300) + 50, 
                                       (int) (Math.random()* 300) + 50), 
                                       (int) (Math.random()* 40) + 10)); 
    // this.current = this.circles.get(0);
  } 
  int i; 
  ArrayList<Shape> circles;; 
  public void paintComponent(Graphics g) {
    // this.i += 1; 
    // System.out.println( " Howdy. " + i ); 
    for (Shape c : this.circles) 
      c.draw(g); 
  }  
}