I have problem drawing the triangle polygons based on the coordinates which are stored in linked list. When I checked the linked list elements using System.out.println
in paint component
method
Public void paintComponent (Graphics g) {
...
for (Polygon p : triangles) {
g2.drawPolygon(triangle);
... // print the elements of triangles
System.out.println(p.xpoints[0]);
System.out.println(p.xpoints[1]);
System.out.println(p.xpoints[2]);
}
}
it is not similar with the linked list elements read in
public void getTriangles (){
.....
while (overlap)
{
...
}
for (Polygon p: triangles){
... //print the elements of triangles
System.out.println(p.xpoints[0]);
System.out.println(p.xpoints[1]);
System.out.println(p.xpoints[2]);
}
}
I was wondering WHY this happened. e.g. the x points in linked list triangles read in public getTriangles
method are x[0]= 379, x[0]= 429, x[2]= 404, and in paintComponent(Graphics g)
x[0]= 249, x[0]= 299, x[2]= 274
public class patternGenerator extends JPanel {
private int n = 10;
private int[] xCoord = new int[100];
private int[] yCoord = new int[100];
private List<Polygon> triangles = new LinkedList<Polygon>();
public patternGenerator () {
....
getTriangles();
}
public void getTriangles (){
boolean overlap = true; //overlap
/* This LOOP check for overlapping triangle.
* It will erase previous elements in linked list which are overlapping.
* The triangles will only be painted when there is no overlapping triangles
*/
while(overlap) {
int MAX_WIDTH = 600;
int MAX_HEIGHT = 600;
int sizeCombination2a [] = {10,20};
//remove previous drawn triangles
if(triangles.size()>1) {
Polygon[] triArray = triangles.toArray(new Polygon[triangles.size()]);
for (Polygon p:triArray)
triangles.remove (p);
}
//create new triangles
for(int i = 0; i < numTriangles; i++) { //generate 10 triangles {
int xWidth = sizeCombination2a[generator.nextInt(sizeCombination2a.length)]
int yHeight= sizeCombination2a[generator.nextInt(sizeCombination2a.length)];
xCoord[0] = generator.nextInt(MAX_WIDTH);
xCoord[1] = (int) (xCoord[0] - xWidth);
xCoord[2] = (int) (xCoord[1] + (xWidth/2));
yCoord[0] = generator.nextInt(MAX_WIDTH);
yCoord[1] = yCoord[0];
yCoord[2] = (int) (yCoord[1] - yHeight);
triangles.add( new Polygon(xCoord,yCoord, 3));
}
boolean results = checkOverlapAxis(aesParameter,aesLevel);
if(results)
overlap = false;
}//end while (overlap)
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(1)); // set the thickness of polygon line
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));
g2.setPaint(Color.black);//set the polygon line
for (Polygon triangle : triangles)
g2.drawPolygon(triangle);
}//end Paint
}