Java 在二维阵列中绘制完整的圆
我搜索了一个函数,该函数允许我在二维数组中绘制一个圆。Java 在二维阵列中绘制完整的圆,java,arrays,swing,Java,Arrays,Swing,我搜索了一个函数,该函数允许我在二维数组中绘制一个圆。 我发现最好的方法是使用周长的数学方程,所以我使用了它 有了这个代码,我只得到了1/4的圆,我不知道如何“完成”它 目标:在按下的按钮周围画一个圆圈 System.out.println("\nwait button pression\n"); while(true){ if(premuto.get()==true){
我发现最好的方法是使用周长的数学方程,所以我使用了它 有了这个代码,我只得到了1/4的圆,我不知道如何“完成”它
目标:在按下的按钮周围画一个圆圈
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);
}
}
}
}
System.out.println(“\n等待按钮按下\n”);
while(true){
if(premuto.get()==true){
如果(位置!=null){
如果(位置长度()!=0){
打破
}
}
}
}
试一试{
睡眠(20);
}捕捉(中断异常e){
e、 printStackTrace();
}
i1=this.getIntI()//获取按下按钮的坐标
j1=this.getIntJ();//"
if(unita[i1][j1]!=null){//控制posistion是否为空
int ir=i1 unita[i1][j1].getAtkRange();//计算正方形的00点。请参见图像“数组”
int jr=j1 unita[i1][j1].getAtkRange();
ImageIcon=newImageIcon(MyFrame.class.getResource(“test.png”);
对于(inti=ir;i我认为您的循环条件有一个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 (...) {
...
}
}
}
for(inti=ir;i
你从ir
开始,但是你想做(unita[i1][j1].getAtkRange()*2)
步骤,所以条件应该是i
也许是个愚蠢的问题,但为什么不使用Graphics2D.drawOval()您没有告诉我们什么是pos
,因此无法知道循环过早退出的原因。只是因为它绘制了一个图形圆/椭圆形,并且我需要用于制作控件或计算的值。绘制圆是一个经典问题,并且已经非常有效地解决了。对不起,patrick,pos是一个包含位置(I、j或x、y)的字符串点击btnArray.ehy上的按钮。谢谢!现在它似乎工作正常,但并不完美。请阅读编辑的问题以了解更多详细信息。看起来像一个问题,请尝试更改