3 Replies - 1947 Views - Last Post: 14 March 2013 - 06:45 PM Rate Topic: -----

#1 hwoarang69   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

how to restart applet without closing it

Posted 14 March 2013 - 04:45 PM

ok so i kind of know how to restart but the problem is that when ever i restart my animation get faster and faster.

so i have main.java and alot of other objects. so one class for player one for player one for background etc...

public class main exteds JApplet actionlistener
{
  public void init()
  {
    //create window stuff
  }

  public void start()
  {
    player_class = new player1();
    //create all classes

    timer_class = new Timer(10, this);
    timer_class.start();
  }

  public void actionPerformed(ActionEvent e)
  {
    //all collsion stuff go in here
      repaint();
  }

  public void paint(graphics g)
  {
   //paint here
  }

  public void stop(){} //i dont use this method. well i dont know how to

  public void mousePressed(MouseEvent e)
  {
  //restart button here
    if(...)
    {
      if(...)
      {
       //if user hit restart button
       //i am calling start() method again so it restart
       start();   //--------------this is how i am restarting
      }
    }
  }


}



i think what is happing is that when i call start(); method at bottom it double my timer.
so 1st my timer is 10 than restart now its 20 ... and so on. that why my animation get faster and faster?

any idea how can i fix this issue. or is there a better logic to restart?

Is This A Good Question/Topic? 0
  • +

Replies To: how to restart applet without closing it

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to restart applet without closing it

Posted 14 March 2013 - 05:12 PM

Worst than that you end up with two Timer
do a timer_class.stop() before calling start()
or even better in your start() method

  if(timer_class == null) {
     timer_class = new Timer(10, this);
  else {
     timer_class.stop();
     timer_class.setDelay(10);
  }
  timer_class.start();


Was This Post Helpful? 0
  • +
  • -

#3 hwoarang69   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 171
  • Joined: 23-October 12

Re: how to restart applet without closing it

Posted 14 March 2013 - 05:29 PM

awesome! just one question. iam not sure what is delay function is for? is it wait 10 minsec than start again?
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to restart applet without closing it

Posted 14 March 2013 - 06:45 PM

by default Timer are fired every delay time
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1