Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how to restart applet without closing it

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default how to restart applet without closing it

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: how to restart applet without closing it

    Use a variable to tell the method not to double the time.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: how to restart applet without closing it

    i just did some debuging and the result are shocking.
    turn out that value of timer isnt change and my animation speed(dx) isnt changing too. and yet my animation are speeding up.

    should i submit all my code so you can run and see the problem?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: how to restart applet without closing it

    Is the speed up caused by an additional timer being started?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. IE9 crashes on closing window containing Java Applet
    By parthei in forum Java Applets
    Replies: 4
    Last Post: August 30th, 2012, 01:24 AM
  2. Help with closing a JFrame (not the program)
    By lanepulcini in forum AWT / Java Swing
    Replies: 1
    Last Post: February 10th, 2012, 11:20 AM
  3. how to restart my program
    By jack_nutt in forum Java Theory & Questions
    Replies: 15
    Last Post: November 16th, 2011, 06:39 PM
  4. Sliding puzzle Restart button(same exact game)
    By carterb32 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2011, 04:29 AM
  5. [SOLVED] frame closing programmatically
    By nasi in forum AWT / Java Swing
    Replies: 9
    Last Post: May 10th, 2010, 03:43 AM