我有一个画布设置,我试图找出一个方程式,使圆圈从原点开始出现,然后在随机位置增加边界。我在绘画中绘制了这个图像,以帮助解释我想要发生的事情
这是从右上角和左上角绘制两个爆炸的循环:
for (iii = 0; iii <= 6000; iii++)
{
graphics.setColor(colors[c1]);//same color as comet
a1 = ((int)(Math.random() * iii/10));
a2 = ((int)(Math.random() * iii/10 ));
a3 = ((int) (Math.random()* 15 + 1));
graphics.drawOval(a1, a2, a3, a3);
graphics.setColor(colors[c2]);//same color as comet
a2 = ((int)(Math.random() * iii/10 ));
a1 = 600 - ((int)(Math.random() * iii/10));
a3 = ((int) (Math.random()* 20 + 1));
graphics.drawOval(a1, a2, a3, a3);
try
{
TimeUnit.NANOSECONDS.sleep(1);///change to higher #
} // end try
catch (Exception e)
{
System.out.println("Exception caught");
} // end catch
以下是:
的图片我不是要求别人为我编写代码,我只是无法弄清楚如何去做,并且可以在正确的方向上使用一点点。
答案 0 :(得分:0)
a1 = screenWidth - (int)((Math.random()-0.5)*iii/10);
...
graphics.drawOval(a1-a3/2, a2-a3/2, a3, a3);
答案 1 :(得分:0)
继承人我最终做的事情:
for (iii = 0; iii <= 6000; iii++)
{
//random sized, random colored circles explosion
c3 = ((int)(Math.random()*8));
graphics.setColor(Color.BLACK);
a1 = 300-iii/10 +(2*((int)(Math.random()*iii)))/10;//x coordinate
a2 = 300-iii/10 +(2*((int)(Math.random()*iii)))/10;//y coordinate
a3 = ((int) (Math.random()* 15 + 1)); //size (length = width)
graphics.fillOval(a1, a2, a3, a3); //center of circle black
graphics.setColor(colors[c3]); //randomly selected color
graphics.drawOval(a1, a2, a3, a3); //outline of circle
try
{
TimeUnit.MILLISECONDS.sleep(3);
} // end try
catch (Exception e)
{
System.out.println("Exception caught");
} // end catch
}//end for center explosions
答案 2 :(得分:0)
public void explosion(final float XCenter, final float YCenter, final int rad) {
new Thread(new Runnable() {
@Override
public void run() {
Random rand = new Random();
for (int i = 0; i <= rad; i++) {
int degree = rand.nextInt(359);
int x = XCenter + (float) (Math.cos(degree * 3.141592 / 180) * i);
int y = YCenter + (float) (Math.sin(degree * 3.141592 / 180) * i);
int size = ((int) (Math.random() * 15 + 1));
int color = Color.rgb(rand.nextInt(254), rand.nextInt(254), rand.nextInt(254));
graphics.setColor(color);
graphics.drawOval(x, y, size, size);
try {
Thread.sleep(1);
} catch (Exception e) {
}
}
}
}).start();
}