0

I'm having a lot of trouble with this one program and I was wondering if anyone could tell me what I'm doing wrong... Here's the prompt: Implement a class Cloud that contains an array list of Point2D.Double objects. Support methods

public void add(Point2D.Double aPoint)
public void draw (Graphics2D g2)

Draw each point as a tiny circle. Write a graphical application that draws a cloud of 100 random points.

Here's what I have, but it won't actually do anything, I'm just really confused and I could use any 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;
        for (int i=0;i<=20;i++)
        {
            this.add(new Point2D.Double(rand.nextInt(400)+1,rand.nextInt(300)+1));
        }
        for (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);
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Well, you're not adding anything to the cloud... – MadProgrammer Aug 19 '14 at 02:09
  • And you're creating a new Cloud within the paint method which is probably not a good idea. Better to create just one cloud in the class, and then use that instance inside of paint. – Hovercraft Full Of Eels Aug 19 '14 at 02:10
  • 1
    And you've not actually overriden the `paint` method correctly so it will never get called... – MadProgrammer Aug 19 '14 at 02:17
  • @MadProgrammer: Oy, indeed! – Hovercraft Full Of Eels Aug 19 '14 at 02:17
  • 1) Why code an applet? If it is due to spec by your instructor, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT components rather than Swing? See [this answer](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon AWT. – Andrew Thompson Aug 19 '14 at 12:29

1 Answers1

1

Basically, probably in your init method, you actually need to create a random series of data...

Take a look at java.util.Random for starters.

Basically, you need to create a random x position, with the an upper bound no greater than the width of the applet and a random y position, with an upper bound no greater than the height of the applet, for example...

private Cloud cloud = new Cloud();

//...Probably within the init method...
Random rnd = new Random();
int cloudDensity = 10 + rnd.nextInt(990);
for (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 your paint method, you need to paint this cloud...

    @Override
    public void paint(Graphics g)
    {    
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g.create();
        myCloud.draw(g2d);
        g2d.dispose();
    }

Now, the paint method of Applet expects an instance of Graphics, not Graphcis2D, otherwise the method will never get painted. This is where the @Override annotation is so important, as it will cause a compile time error if you've done something wrong when attempting to override a method

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366