用户按下 Jbutton 后随机时间后的 Java drawOval

发布于 2024-09-13 10:04:03 字数 489 浏览 0 评论 0原文

因此,当用户按下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-09-20 10:04:03

您可以使用 Thread 类中的 sleep 函数使程序等待随机时间。像这样的事情:

try{
Thread.sleep(PopUpTime);
}
catch(Exception e)
{}
// and then compute new points and repaint

You could use the sleep function from the Thread class to make the program wait for a random time. Something like this:

try{
Thread.sleep(PopUpTime);
}
catch(Exception e)
{}
// and then compute new points and repaint
心意如水 2024-09-20 10:04:03

问题在于你的逻辑:

if event is start button, then setup oval and timer and call repaint();

假设重绘是在设定的坐标处绘制椭圆形。

您可能应该这样做:

if (e.getSource() == startButton)  {
  drawOval = false;  // flag to repaint method to NOT display oval
  // setup timer 
  repaint();  // oval will not be drawn
else {
  // assuming timer has fired (which is a bit weak)
  x = ....;
  y = ...;
  drawOval = true;
  repaint();  // oval will be drawn.
}

您的 repaint() 方法将需要检查 drawOval 设置:

public void repaint() {
  if (drawOval) {
    // draw it
  } else {
    // may need to clear oval
  }

  // draw other stuff.
}

The problem is your logic:

if event is start button, then setup oval and timer and call repaint();

assumedly repaint is drawing your oval at the set coordinates.

You probably should do something like this:

if (e.getSource() == startButton)  {
  drawOval = false;  // flag to repaint method to NOT display oval
  // setup timer 
  repaint();  // oval will not be drawn
else {
  // assuming timer has fired (which is a bit weak)
  x = ....;
  y = ...;
  drawOval = true;
  repaint();  // oval will be drawn.
}

Your repaint() method will need to check the drawOval setting:

public void repaint() {
  if (drawOval) {
    // draw it
  } else {
    // may need to clear oval
  }

  // draw other stuff.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文