Code we wrote in the second lecture: 

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

class One extends JFrame {
  One() {
    Container hook = this.getContentPane(); 
    Two b = new Two();
    hook.add( b );
    this.addMouseMotionListener( b );  
    this.addMouseListener( b ); 
    this.setVisible( true ); 
    this.setSize( 400, 400 ); 
    this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
  }
  public static void main(String[] args) {
    JFrame a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener, MouseListener {

  Circle current; 

  public void mouseEntered (MouseEvent e) { }
  public void mouseExited  (MouseEvent e) { }
  public void mousePressed (MouseEvent e) { 
    Point p = new Point( e.getX(), e.getY() ); 
    for (Circle c : this.circles) {
      if (c.contains(p)) {
        this.current = c;
        break; 
      } 
    }
  }
  public void mouseReleased(MouseEvent e) { 
    this.current = null; 
  }
  public void mouseClicked (MouseEvent e) { }

  ArrayList<Circle> circles; 
  Two() {
    this.circles = new ArrayList<Circle>(); 
    for (int i = 0;i < 20; i++) {
      this.circles.add( new Circle( new Point((int) (Math.random() * 300 + 50), 
                                              (int) (Math.random() * 300 + 50)), 
                                              (int) (Math.random() * 20 + 20))); 
    }
  } 
  int x, y; 
  public void mouseMoved(MouseEvent e) { }
  public void mouseDragged(MouseEvent e) { 
    int x = e.getX(), y = e.getY(); 
    this.x = x; this.y = y;     

    if (this.current != null) {
      this.current.moveTo( new Point(x, y) ); 
    } 

    this.repaint(); 
    System.out.println(" Mouse dragged... "); 
  }

  public void paintComponent(Graphics g) {
    for (Circle c : this.circles)
      c.draw(g); 
    g.drawString( "(" + this.x + ", " + this.y +")" , this.x, this.y ); 
    System.out.println(" I am painting... "); 
  } 


class Circle {
  Point center;
  int radius;
  Circle(Point c, int r) {
    this.center = c; 
    this.radius = r; 
  }
  void draw(Graphics g) {
    g.setColor(Color.YELLOW); 
    g.fillOval(this.center.x, this.center.y, 2 * this.radius, 2 * this.radius);
    g.setColor(Color.BLUE); 
    g.drawOval(this.center.x, this.center.y, 2 * this.radius, 2 * this.radius);

  }
  void moveTo(Point where) {
    this.center = where; 
  }
  boolean contains(Point p) {
    return p.distanceTo(center) <= this.radius; 
  }
}

class Point {
  int x, y; 
  Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  } 
  double distanceTo(Point other) {
    double dx = this.x - other.x;
    double dy = this.y - other.y; 
    return Math.sqrt( dx * dx + dy * dy ); 
  }
}