Java Applet | Digital Stopwatch - GeeksforGeeks

29 Aug, 2018
This article will provide an instance of creating one type of stopwatch with Java Applet, AWT and Thread.
We shall be using all these library to make a working model of a stopwatch. The GUI shall have 3 buttons for interaction, namely, start (to start the time), reset (to reset the time to default value) and stop (to stop the timer).
With the help of actionListener in Java we can learn which button is pressed when.
When the start button is pressed, we start the timer. While the reset or the stop button isn’t pressed we shall keep the timer running i.e. we shall change the value of the timer (hh:mm:ss:ms) every 1 millisecond and update the screen.When reset button is pressed, we shall stop the timer and set it to is default value (00:00:00:000).When the stop button is pressed, we stop the timer with the value as it is.
When the start button is pressed, we start the timer. While the reset or the stop button isn’t pressed we shall keep the timer running i.e. we shall change the value of the timer (hh:mm:ss:ms) every 1 millisecond and update the screen.
When reset button is pressed, we shall stop the timer and set it to is default value (00:00:00:000).
When the stop button is pressed, we stop the timer with the value as it is.
Below is the implementation of Digital Stopwatch
// Java program to illustrate// digital stop watch// using Applets  // importing required packagesimport java.applet.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;  public class GeeksforGeeks extends Applet implements Runnable, ActionListener {      // Panel to keep all the buttons and labels    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 stopwatch is on        while (on) {            try {                // pause 1 millisecond                Thread.sleep(1);                // update the time                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;        }    }}
Output :
See here
java-applet
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java