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