用户按下 Jbutton 后随机时间后的 Java drawOval
因此,当用户按下 JButton 时,它会选择一个随机时间,在该时间之后,它将在屏幕上绘制一个椭圆形。然而,就我现在所拥有的,它会在按下按钮后立即绘制椭圆形。我希望它在随机时间后出现。
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startButton)
{
popUpTime = random.nextInt(5000);
timer = new Timer(popUpTime, this);
x = random.nextInt(400) + 70;
y = random.nextInt(400) + 100;
points[current++] = new Point(x, y);
timer.start();
start();
repaint();
}
}
So when the user presses my JButton, it picks a random time, and after that time, it will draw a oval to the screen. However, with what I have now, it draws the oval right after the button is pressed. I want it to appear after a random time.
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startButton)
{
popUpTime = random.nextInt(5000);
timer = new Timer(popUpTime, this);
x = random.nextInt(400) + 70;
y = random.nextInt(400) + 100;
points[current++] = new Point(x, y);
timer.start();
start();
repaint();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Thread 类中的 sleep 函数使程序等待随机时间。像这样的事情:
You could use the sleep function from the Thread class to make the program wait for a random time. Something like this:
问题在于你的逻辑:
假设重绘是在设定的坐标处绘制椭圆形。
您可能应该这样做:
您的 repaint() 方法将需要检查 drawOval 设置:
The problem is your logic:
assumedly repaint is drawing your oval at the set coordinates.
You probably should do something like this:
Your repaint() method will need to check the drawOval setting: