/* */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class One extends Applet implements MouseMotionListener { Circle[] circles = new Circle[100]; public void init() { this.addMouseMotionListener(this); for (int i = 0; i < this.circles.length; i++) { Point center = new Point((int)(Math.random() * 600 + 50), (int)(Math.random() * 600 + 50) ); int radius = (int)(Math.random() * 80 + 10) ; Color color = new Color( (float) Math.random(), (float) Math.random(), (float) Math.random()); this.circles[i] = new Circle(center, radius, color); } } public void paint(Graphics g) { for (int i = 0; i < this.circles.length; i++) this.circles[i].draw(g); } public void mouseDragged(MouseEvent e) { if (this.currentCircle != null) this.currentCircle.move(e.getX(), e.getY()); else for (int i = 0; i < this.circles.length; i++) if (this.circles[i].contains(e.getX(), e.getY())) { this.currentCircle = this.circles[i]; break; } this.repaint(); } Circle currentCircle; public void mouseMoved(MouseEvent e) { this.currentCircle = null; } } class Circle { Point center; int radius; Color color; void move(int x, int y) { this.center = new Point(x+radius/2, y+radius/2); } boolean contains(int x, int y) { Point a = new Point(x, y); return a.distanceTo(this.center) < this.radius; } Circle(Point c, int r, Color color) { this.center = c; this.radius = r; this.color = color; } void draw(Graphics g) { g.setColor(this.color); g.fillOval(center.x - radius, center.y - radius, radius, radius); g.setColor(Color.BLACK); g.drawOval(center.x - radius, center.y - radius, radius, radius); } } class Point { int x, y; Point(int x, int y) { this.x = x; this.y = y; } double distanceTo(Point other) { return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2)); } }