2017-05-04 3 views
0

2d配列に円を描く関数を検索しました。
私は、周りの数学的方程式を使うのが最善の方法であることを発見しました。このコードでは2d配列で完全な円を描く

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); 
         } 
        } 
       } 
      } 

の周りに円を描きます。助けてくれてありがとう。

第一EDIT:ヨッシVainshteinへ 最初イム感謝:私は条件に
i< ir + (unita[i1][j1].getAtkRange()*2)
を設定した場合、プログラムが半円形を作成するので、私は条件2を変更し、それが動作しますが、ない完璧。ここに画像があります:最初の画像を確認してください。

結論:今、それは条件2で動作します:
i<= ir + (unita[i1][j1].getAtkRange()*2)

+1

愚かな質問かもしれないが、なぜGraphics2D.drawOval()を使用しないでください – qry

+0

あなたはhav 'pos'が何であるかを教えてください。ループがなぜ早すぎるのかを知る方法はありません。 –

+0

グラフィック円/楕円形を描画し、コントロールや計算を行うための値が必要なだけです –

答えて

0

私はあなたのループ条件がバグを持っていると思うが、それは

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

はオフバイワイヤのように見えますが、 '<'から '<=' –