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();
  }


   }
4

2 に答える 2

0

Thread クラスの sleep 関数を使用して、プログラムをランダムな時間待機させることができます。このようなもの:

try{
Thread.sleep(PopUpTime);
}
catch(Exception e)
{}
// and then compute new points and repaint
于 2010-08-11T06:49:00.740 に答える
0

問題はあなたの論理です:

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.
}
于 2010-08-11T05:54:47.870 に答える