0

リンクされたリストに保存されている座標に基づいて三角形のポリゴンを描画する際に問題があります。System.out.printlninpaint 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
 }
4

2 に答える 2

0

これはスレッドの問題である可能性があります。このtriangles変数を使用するすべてのアクティビティが同期されていることを確認してください。

さらに良いことに、triangles変数を使用するすべてのアクティビティ(設定、取得、ペイント)をイベントディスパッチスレッドに制限します。

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        patternGenerator.setTriangles(...);
   }
}
于 2009-10-14T08:34:30.580 に答える
0

最後に、LIST を次のように宣言することで、この問題を解決しました。

public static List<Polygon> triangles = new LinkedList<Polygon>(); 
于 2009-10-14T14:06:26.020 に答える