import java.awt.*; 
import java.util.*;

public class Circle {
  boolean safe; // false initially
  Point center;
  int radius;
  Color color; 
  int neighbors; // [1]
  public void neighbors(ArrayList<Circle> circles) { // [1]
    this.neighbors = 0; 
    for (Circle c : circles)
      if (c != this && c.overlaps(this)) 
        this.neighbors += 1; 
  }
  public Circle(Point center, int radius, Color color) {
    this.center = center;
    this.radius = radius;
    this.color = new Color((float) (Math.random()/2 + 0.5), 
                           (float) (Math.random()/2 + 0.5), 
                           (float) (Math.random()/2) + 0.5f); 
  }
  public void draw(Graphics g) {
    g.setColor(this.color); 
    g.fillOval(center.x - radius, center.y - radius, 2 * radius, 2 * radius); 
    g.setColor(Color.BLACK); 
    g.drawOval(center.x - radius, center.y - radius, 2 * radius, 2 * radius); 
    g.drawString(this.neighbors + "", center.x, center.y); // [1] 
  }
  public boolean contains(Point point) {
    return this.center.distanceTo(point) < this.radius; 
  }
  public void move(int x, int y) {
    this.center = new Point(x, y);  
  }
  public boolean overlaps(Circle c) {
    return this.center.distanceTo(c.center) < this.radius + c.radius; 
  }
}


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

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

public class Screen extends JComponent implements MouseMotionListener, MouseListener {
  ArrayList<Circle> circles = new ArrayList<Circle>(); 
  Circle current; // = null;
  public Screen() {
    for (int i = 0; i < 12; i++)
      this.circles.add(new Circle(new Point((int)(Math.random()*390) + 5, 
                                            (int)(Math.random()*390) + 5),
                                  (int)(Math.random() * 40 + 10),
                                  Color.WHITE));
    for (Circle c : this.circles)
      c.neighbors(this.circles); 
    this.addMouseMotionListener(this);
    this.addMouseListener(this);
  }
  public void paintComponent(Graphics g) {
    for (Circle c : this.circles)
      c.draw(g); 
  }
  public void mouseMoved(MouseEvent e) { }
  public void mouseDragged(MouseEvent e) { 
    if (this.current != null) {
      this.current.move(e.getX(), e.getY()); 
      for (Circle c : this.circles) { // [1] 
        c.neighbors(this.circles); // [1] 
      }
      this.repaint(); 
    }
  }
  public void purgeOne() { 
    ArrayList<Circle> result = new ArrayList<Circle>(); 
    for (Circle c : this.circles)
      if (c.neighbors >= 3) {
          
      } else {
        result.add( c );
      }
    this.circles = result; 
  }
  public void purgeTwo() { 
    for (Circle c : this.circles) 
      c.safe = false; 
    ArrayList<Circle> result = new ArrayList<Circle>(); 
    for (Circle c : this.circles)
      if (c.center.y < c.radius) // connected to the ceiling
        c.safe = true; 
    for (Circle c1 : circles) // Yi saved us; how did she do it? 
      for (Circle c2 : circles)
        if (c1.overlaps(c2))
          if (c1.safe || c2.safe) 
            c1.safe = c2.safe = true; 
    for (Circle c : this.circles)
      if (c.safe)
        result.add(c); 
    this.circles = result; 
  }
  public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();
    if (code == 49) { // this is a 1
      this.purgeOne();  
    } else if (code == 50) { // this is a 2 
      this.purgeTwo();  
    } 
    this.repaint(); 
    // System.out.println(e.getKeyCode()); 
  }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { 
    for (Circle c : this.circles) {
      if (c.contains(new Point(e.getX(), e.getY()))) {
         this.current = c;
         // break;
      }
    }
    if (this.current != null) {
      this.circles.remove( this.current ); 
      this.circles.add( this.current ); 
    }
  }
  public void mouseReleased(MouseEvent e) { 
    this.current = null; // [0] 
  }
  public void mouseClicked(MouseEvent e) { }
}

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

public class Testing extends JFrame implements KeyListener {
  public Testing() {
    this.setSize(400, 400);
    this.setVisible(true); 
    this.screen = new Screen(); 
    this.addKeyListener(this); 
  }
  Screen screen;
  public void keyPressed(KeyEvent e) { 
    this.screen.keyPressed(e); 
  }
  public void keyReleased(KeyEvent e) { }
  public void keyTyped(KeyEvent e) { }  
  public static void main(String[] args) {
    Testing a = new Testing();  
    a.add(a.screen); 
  }
}