2017-05-04 28 views
0

我搜索了一個讓我在二維數組中繪製圓的函數。
我發現最好的方法是使用周長的數學公式,所以我用它。在二維數組中繪製完整的圓

math equation

有了這個代碼,我只得到一個圓的1/4,我不知道如何「完成」了。

array
目的:周圍繪製按下的按鈕

  System.out.println("\nwait button pression\n"); 
      while(true){ 
       if(premuto.get()==true){ 
        if(pos!=null){ 
         if(pos.length()!=0){ 
          break; 
         } 
        } 
       } 
      } 
      try { 
       Thread.sleep(20); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      i1=this.getIntI();//get the coordinates of the button pressed 
      j1=this.getIntJ();// " 

      if(unita[i1][j1]!=null){ //control if the posistion isnt empty 

       int ir=i1-unita[i1][j1].getAtkRange();//calculate the 00 point of the square .see image "array" 
       int jr=j1-unita[i1][j1].getAtkRange(); 

       ImageIcon icon= new ImageIcon(MyFrame.class.getResource("test.png")); 

       for (int i=ir ; i<(ir+(unita[i1][j1].getAtkRange()*2)) ; i++){ 
        for (int j=jr ; j<(jr+unita[i1][j1].getAtkRange()*2) ;j++){//1st edit, changed the condition 
         if(((i - i1) * (i - i1) + (j - j1) * (j - j1)) <= unita[i1][j1].getAtkRange() * unita[i1][j1].getAtkRange()){ 
          btnArray[i][j].setIcon(icon); 
         } 
        } 
       } 
      } 

了一圈,我會希望你明白。謝謝您的幫助。

月1日編輯: 第一IM要去感謝貝納Vainshtein:如果我設定的條件
i< ir + (unita[i1][j1].getAtkRange()*2)
程序創建了半圈,所以我改變了2條件和它的作品,但不完全。這是一張圖片:查看第一張圖片。

結論:現在用2個工程條件:
i<= ir + (unita[i1][j1].getAtkRange()*2)

+1

也許一個愚蠢的問題,但爲什麼不使用Graphics2D.drawOval() – qry

+0

你hav en't告訴我們'pos'是什麼,所以沒有辦法知道爲什麼循環過早退出。 –

+0

只是因爲它繪製了一個圖形圈/橢圓形,我需要用於製作控制或計算的值 –

回答

0

我覺得你的循環狀況有個bug,應該不會是

for (int i=ir ; i< ir + (unita[i1][j1].getAtkRange()*2) ; i++){ 
    for (int j=jr ; j< jr + (unita[i1][j1].getAtkRange()*2) ; j++){ 
      if (...) { 
       ... 
      } 
    } 
} 

你開始ir但你想使(unita[i1][j1].getAtkRange()*2)步驟,所以條件應該是i< ir + (unita[i1][j1].getAtkRange()*2)

+0

ehy,謝謝!現在看起來有效但並不完美。閱讀編輯後的問題以獲取更多詳情。 –

+0

看起來像是一個錯誤的問題,試着將'<'改成'<=' –