Program Code:-
******************Stop Watch******************
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Clock extends Applet implements Runnable,ActionListener
//Panel to keep all the buttons and label
Panel p;
Label display;
//Button
Button start, stop, reset;
//Time
int hour, minute,second,millisecond;
//String to be displayed on the label
String disp;
//State of stopwatch on/off
boolean on;
//Initialization
public void init()
//Initially off
on=false;
p=new Panel();
//Setting layout of the panel
p.setLayout(new GridLayout(4,1,6,10));
//Initial time 00:00:00:000
hour=minute=second=millisecond=0;
//Label
display =new Label();
disp="00:00:00:000";
display.setText(disp);
p.add(display);
//Start Button
start=new Button("Start");
start.addActionListener((ActionListener)this);
p.add(start);
//Reset Button
reset = new Button("Reset");
reset.addActionListener((ActionListener)this);
p.add(reset);
//Stop Button
stop=new Button("Stop");
stop.addActionListener((ActionListener)this);
p.add(stop);
add(p);
//Starting Thread
new Thread(this,"StopWatch").start();
//Reset Function
//Reset to default value
public void reset()
try{
Thread.sleep(1);
}
catch(Exception e){
System.out.println(e);
}
hour=minute=second=millisecond=0;
}
//Update function
//Update the timer
public void update()
millisecond++;
if(millisecond==1000) {
millisecond=0;
second++;
if(second==60)
{
second=0;
minute++;
if(minute==60)
{
minute=0;
hour++;
}
}
//Changing Label
public void changeLabel()
{
//Properly formatting the display of the timer
if(hour<10)
disp="0"+hour+" : ";
else
disp=hour+" : ";
if(minute<10)
disp+="0"+minute+" : ";
else
disp+=minute+" : ";
if(second<10)
disp+="0"+second+" : ";
else
disp+=second+" : ";
if(millisecond<10)
disp+="00"+millisecond;
else if (millisecond<100)
disp+="0"+millisecond;
else
disp+=millisecond;
display.setText(disp);
}
//thread.run function
public void run()
//while the strength is on
while(on)
try{
//pause 1 millisecond
Thread.sleep(1);
//update the timer
update();
//changeLabel
changeLabel();
catch(InterruptedException e){
System.out.println(e);
}
//actionPerformed
//To listen to the actions on the buttons
public void actionPerformed(ActionEvent e)
//start a thread when start button is clicked
if(e.getSource()==start)
//stopwatch is on
on=true;
new Thread(this,"StopWatch").start();
//reset
if(e.getSource()==reset)
//stopwatch off
on=false;
reset();
changeLabel();
if(e.getSource()==stop)
//stopwatch off
on=false;}
**********************Applet Code*********************
<html>
<body>
<applet>
<applet code=”Clock.java” width=400 height=600>
</applet>
</body>
</html>
Conclusion:-
Hence from this project we successfully learn to develop a stop watch using applet by
using Action Listener to handle events. This program contains three buttons Start , Stop and
Reset. When the Start button is placed the timer gets start , when we press the Stop button the
the timer gets stop and when the Reset button is pressed then the timer again starts from its
initial value.