绘制组件未根据预期坐标打印多边形
我在根据存储在链接列表中的坐标绘制三角形多边形时遇到问题。当我在paint组件
方法中使用System.out.println
检查链表元素时,
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
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是线程问题。确保使用此三角形变量的所有活动都是同步的。
更好的是,将三角形变量的所有活动(设置、获取和绘制)限制在事件调度线程中,如下所示:
This could be a threading problem. Make sure all activity with this triangles variable is synchronized.
Better still, restrict all activity (setting and getting and painting) with the triangles variable to the event dispatch thread, as so:
最后我通过将 LIST 声明为以下方式解决了这个问题:
Finally I solved this problem by declaring the LIST as: