我在根据存储在链表中的坐标绘制三角形多边形时遇到问题。System.out.println
当我使用inpaint component
方法检查链表元素时
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]);
}
}
它与读入的链表元素不相似
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]);
}
}
我想知道为什么会这样。public getTriangles
例如,在方法中读取的链表三角形中的 x 点是x[0]= 379, x[0]= 429, x[2]= 404,并且在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
}