我写的代码是针对学校项目的,我仍然是java的新手,对于丑陋的编码感到遗憾。即时尝试使8-9随机椭圆出现在y = 0
和随机x间隔(固定值)但我的问题是它是否会产生随机x值并将椭圆放在那里但不会添加到y值,或者它们会掉到屏幕上但是当我希望它保持固定数字时它会不断改变x值。
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
public class animation extends Applet implements Runnable
{
Thread runner;
int xPos;
int y = 0;
int stop = 0;
Random rng = new Random();
public void start()
{
if(runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void run()
{
while(stop != 8)
{
for(int i = 0; i < 9; i++)
{
xPos = rng.nextInt(300);
stop++;
}
}
while(true)
{
y += 10;
repaint();
try{runner.sleep(250);}
catch (InterruptedException e) { }
}
}
public void paint(Graphics gr)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
gr.fillOval((xPos), y, 8, 8);
}
}
}
}