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