Hi, I am trying to make a timer applet when I click stop button to stop counting, disappear stop and appears continue button and when I click continue to continue the counting.

My code is :

import java.applet.*;
import java.awt.*;
import javax.swing.*;


public class Timer extends Applet implements Runnable
    {
     private String msg2 = new String("");
     private int secs;
     private int mins;
     private int hrs;
     private Thread clock;
     public int time =300000;
     Button clickOnMe;


     public void destroy()
     {
         clock.stop();
     }
    
     public void init()
     {
             clickOnMe = new Button("Stop!");
             add(clickOnMe);

             String parameter;
        
             if(clock == null)
             {
               clock = new Thread(this);
                clock.start();
             }
     }
    
     
     public void paint(Graphics gr)
         {
         ++secs;
         if(secs == 60)
             {
             mins++;
             secs = 0;
         }
         if(mins == 60)
             {
             hrs++;
             secs = 0;
             mins = 0;
         }
         gr.drawString(" "+ hrs + ":" + mins + ":" + secs, 10, 130);
         gr.drawString(msg2,10,150);
         setBackground(Color.white);
     }
    
     public void run()
         {
             while(true)
             {
             repaint();
             try
                 {
                 clock.sleep(1000);
             }
             catch(InterruptedException e)
                 {}
         }
     }
    
     public void start()
         {
         clock.resume();
     }
    
     public void stop()
         {
         clock.suspend();
     }
     
     public boolean action (Event e, Object args)
     { 
       if (e.target == clickOnMe)
          stop();
       repaint();
       
       return true;
   }

}

Are you having problems? Can you explain what the problem is?

Look at using a Timer vs writing your own using Thread.

Also its better NOT to use one of the standard Java class names for your class.

Edited 5 Years Ago by NormR1: n/a

Are you having problems? Can you explain what the problem is?

Look at using a Timer vs writing your own using Thread.

Also its better NOT to use one of the standard Java class names for your class.

The problem I have is that when I click on stop and press again the button the counter increases a sec. And then increases only when I press button

First comment is that most of the Thread methods used in your code are deprecated.
You should look at using the Timer class.

What code is supposed to start the timer again? Where/when is it called?

First comment is that most of the Thread methods used in your code are deprecated.
You should look at using the Timer class.

What code is supposed to start the timer again? Where/when is it called?

Ok thanks a lot for your help,
I did not know that exists Timer class already.

but there are two Timers java.util.Timer and and javax.swing.Timer for your purpose is only javax.swing.Timer

never call sleep(int) in Swing, because there no guarantee for unexpected output to the GUI

Edited 5 Years Ago by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.