import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; import java.util.Random; public class CloudTest extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Random generator = new Random(); final int CLOUD_SIZE = 100; Cloud randomCloud = new Cloud(CLOUD_SIZE); for (int i = 0; i < CLOUD_SIZE; i++) { // generate two random numbers between 100 and 200 double x = 100 + 100 * generator.nextDouble(); double y = 100 + 100 * generator.nextDouble(); randomCloud.add(new Point2D.Double(x, y)); } randomCloud.draw(g2); } } /** A cloud is a collection of dots. */ class Cloud { /** Constructs a cloud with a given number of dots. @param n the number of dots. */ public Cloud(int n) { dots = new Point2D.Double[n]; dotsSize = 0; } /** Sets a dot point of the cloud. The point is ignored if the maximum number of points has been added. @param p the dot point */ public void add(Point2D.Double p) { if (dotsSize < dots.length) { dots[dotsSize] = p; dotsSize++; } } /** Draws the cloud. @param g2 the graphics context */ public void draw(Graphics2D g2) { final double SMALL_CIRCLE_RADIUS = 2; for (int i = 0; i < dotsSize; i++) { Ellipse2D.Double smallCircle = new Ellipse2D.Double( dots[i].getX() - SMALL_CIRCLE_RADIUS, dots[i].getY() - SMALL_CIRCLE_RADIUS, 2 * SMALL_CIRCLE_RADIUS, 2 * SMALL_CIRCLE_RADIUS); g2.draw(smallCircle); } } private Point2D.Double[] dots; private int dotsSize; }