Domanda

Così, quando l'utente preme il mio JButton, raccoglie un tempo casuale, e dopo che il tempo, sarà disegnare un ovale sullo schermo. Tuttavia, con quello che ho adesso, si disegna il diritto ovale dopo aver premuto il pulsante. Voglio che appare dopo un tempo casuale.

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


   }
È stato utile?

Soluzione

Si potrebbe utilizzare la funzione sleep dalla classe Thread per rendere il programma attesa per un tempo casuale. Qualcosa di simile a questo:

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

Altri suggerimenti

Il problema è la logica:

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

riverniciare assumedly sta disegnando il vostro ovale alle coordinate impostati.

Probabilmente si dovrebbe fare qualcosa di simile:

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.
}

Il tuo metodo repaint () sarà necessario verificare l'impostazione drawOval:

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

  // draw other stuff.
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top