Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 37

Thread: draw 4 different polygons.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default draw 4 different polygons.

    hi my problem is to draw 4 different polygons thanks in advance


    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Polygon;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.ArrayList;
    import java.util.LinkedHashSet;
    import java.util.Set;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Polygons1 extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);


    Set<POINTS> POLYGON = new LinkedHashSet<POINTS>();


    //polygon 1 point 3
    POLYGON.add(new POINTS(1, 50, 20));
    POLYGON.add(new POINTS(1, 70, 30));
    POLYGON.add(new POINTS(1, 50, 40));


    //polygon 2 point 4
    POLYGON.add(new POINTS(2, 100, 20));
    POLYGON.add(new POINTS(2, 130, 30));
    POLYGON.add(new POINTS(2, 100, 40));
    POLYGON.add(new POINTS(2, 120, 30));


    //polygon 3 point 6
    POLYGON.add(new POINTS(3, 150, 20));
    POLYGON.add(new POINTS(3, 180, 30));
    POLYGON.add(new POINTS(3, 150, 40));
    POLYGON.add(new POINTS(3, 130, 20));
    POLYGON.add(new POINTS(3, 180, 30));
    POLYGON.add(new POINTS(3, 150, 50));


    //polygon 4 point 12
    POLYGON.add(new POINTS(4, 150, 20));
    POLYGON.add(new POINTS(4, 180, 30));
    POLYGON.add(new POINTS(4, 150, 40));
    POLYGON.add(new POINTS(4, 130, 20));
    POLYGON.add(new POINTS(4, 180, 30));
    POLYGON.add(new POINTS(4, 150, 50));
    POLYGON.add(new POINTS(4, 150, 20));
    POLYGON.add(new POINTS(4, 180, 30));
    POLYGON.add(new POINTS(4, 130, 60));
    POLYGON.add(new POINTS(4, 140, 50));
    POLYGON.add(new POINTS(4, 140, 30));
    POLYGON.add(new POINTS(4, 120, 30));







    int n = POLYGON.size() ;

    int[] x = new int[n];
    int[] y = new int[n];
    int counter = 0;

    for (POINTS pnt : POLYGON) {
    x[counter] = pnt.getX();
    y[counter++] = pnt.getY();


    Polygon[] polygons = new Polygon[4];
    for (int i = 0; i < polygons.length; i++) {
    polygons[i] = new Polygon();
    polygons[i] = new Polygon(x, y, n);
    g.setColor(Color.RED);
    g.drawPolygon(polygons[i]);
    }} }





    public class POINTS {

    private int ID;
    private int X;
    private int Y;

    public POINTS(
    int ID, int X, int Y) {
    this.ID = ID;
    this.X = X;
    this.Y = Y;
    }

    public int getX() {
    return X;
    }

    public int getY() {
    return Y;
    }

    public int getID() {
    return ID;
    }
    }

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("Polygons");
    frame.setSize(550, 550);
    frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    Container contentPane = frame.getContentPane();
    contentPane.add(new Polygons1());

    frame.show();
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    Can you explain the problem you are having?
    If you are getting any errors, please copy the full text and paste it here.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    my problem is how to draw polygons other than 4 <id> a arraylist containing all points of the polygons the code that I wrote I just draw a polygon with 25 points
    example
    polygon 1 ...... id = 1 .... points .. .....x {50,70,50}, y = {20,30,40};
    polygon 2 ...... id = 2 .... points .... .. x {100,130,100,120}, y = {20,30,40,30};
    polygon 3 ...... id = 3 .... points .... .. x {,,,,,,,,,,,,,,, }, y = {,,,,,,,,,,,,,,,,,,,,,};
    polygon 4 ...... id = 4 .... points .... .. x {,,,,,,,,,,,,,,,,}, y = {,,,,,,,,,,,,,,,,,,,,,};

    I thank you in advance

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hi this is the code formatted I hope I was clear I thank you in advance


    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.Polygon;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.ArrayList;
    import java.util.LinkedHashSet;
    import java.util.Set;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class Polygons2 extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);


    Set<POINTS> POLYGON = new LinkedHashSet<POINTS>();


    //polygon 1 point 3
    POLYGON.add(new POINTS(1, 50, 20));
    POLYGON.add(new POINTS(1, 70, 30));
    POLYGON.add(new POINTS(1, 50, 40));


    //polygon 2 point 4
    POLYGON.add(new POINTS(2, 100, 20));
    POLYGON.add(new POINTS(2, 130, 30));
    POLYGON.add(new POINTS(2, 100, 40));
    POLYGON.add(new POINTS(2, 120, 30));


    //polygon 3 point 6
    POLYGON.add(new POINTS(3, 150, 20));
    POLYGON.add(new POINTS(3, 180, 30));
    POLYGON.add(new POINTS(3, 150, 40));
    POLYGON.add(new POINTS(3, 130, 20));
    POLYGON.add(new POINTS(3, 180, 30));
    POLYGON.add(new POINTS(3, 150, 50));


    //polygon 4 point 12
    POLYGON.add(new POINTS(4, 150, 20));
    POLYGON.add(new POINTS(4, 180, 30));
    POLYGON.add(new POINTS(4, 150, 40));
    POLYGON.add(new POINTS(4, 130, 20));
    POLYGON.add(new POINTS(4, 180, 30));
    POLYGON.add(new POINTS(4, 150, 50));
    POLYGON.add(new POINTS(4, 150, 20));
    POLYGON.add(new POINTS(4, 180, 30));
    POLYGON.add(new POINTS(4, 130, 60));
    POLYGON.add(new POINTS(4, 140, 50));
    POLYGON.add(new POINTS(4, 140, 30));
    POLYGON.add(new POINTS(4, 120, 30));







    int n = POLYGON.size();

    int[] x = new int[n];
    int[] y = new int[n];
    int counter = 0;

    for (POINTS pnt : POLYGON) {
    x[counter] = pnt.getX();
    y[counter++] = pnt.getY();


    Polygon[] polygons = new Polygon[4];
    for (int i = 0; i < polygons.length; i++) {
    polygons[i] = new Polygon();
    polygons[i] = new Polygon(x, y, n);
    g.setColor(Color.RED);
    g.drawPolygon(polygons[i]);
    }
    }
    }

    public class POINTS {

    private int ID;
    private int X;
    private int Y;

    public POINTS(
    int ID, int X, int Y) {
    this.ID = ID;
    this.X = X;
    this.Y = Y;
    }

    public int getX() {
    return X;
    }

    public int getY() {
    return Y;
    }

    public int getID() {
    return ID;
    }
    }

    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("Polygons");
    frame.setSize(550, 550);
    frame.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    Container contentPane = frame.getContentPane();
    contentPane.add(new Polygons1());

    frame.show();
    }
    }

    --- Update ---

    hello
    I'm working on this solution but can not figure out how to draw the polygons
    that is how to pass the points to the polygons you asking for your help
    I thank you in advance.


    Map<Integer,List<coordinates>> PolygonsByID =
    new LinkedHashMap<Integer,List<coordinates>>();


    for (POINTS n : POLYGON) {
    List PolygonsForID = PolygonsByID.get(n.getID());
    if (PolygonsForID == null) {
    PolygonsForID = new ArrayList<coordinates>();
    PolygonsByID.put(n.getID(), PolygonsForID);
    }
    PolygonsForID.add(n);



    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    how to draw four different polygons
    What happens if you call drawPolygon() for each polygon you want drawn?
    I only see one call to drawPolygon() in the posted code. Because of the poor formatting I can't tell if there are any loops. There should not be more than one } on a line.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    --- Update ---

    The code you posted does not compile without errors. There is a class definition missing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    I'm sorry this is the correct code
    I thank you in advance help me

    <
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class Polygons2 extends JPanel {
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
     
     
            Set<POINTS> POLYGON = new LinkedHashSet<POINTS>();
     
     
    //polygon 1 point 3
            POLYGON.add(new POINTS(1, 50, 20));
            POLYGON.add(new POINTS(1, 70, 30));
            POLYGON.add(new POINTS(1, 50, 40));
     
     
    //polygon 2 point 4
            POLYGON.add(new POINTS(2, 100, 20));
            POLYGON.add(new POINTS(2, 130, 30));
            POLYGON.add(new POINTS(2, 100, 40));
            POLYGON.add(new POINTS(2, 120, 30));
     
     
    //polygon 3 point 6
            POLYGON.add(new POINTS(3, 150, 20));
            POLYGON.add(new POINTS(3, 180, 30));
            POLYGON.add(new POINTS(3, 150, 40));
            POLYGON.add(new POINTS(3, 130, 20));
            POLYGON.add(new POINTS(3, 180, 30));
            POLYGON.add(new POINTS(3, 150, 50));
     
     
    //polygon 4 point 12
            POLYGON.add(new POINTS(4, 150, 20));
            POLYGON.add(new POINTS(4, 180, 30));
            POLYGON.add(new POINTS(4, 150, 40));
            POLYGON.add(new POINTS(4, 130, 20));
            POLYGON.add(new POINTS(4, 180, 30));
            POLYGON.add(new POINTS(4, 150, 50));
            POLYGON.add(new POINTS(4, 150, 20));
            POLYGON.add(new POINTS(4, 180, 30));
            POLYGON.add(new POINTS(4, 130, 60));
            POLYGON.add(new POINTS(4, 140, 50));
            POLYGON.add(new POINTS(4, 140, 30));
            POLYGON.add(new POINTS(4, 120, 30));
     
     
           Map<Integer,List<POINTS>> PolygonsByID =
           new LinkedHashMap<Integer,List<POINTS>>();
     
     
            for (POINTS n : POLYGON) {
            List PolygonsForID = PolygonsByID.get(n.getID());
            if (PolygonsForID == null) {
            PolygonsForID = new ArrayList<POINTS>();
            PolygonsByID.put(n.getID(), PolygonsForID);
        }
            PolygonsForID.add(n);
     
     
     
     
     
        }
       }
     
        public class POINTS {
     
            private int ID;
            private int X;
            private int Y;
     
            public POINTS(
                    int ID, int X, int Y) {
                this.ID = ID;
                this.X = X;
                this.Y = Y;
            }
     
            public int getX() {
                return X;
            }
     
            public int getY() {
                return Y;
            }
     
            public int getID() {
                return ID;
            }
        }
     
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setTitle("Polygons2");
            frame.setSize(550, 550);
            frame.addWindowListener(new WindowAdapter() {
     
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            Container contentPane = frame.getContentPane();
            contentPane.add(new Polygons2());
     
            frame.show();
        }
    }
    >

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    Where is the call to the draw method for drawing the polygon?

    If you want to draw 4 different polygons, where are the 4 containers that hold the points for the polygons? It looks like all the points are added to just one container, not to 4 different containers.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    container is one where all the points then group the different polygons based on their <id> and eventually draw polygons can be such a thing or are on the wrong track
    I'm working on this code but I can not move the points to the polygons and then draw them as you can continue asking for your help and thank you in advance

    <
            Map<Integer,List<POINTS>> PolygonsByID =
            new LinkedHashMap<Integer,List<POINTS>>();
            for (POINTS n : POLYGON) {
            List PolygonsForID = PolygonsByID.get(n.getID());
            if (PolygonsForID == null) {
            PolygonsForID = new ArrayList<POINTS>();
            PolygonsByID.put(n.getID(), PolygonsForID);
        }
            PolygonsForID.add(n);
     
            //At this point I would like to draw the polygons
            //in PolygonsForID but do not know how
            //you asking for your help
     
        }
    >

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    can not move the points to the polygons
    Please explain what the problem is.
    Where does the code call the drawPolygon() method to draw a polygon?
    There are two versions of the method. You need to write some code that uses one of them to draw a polygon(). When you can draw a polygon then you need to work on separating the points that are in the POLYGON container into 4 separate places that can be used to draw the 4 polygons you want to draw.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    the question is
    1) how do I count the number of points for each polygon;
    2) How do I switch points to polygons
    I thank you in advance

    <
            Map<Integer,List<POINTS>> PolygonsByID =
            new LinkedHashMap<Integer,List<POINTS>>();
            for (POINTS n : POLYGON) {
            List PolygonsForID = PolygonsByID.get(n.getID());
            if (PolygonsForID == null) {
            PolygonsForID = new ArrayList<POINTS>();
            PolygonsByID.put(n.getID(), PolygonsForID);
        }
            PolygonsForID.add(n);
     
     
            Polygon[] polygons = new Polygon[4];
            for (int i = 0; i < polygons.length; i++) {
            polygons[i] = new Polygon();
            polygons[i] = new Polygon(new int[] {.........},new int[] {.............}, .......);
            g.setColor(Color.RED);
            g.fillPolygon(polygons[i]);
     
        }
        }
    >

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    How do I count the number of points for each polygon;
    How many points are there for each polygon? How do you find those points in the POLYGON collection? Count them as they are added to the arrays used to build the Polygon object and use that count when you create the Polygon object. Then reset the count to 0 and start over filling the arrays for the next Polygon object.

    How do I switch points to polygons
    The Polygon class's constructor takes arrays of points to create the Polygon object.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I'm working on this solution but I error
    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 50, Size: 1
    Thanks for your patience

    <
            Map<Integer,List<POINTS>> PolygonsByID =
            new LinkedHashMap<Integer,List<POINTS>>();
            for (POINTS n : POLYGON) {
            List PolygonsForID = PolygonsByID.get(n.getID());
            if (PolygonsForID == null) {
            PolygonsForID = new ArrayList<POINTS>();
            PolygonsByID.put(n.getID(), PolygonsForID);
        }
            PolygonsForID.add(n);
     
     
              int num = PolygonsForID.size();
              int[] x =  (int[]) PolygonsForID.get(n.getX());
              int[] y = (int[]) PolygonsForID.get(n.getY());
     
              Polygon[] polygons = new Polygon[4];
              for (int i = 0; i < polygons.length; i++) {
              polygons[i] = new Polygon();
              polygons[i] = new Polygon(new int[] {x[i]},new int[] {y[i]}, num);
              g.setColor(Color.RED);
              g.fillPolygon(polygons[i]);
     
        }
        }
    >

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    What line did the error happen on?

    Before trying to write any more code, you need to stop and do some design work on what the code needs to do. The posted code does not do what I think the program needs to do.

    Given that the points for the 4 polygons are in a collection: POLYGON, work out the steps to get the points for each ploygon out of that collection and into 4 separate Polygon objects.

    Make a list of the steps (in pseudo code) the program needs to do to create the 4 Polygon objects.
    When the logic for that is done, then write the code to do it.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    asking for your help I do not understand do wrong I'm working on this code can not spend points to individual polygons could get some help I thank you in advance.

    <
            Map<Integer,List<POINTS>> PolygonsByID =
            new LinkedHashMap<Integer,List<POINTS>>();                
            for (POINTS n : POLYGON) {
            List PolygonsForID = PolygonsByID.get(n.getID());
            if (PolygonsForID == null) {
            PolygonsForID = new ArrayList<POINTS>();
            PolygonsByID.put(n.getID(), PolygonsForID);
            Collections.sort(PolygonsForID);                         
            }        
            PolygonsForID.add(n);
     
            }           
            for(Map.Entry<Integer,List<POINTS>> count : PolygonsByID.entrySet()) {
            int np =  count.getValue().size();
            //System.out.println("polygon : " + count.getKey() + " points : " + np);
     
            int[] x = new int[25];
            int[] y = new int[25];
            int counter = 0;
     
            for (POINTS pnt : POLYGON) {
            x[counter] = pnt.getX();
            y[counter] = pnt.getY();
            counter++;
           }
             Polygon[] polygons = new Polygon[4];
             for (int i = 0; i < polygons.length; i++) {
             polygons[i] = new Polygon();
             polygons[i] = new Polygon(x, y, np);
             g.setColor(Color.RED);
             g.drawPolygon(polygons[i]);
         }
         }  
    >

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    What are the steps to make 4 Polygons from the points contained in the POLYGON container?
    Here is one way it could be done:

    Define array to hold Polygons
    Initialize ptr to the above array to 0
    initialize polygon id number
    begin loop
    get next point from POLYGON
    is this point in current polygon?
    yes - save point's x and y values
    no- create new Polygon object with above saved points; increment polygon id number and array ptr
    end loop


    Now the array holds 4 Polygon objects that can be looped through to be drawn
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I'm sorry but I do not know how I solve this problem you can see some examples thank you in advance.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    I just posted pseudo code for one way to solve your problem.
    Look at the lines of pseudo code, one at a time and if you don't understand how to do that step, please ask a question about that step.
    The first step: define an array - there is an example in the posted code:
     Polygon[] polygons = new Polygon[4];
    What about the rest of the steps?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I'm sorry but I do not understand what code I have to watch.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    Many of the steps the program needs to do were listed in post#15
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    I followed the steps # 15 but the problem is that I can not understand
    I can get some help or some example thank you in advance

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    Which step(s) are you having problems with?
    Post the new code you have
    and post the questions you have about the listed steps.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    this is the code and I get this error I can get some help I thank you in advance.

    <
     
            Map<Integer,List<POINTS>> PolygonsByID = new LinkedHashMap<Integer,List<POINTS>>();
     
            for (POINTS n : POLYGON) {
            List PolygonsForID = PolygonsByID.get(n.getID());
            if (PolygonsForID == null) {
            PolygonsForID = new ArrayList<POINTS>();
            PolygonsByID.put(n.getID(), PolygonsForID);
            Collections.sort(PolygonsForID);                         
            }        
            PolygonsForID.add(n);
     
           }
            for(Map.Entry<Integer,List<POINTS>> count : PolygonsByID.entrySet()) {
            int np =  count.getValue().size();
            System.out.println("polygon : " + count.getKey() + " points : " + np);
            int num_poly = 0;
            Polygon[] polygons = new Polygon[4];
            for (POINTS pnt : POLYGON) {
            polygons[count.getKey()] = new Polygon(new int[] {pnt.getX()},new int[] {pnt.getY()},np);
            num_poly ++;
            g.setColor(Color.RED);
            g.drawPolygon(polygons[count.getKey()]);
        }
       }
     
     
      Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: npoints > xpoints.length || npoints > ypoints.length
     
    >

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    The posted code does NOT follow the steps listed in post #15.
    The first step was: Define array to hold Polygons
    The code to do that was shown in post #17.

    In the code in post #22, the array is NOT created first. It is deep inside some loops.


    If you are not going to follow the steps I listed in post #15, Can you make a list of the steps you want to follow. Writing code BEFORE you have a good design is a waste of time.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Junior Member
    Join Date
    Nov 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: draw 4 different polygons.

    hello
    the procedure described in post # 15. is correct but I can not write the code
    I'm not very experienced you asking for your help I thank you in advance

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: draw 4 different polygons.

    I suggested a technique in post#15. Try to do the steps listed there one at a time.
    I've shown the first step. Now work on the second, third, etc
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with Draw Program
    By kaso2793 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 23rd, 2012, 02:12 PM
  2. Re: Draw labyrinth ( I need help)
    By tubluforu in forum Java Theory & Questions
    Replies: 1
    Last Post: January 6th, 2012, 11:08 PM
  3. Draw labyrinth ( I need help)
    By ramin in forum Loops & Control Statements
    Replies: 2
    Last Post: January 3rd, 2012, 05:27 PM
  4. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 02:29 AM