import java.awt.*; import java.util.*; class Circle { Point center; Color color; int radius; Foam foam; Circle(Foam foam) { this( new Point( (int) (300 * Math.random()), 0), new Color( (float) Math.random(), (float) Math.random(), (float) Math.random()), 10 + (int) (60 * Math.random()), foam ); } Circle(Point center, Color color, int radius, Foam foam) { this.center = center; this.color = color; this.radius = radius; this.foam = foam; } ArrayList neighbors = new ArrayList(); boolean fall() { if (this.neighbors.size() == 0 && this.center.y <= 560 - this.radius) { this.center.y += 5; for (Circle c : foam) { if (this != c && c.overlaps(this)) { this.neighbors.add(c); c.neighbors.add(this); } } return true; } else { return false; } } void draw(Graphics g) { g.setColor(this.color); g.fillOval(this.center.x - this.radius, this.center.y - this.radius, 2 * this.radius, 2 * this.radius); g.setColor(Color.BLACK); g.drawOval(this.center.x - this.radius, this.center.y - this.radius, 2 * this.radius, 2 * this.radius); } boolean overlaps(Circle other) { return this.center.distanceTo(other.center) <= this.radius + other.radius; } }