This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes draw different polygons from a arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "draw different polygons from a arraylist " Watch "draw different polygons from a arraylist " New topic
Author

draw different polygons from a arraylist

liulca ruggiero
Greenhorn

Joined: Nov 21, 2012
Posts: 15
greetings to all my problem I would like to draw polygons from an arraylist I wrote this code but does not work for any of you any thoughts on this 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.LinkedHashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Polygons 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 POLYGON_POINTS = 3;
int counter = 0;
int[][] x = new int[3][POLYGON_POINTS];
int[][] y = new int[3][POLYGON_POINTS];



for (POINTS pnt : POLYGON) {
x[counter/POLYGON_POINTS][counter%POLYGON_POINTS] = pnt.getX();
y[counter/POLYGON_POINTS][counter%POLYGON_POINTS] = pnt.getY();
counter++;
}
Polygon[] polygons = new Polygon[3];
for (int i = 0; i < polygons.length; i++) {
polygons[i] = new Polygon();
polygons[i] = new Polygon(x[i], y[i], POLYGON_POINTS);
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 Polygons());

frame.show();
}
}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
wouldn't you be better off keeping the conversation going here

http://stackoverflow.com/questions/13480107/draw-two-different-polygons-from-a-arraylist

where the 'code you wrote' was given to you.
liulca ruggiero
Greenhorn

Joined: Nov 21, 2012
Posts: 15
in fact this not the original code and modified it during processing but the result is not only hoped I wanted to ask a help thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Welcome to the Ranch

Please consider that you ought to point out both here and on SO that you are asking questions on several sites. Please consider whether it is courteous or not to whoever gave you that code to post it elsewhere without acknowledgement.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: draw different polygons from a arraylist
 
Similar Threads
Want confirmation that I am doing everything right
problem in threads(sun accesibility test)
A Hard Puzzle
Creating triangles using the Polygon class problem???
Applet - Graphing Quadratic Function Help