This repository has been archived by the owner on Oct 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstopwatch.java
176 lines (146 loc) · 3.22 KB
/
stopwatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Java program to illustrate
// digital stop watch
// using Applets
// importing required packages
import 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;
}
}
}