Results 1 to 16 of 16

Thread: Some Guidance

  1. #1
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,878
    Rep Power
    8

    Default

    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 Swing
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    It is an application but I didn't know where to place the thread.

  4. #4
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    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());
    }

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,878
    Rep Power
    8

    Default

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    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);

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,878
    Rep Power
    8

    Default

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    I'm just trying to make it so the Array stores the points.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,878
    Rep Power
    8

    Default

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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...
    Attached Files Attached Files
    Last edited by j2me64; 01-07-2011 at 01:35 PM.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,878
    Rep Power
    8

    Default

    Hooray spoonfeeding :-/
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    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);
    However, only the last Polygon object, in this case "square" appears in the JFrame. Is there a way to get around this. I know I could put all the points and segments in one Polygon Object, but the question asks for two.
    Thanks, again.

  13. #13
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by bengregg View Post
    I know I could put all the points and segments in one Polygon Object, but the question asks for two.
    Thanks, again.

    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?

  14. #14
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    I want to display both polygons at the same time so I think I might just put all the points into one object.

  15. #15
    bengregg is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    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?

  16. #16
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by bengregg View Post
    I understand that this code runs through the Array List to find the values, but what is the .x1, .y1, etc?

    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

  1. newbie guidance.
    By Bgreen7887 in forum New To Java
    Replies: 28
    Last Post: 10-17-2010, 10:31 PM
  2. Guidance for SCJP
    By sairam in forum New To Java
    Replies: 1
    Last Post: 06-30-2010, 09:21 AM
  3. Guidance needed
    By Curtiz in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2010, 05:41 PM
  4. Guidance in writing a compiler
    By Jeremy in forum Advanced Java
    Replies: 2
    Last Post: 11-28-2008, 11:25 PM
  5. Guidance
    By gizdev in forum New To Java
    Replies: 5
    Last Post: 09-07-2008, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •