Results 1 to 16 of 16
Thread: Some Guidance
- 01-06-2011, 06:56 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Some Guidance
I just started my second quarter of Java. I'm in Java 2 now anyways I was given this as my first assignment:
Implement a Class Polygon that contains an array List of Point2D.Double objects. Support methods
public void add(Point2D.Double aPoint)
public void draw(Graphics g2)
Draw the polygon by joining adjacent points with a line, and then closing it up by joining the end and start points.
Write a graphical application that draws a square and a pentagon using two Polygon Objects.
I don't even know where to start with this. The only thing we have does with an array list is input a list of values and have the average calculated.
Any help is appreciated I'm just looking for a direction to take.
- 01-06-2011, 07:27 PM #2
You should probably use a more meaningful post title next time.
Do you have to use an applet? Writing an application is generally easier, and I think more people start there instead.
Read these:
Starting Writing a Program
Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
Painting in AWT and SwingHow to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-06-2011, 07:39 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
It is an application but I didn't know where to place the thread.
- 01-06-2011, 07:48 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
This is what I'm working with so far
public class Polygon {
private ArrayList<Point2D.Double> points;
public Polygon()
{
points=new ArrayList<Point2D.Double>();
}
public void addPoint (double X)
{
points.add(X);
}
And then I'll have a tester class to input the Point Axises
like:
Scanner in = new Scanner(System.in);
Polygon poly = new Polygon();
System.out.println("Input an Axis X, Y");
while (in.hasNextDouble())
{
poly.addPoint(in.nextDouble());
}
- 01-06-2011, 07:52 PM #5
That's a good start, although I'm not sure you need to use a Scanner at all. It sounds like you can hardcode the Points, as long as you use the add() function. But I'd definitely suggest reading through the GUI tutorials. We can't really help you until you do at least that.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-06-2011, 09:24 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Okay so I think I'm starting to make some progress
Here's where I'm at...
public class Polygon {
private ArrayList<Point2D.Double> myPolygon;
public void add(Point2D.Double aPoint) {
System.out.println(aPoint); // Outputs Point2D.Double[20.0, 10.0]
myPolygon.add(aPoint); // NullPointerException gets thrown here
}
and
Polygon poly = new Polygon();
Point2D.Double a = new Point2D.Double(20,10);
poly.add(a);
Point2D.Double b = new Point2D.Double(30,50);
poly.add(b);
Point2D.Double c = new Point2D.Double(40,60);
poly.add(c);
Point2D.Double d = new Point2D.Double(70,90);
poly.add(d);
Line2D.Double segmenta = new Line2D.Double(a, b);
Line2D.Double segmentb = new Line2D.Double(b, c);
Line2D.Double segmentc = new Line2D.Double(c, d);
Line2D.Double segmentd = new Line2D.Double(d, a);
- 01-07-2011, 12:53 AM #7
I think your requirements call for an ArrayList of Points, not a Polygon. Using Polygon is the more "advanced" way to go, but I think it's overkill for your assignment.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-07-2011, 02:02 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
I'm just trying to make it so the Array stores the points.
- 01-07-2011, 02:13 AM #9
I gotcha, but a Polygon isn't the same thing as an ArrayList of Points, and I'd bet your instructor would take points off. But I don't know, I'm just guessing based on your OP.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-07-2011, 12:11 PM #10
try
private static ArrayList<Point2D.Double> myPolygon = new ArrayList<Point2D.Double>();
so that you ArrayList will be instantiated. and i added the static so that all objects of Polygon will share the same ArrayList. if you don't want that remove static.
i made some modifications so that you can visualize your segments in a jpanel. the code is in the attachment. if you have questions let me know...Last edited by j2me64; 01-07-2011 at 01:35 PM.
- 01-07-2011, 02:57 PM #11
Hooray spoonfeeding :-/
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 01-07-2011, 05:18 PM #12
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
Hey, thank you very much for the example.
The question says that I need two polygon objects a square and a pentagon so I added this to the code...
Java Code:JFrame f = new JFrame("Polygon"); Polygon penta = new Polygon(); Point2D.Double a = new Point2D.Double(50, 50); penta.add(a); Point2D.Double b = new Point2D.Double(50, 150); penta.add(b); Point2D.Double c = new Point2D.Double(150, 150); penta.add(c); Point2D.Double d = new Point2D.Double(150, 50); penta.add(d); Point2D.Double e = new Point2D.Double(100, 10); penta.add(e); Line2D.Double segmenta = new Line2D.Double(a, b); Line2D.Double segmentb = new Line2D.Double(b, c); Line2D.Double segmentc = new Line2D.Double(c, d); Line2D.Double segmentd = new Line2D.Double(d, e); Line2D.Double segmente = new Line2D.Double(e, a); penta.addSegment(segmenta); penta.addSegment(segmentb); penta.addSegment(segmentc); penta.addSegment(segmente); penta.addSegment(segmentd); Polygon square = new Polygon(); Point2D.Double w = new Point2D.Double(175, 50); square.add(w); Point2D.Double x = new Point2D.Double(175, 100); square.add(x); Point2D.Double y = new Point2D.Double(225, 100); square.add(y); Point2D.Double z = new Point2D.Double(225, 50); square.add(z); // these segments build something like a house Line2D.Double segmentw = new Line2D.Double(w, x); Line2D.Double segmentx = new Line2D.Double(x, y); Line2D.Double segmenty = new Line2D.Double(y, z); Line2D.Double segmentz = new Line2D.Double(z, w); square.addSegment(segmentw); square.addSegment(segmentx); square.addSegment(segmenty); square.addSegment(segmentz); f.add(penta); f.add(square); f.setSize(400, 400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Thanks, again.
- 01-08-2011, 10:05 AM #13
bad idea because inside the polygon you can't distinquish which point belongs to which shape. other approach: the class Polygon is designed for one polygon, so changing this class is not a good idea. i suggest we write an other class, that can handle multiple polygons and methods that can paint or remove the passed polygon. what do you think?
- 01-10-2011, 12:05 AM #14
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
I want to display both polygons at the same time so I think I might just put all the points into one object.
- 01-10-2011, 08:06 PM #15
Member
- Join Date
- Oct 2010
- Posts
- 9
- Rep Power
- 0
So I solved my problem and made it so both polygons display. I just had them open in their own JFrame.
Anyway's I understand all of the code except this part:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
for (int i = 0; i < mySegments.size(); i++)
g.drawLine((int)mySegments.get(i).x1, (int)mySegments.get(i).y1, (int)mySegments.get(i).x2, (int)mySegments.get(i).y2);
I understand that this code runs through the Array List to find the values, but what is the .x1, .y1, etc?
- 01-11-2011, 10:04 AM #16
great, you could solve your problem. the reason you couldn't paint two shapes in the same frame is that each instance of Polygon uses its own Graphics. are you interested in an applet, that can do that?
to your last question: the array mySegments is storing line segments. so when you read a segment you got two points, the first point has its coordinates at x1 and y1 and the second point at x2 and y2. for detailed informations please read the Line2D api.
Similar Threads
-
newbie guidance.
By Bgreen7887 in forum New To JavaReplies: 28Last Post: 10-17-2010, 10:31 PM -
Guidance for SCJP
By sairam in forum New To JavaReplies: 1Last Post: 06-30-2010, 09:21 AM -
Guidance needed
By Curtiz in forum AWT / SwingReplies: 1Last Post: 03-23-2010, 05:41 PM -
Guidance in writing a compiler
By Jeremy in forum Advanced JavaReplies: 2Last Post: 11-28-2008, 11:25 PM -
Guidance
By gizdev in forum New To JavaReplies: 5Last Post: 09-07-2008, 07:14 PM
Bookmarks