Results 1 to 7 of 7
  1. #1
    liulca is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default polygon problem help

    hello
    I would like to draw 4 different polygons each polygon has its own "id" and its points in an arraylist I'm working on this code but I can not draw polygons asking for your help 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.Collections;
    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, 250, 20));
    POLYGON.add(new POINTS(3, 280, 30));
    POLYGON.add(new POINTS(3, 250, 40));
    POLYGON.add(new POINTS(3, 230, 20));
    POLYGON.add(new POINTS(3, 280, 30));
    POLYGON.add(new POINTS(3, 250, 50));


    //polygon 4 point 12
    POLYGON.add(new POINTS(4, 350, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 350, 40));
    POLYGON.add(new POINTS(4, 330, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 350, 50));
    POLYGON.add(new POINTS(4, 350, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 330, 60));
    POLYGON.add(new POINTS(4, 340, 50));
    POLYGON.add(new POINTS(4, 340, 30));
    POLYGON.add(new POINTS(4, 320, 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);
    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 n = 25 ;
    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, np);
    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("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();
    }
    }

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: polygon problem help

    "but I can not draw polygons " is a little too short as problem description, what do you expect, what happens exactly (output?)
    Please use the forum code tags around your code.
    I like likes!

  3. #3
    liulca is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: polygon problem help

    Please use the forum tags around your code code.
    I do not understand

  4. #4
    liulca is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: polygon problem help

    Java Code:

    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.Collections;
    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, 250, 20));
    POLYGON.add(new POINTS(3, 280, 30));
    POLYGON.add(new POINTS(3, 250, 40));
    POLYGON.add(new POINTS(3, 230, 20));
    POLYGON.add(new POINTS(3, 280, 30));
    POLYGON.add(new POINTS(3, 250, 50));


    //polygon 4 point 12
    POLYGON.add(new POINTS(4, 350, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 350, 40));
    POLYGON.add(new POINTS(4, 330, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 350, 50));
    POLYGON.add(new POINTS(4, 350, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 330, 60));
    POLYGON.add(new POINTS(4, 340, 50));
    POLYGON.add(new POINTS(4, 340, 30));
    POLYGON.add(new POINTS(4, 320, 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);
    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 n = 25 ;
    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, np);
    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("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();
    }
    }

  5. #5
    liulca is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: polygon problem help

    Java Code:
    <
    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.Collections;
    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, 250, 20));
    POLYGON.add(new POINTS(3, 280, 30));
    POLYGON.add(new POINTS(3, 250, 40));
    POLYGON.add(new POINTS(3, 230, 20));
    POLYGON.add(new POINTS(3, 280, 30));
    POLYGON.add(new POINTS(3, 250, 50));
    
    
    //polygon 4 point 12
    POLYGON.add(new POINTS(4, 350, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 350, 40));
    POLYGON.add(new POINTS(4, 330, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 350, 50));
    POLYGON.add(new POINTS(4, 350, 20));
    POLYGON.add(new POINTS(4, 380, 30));
    POLYGON.add(new POINTS(4, 330, 60));
    POLYGON.add(new POINTS(4, 340, 50));
    POLYGON.add(new POINTS(4, 340, 30));
    POLYGON.add(new POINTS(4, 320, 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);
    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 n = 25 ;
    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, np);
    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("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();
    }
    } 
    >

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: polygon problem help

    Code Conventions for the Java Programming Language: Contents -- especially the part about indenting code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    liulca is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Default Re: polygon problem help

    hello
    excuse me where am I wrong


    Java Code:
    <
    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.Collections;
    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, 250, 20));
            POLYGON.add(new POINTS(3, 280, 30));
            POLYGON.add(new POINTS(3, 250, 40));
            POLYGON.add(new POINTS(3, 230, 20));
            POLYGON.add(new POINTS(3, 280, 30));
            POLYGON.add(new POINTS(3, 250, 50));
    
    
    //polygon 4 point 12
            POLYGON.add(new POINTS(4, 350, 20));
            POLYGON.add(new POINTS(4, 380, 30));
            POLYGON.add(new POINTS(4, 350, 40));
            POLYGON.add(new POINTS(4, 330, 20));
            POLYGON.add(new POINTS(4, 380, 30));
            POLYGON.add(new POINTS(4, 350, 50));
            POLYGON.add(new POINTS(4, 350, 20));
            POLYGON.add(new POINTS(4, 380, 30));
            POLYGON.add(new POINTS(4, 330, 60));
            POLYGON.add(new POINTS(4, 340, 50));
            POLYGON.add(new POINTS(4, 340, 30));
            POLYGON.add(new POINTS(4, 320, 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);
                    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 n = 25;
                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[1];
                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]);
    
                }
    
            }
    
        }
    
        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();
        }
    }
    
    
    >
    Last edited by liulca; 12-11-2012 at 10:12 PM.

Similar Threads

  1. polygon growing
    By ericT in forum New To Java
    Replies: 1
    Last Post: 09-24-2011, 08:35 PM
  2. Drag polygon
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 06:30 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
  •