applet - How to write a java program to make a "cloud" of random dots? -


i'm having lot of trouble 1 program , wondering if tell me i'm doing wrong... here's prompt: implement class cloud contains array list of point2d.double objects. support methods

public void add(point2d.double apoint) public void draw (graphics2d g2) 

draw each point tiny circle. write graphical application draws cloud of 100 random points.

here's have, won't anything, i'm confused , use help!

cloud.java:

import java.awt.*; import java.util.*; import java.awt.geom.point2d; import java.awt.geom.ellipse2d;  public class cloud {         public void draw(graphics2d g)     {         random rand = new random();         graphics2d g2 = (graphics2d) g;         (int i=0;i<=20;i++)         {             this.add(new point2d.double(rand.nextint(400)+1,rand.nextint(300)+1));         }         (int i=0; i<list.size();i++)          {             ellipse2d.double circle = new         ellipse2d.double(list.get(i).getx()-5,list.get(i).gety()-5, 10, 10);             g2.draw(circle);         }     }     public void add(point2d.double apoint)     {         list.add(apoint);     }      private arraylist<point2d.double> list = new arraylist<point2d.double>(); } 

cloudtest.java:

import java.applet.applet; import java.awt.*; import java.awt.geom.point2d; import java.util.*;  public class cloudtest extends applet {     public void paint(graphics2d g)     {         graphics2d g1 = (graphics2d) g;         cloud mycloud = new cloud();         mycloud.draw(g1);     } } 

basically, in init method, need create random series of data...

take @ java.util.random starters.

basically, need create random x position, upper bound no greater width of applet , random y position, upper bound no greater height of applet, example...

private cloud cloud = new cloud();  //...probably within init method... random rnd = new random(); int clouddensity = 10 + rnd.nextint(990); (int index = 0; index < clouddensity; index++) {      int x = rnd.nextint(getwidth());     int y = rnd.nextint(getheight());     cloud.add(new point2d.double(x, y));  } 

then in paint method, need paint cloud...

    @override     public void paint(graphics g)     {             super.paint(g);         graphics2d g2d = (graphics2d)g.create();         mycloud.draw(g2d);         g2d.dispose();     } 

now, paint method of applet expects instance of graphics, not graphcis2d, otherwise method never painted. @override annotation important, cause compile time error if you've done wrong when attempting override method


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -