Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: 2D Polygon Help?

  1. #1
    pikapika432 is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default 2D Polygon Help?

    I'm trying to create a program that makes a square and a pentagon and displays them in a separate window.

    Viewer (do not change):
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package polygon;
    
    /**
     *
     * @author Ashley
     */
    import javax.swing.JFrame;
    
    public class PolygonViewer
    {
       public static void main(String[] args)
       {
          JFrame frame = new JFrame();
    
          final int FRAME_WIDTH = 300;
          final int FRAME_HEIGHT = 400;
    
          frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
          frame.setTitle("PolygonViewer");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          PolygonComponent component = new PolygonComponent();
          frame.add(component);
    
          frame.setVisible(true);
       }  
    }
    PolygonComponent (do not change):
    Java Code:
    package polygon;
    
    import javax.swing.JComponent;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Point2D;
    
    /**
       Displays two polygons.
    */
    public class PolygonComponent extends JComponent
    {
       public void paintComponent(Graphics g)
       {
          Graphics2D g2 = (Graphics2D) g;
    
          Polygon square = new Polygon();
          square.add(new Point2D.Double(100, 100));
          square.add(new Point2D.Double(100, 150));
          square.add(new Point2D.Double(150, 150));
          square.add(new Point2D.Double(150, 100));
    
          square.draw(g2);
    
          Polygon pentagon = new Polygon();
          double centerX = 200;
          double centerY = 200;
          double radius = 50;
          for (int i = 0; i < 5; i++)
          {
              double angle = 2 * Math.PI * i / 5;
              pentagon.add(new Point2D.Double(
                               centerX + radius * Math.cos(angle),
                               centerY + radius * Math.sin(angle)));
          }
    
          pentagon.draw(g2);
       }
    }
    Polygon (need help with this):
    Java Code:
    import java.awt.Graphics2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.util.ArrayList;
    
    public class Polygon {
        
      private ArrayList<Double> xValues = new ArrayList<>();
        private ArrayList<Double> yValues = new ArrayList<>();
        private Line2D line;
        public void add(Point2D.Double aPoint) {
         xValues.add(aPoint.getX());
            yValues.add(aPoint.getY());
            
        }
    
          
                
      public void draw(Graphics2D g2) {
    
            for (int i = 0; i < xValues.size()-1; i++) {
                    line = new Line2D.Double ();
                    line.setLine(xValues.get(i), yValues.get(i) , xValues.get(i+1), yValues.get(i+1) );
                    g2.draw(line);
            }
        }
    }
    I think I'm getting an off by one error somewhere and I need some help fixing that.

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

    Default Re: 2D Polygon Help?

    What does this code do? What do you expect it to do? In your loop, why are you stopping before xValues.size()-1?
    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
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,101
    Rep Power
    4

    Default Re: 2D Polygon Help?

    Quote Originally Posted by KevinWorkman View Post
    In your loop, why are you stopping before xValues.size()-1?
    Well because otherwise this

    Java Code:
    xValues.get(i+1)
    goes beyond the valid range (edit: right? I have done countless things with Java2D paiting and yet I still have trouble envisioning these kind of things).
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,701
    Rep Power
    8

    Default Re: 2D Polygon Help?

    Ah yeah, probably. I just saw the < and the -1 and thought it looked weird. Although I would bet the OP is missing the line between the last vertex and the first one... but that's just a guess.
    gimbal2 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,101
    Rep Power
    4

    Default Re: 2D Polygon Help?

    Good one, you're probably right too. Wish the OP would explain what the problem is in so many words though.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: 2D Polygon Help?

    First, I suggest you do not duplicate Java JDK classes because it can become very confusing (e.g. Polygon). Second, why are you doing it because there is
    already a Polygon class that does what yours does. And the Graphics API has a draw(Shape) method. Since Polygon implements Shape, you're set. Now if you must roll your own, then please choose a different class name (e.g. MyPolygon).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    pikapika432 is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default Re: 2D Polygon Help?

    Figured it out on my own, thanks for the input though.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,949
    Rep Power
    4

    Default Re: 2D Polygon Help?

    Then you should share your solution so others may benefit. That's part of the responsibility of being a member of this forum.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,101
    Rep Power
    4

    Default Re: 2D Polygon Help?

    Meh "responsibility", I'd hope that there is a certain degree of willingness there.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Drag polygon
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 06:30 PM
  2. Polygon array?
    By AcousticBruce in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 06:38 PM
  3. Polygon won't show up
    By pizzadude223 in forum Java 2D
    Replies: 5
    Last Post: 08-09-2010, 03:51 AM
  4. How to use Polygon class?
    By lost_soul in forum New To Java
    Replies: 8
    Last Post: 04-25-2010, 12:48 AM
  5. How to copy a polygon
    By cassysumandak in forum New To Java
    Replies: 6
    Last Post: 10-07-2009, 12:37 AM

Posting Permissions

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