import java.awt.geom.Point2D; import java.awt.geom.Ellipse2D; import java.awt.Graphics2D; import java.util.ArrayList; /** This class draws a cloud of circles */ public class Cloud { /** Construct a Cloud object */ public Cloud() { points = new ArrayList(); } /** Add a point to the array list @param aPoint the point to add */ public void add(Point2D.Double aPoint) { points.add(aPoint); } /** Draws the cloud @param g2 the graphics context */ public void draw(Graphics2D g2) { for (int i = 0; i < points.size(); i++) { Point2D.Double p = (Point2D.Double)points.get(i); Ellipse2D.Double e = new Ellipse2D.Double(p.getX(), p.getY(), RADIUS, RADIUS); g2.draw(e); } } private ArrayList points; private final int RADIUS = 5; } /* Class Cloud: 1. Constructor creates an array list. 2. The method add takes a Point2D.Double as an argument and it adds it to the array list. 3. The method draw creates the points and uses g2.draw(aPoint). Note: a Point2D.Double can not be drawn. So, you should draw an Ellipse2D.Double with x and y from Point2D.Double. 4. Write a class that extends Applet or JApplet to draw a cloud's points by creating an object of Cloud and randomly generating the x and y coordinates. */
import javax.swing.JApplet; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.Point2D; /** This applet displays a cloud of circles */ public class ExP13_7JApplet extends JApplet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Cloud c = new Cloud(); Random generator = new Random(); double x = 0; double y = 0; for (int i = 0; i < 20; i++) { x = 200 * generator.nextDouble(); y = 200 * generator.nextDouble(); c.add(new Point2D.Double(x, y)); } c.draw(g2); } }