0% found this document useful (0 votes)
108 views

Java Micro Project

This Java program creates a stopwatch applet with start, stop, and reset buttons. The applet uses threads to continuously update the displayed time. When start is clicked, a thread runs that increments the milliseconds, seconds, minutes, and hours variables and updates the displayed time label. When stop is clicked, the thread stops running. When reset is clicked, all time variables are reset to initial values of zero. The applet provides a simple example of creating a timer with threads and handling button click events.

Uploaded by

Devendra Mali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Java Micro Project

This Java program creates a stopwatch applet with start, stop, and reset buttons. The applet uses threads to continuously update the displayed time. When start is clicked, a thread runs that increments the milliseconds, seconds, minutes, and hours variables and updates the displayed time label. When stop is clicked, the thread stops running. When reset is clicked, all time variables are reset to initial values of zero. The applet provides a simple example of creating a timer with threads and handling button click events.

Uploaded by

Devendra Mali
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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. 

You might also like