2017-05-04 25 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

看起来像是一个错误的问题,试着将'<'改成'<=' –