-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopWatch.java
More file actions
155 lines (133 loc) · 5.09 KB
/
Copy pathStopWatch.java
File metadata and controls
155 lines (133 loc) · 5.09 KB
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
public class StopWatch extends JFrame {
private JLabel clockLabel;
private JLabel stopwatchLabel;
private JButton startButton, stopButton, resetButton, lapButton;
private Timer stopwatchTimer, clockTimer;
private long startTime = 0;
private long elapsedTime = 0;
private boolean isRunning = false;
private Queue<String> lapTimes = new LinkedList<>();
private JTextArea lapTextArea;
public StopWatch() {
setTitle("Clock & Stopwatch");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setResizable(false);
// Clock Panel
JPanel clockPanel = new JPanel();
clockLabel = new JLabel();
clockLabel.setFont(new Font("Arial", Font.BOLD, 32));
clockPanel.add(clockLabel);
add(clockPanel, BorderLayout.NORTH);
// Stopwatch Panel
JPanel stopwatchPanel = new JPanel(new BorderLayout());
stopwatchLabel = new JLabel("00:00:00.000");
stopwatchLabel.setFont(new Font("Arial", Font.BOLD, 24));
stopwatchLabel.setHorizontalAlignment(SwingConstants.CENTER);
stopwatchPanel.add(stopwatchLabel, BorderLayout.NORTH);
// Button Panel
JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
startButton = new JButton("Start");
stopButton = new JButton("Stop");
resetButton = new JButton("Reset");
lapButton = new JButton("Lap");
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
buttonPanel.add(resetButton);
buttonPanel.add(lapButton);
stopwatchPanel.add(buttonPanel, BorderLayout.CENTER);
// Lap Times Panel
lapTextArea = new JTextArea(10, 30);
lapTextArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(lapTextArea);
stopwatchPanel.add(scrollPane, BorderLayout.SOUTH);
add(stopwatchPanel, BorderLayout.CENTER);
// Initialize timers
initClock();
initStopwatch();
// Button actions
startButton.addActionListener(e -> startStopwatch());
stopButton.addActionListener(e -> stopStopwatch());
resetButton.addActionListener(e -> resetStopwatch());
lapButton.addActionListener(e -> recordLapTime());
}
private void initClock() {
clockTimer = new Timer(1000, e -> {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
clockLabel.setText(sdf.format(new Date()));
});
clockTimer.start();
}
private void initStopwatch() {
stopwatchTimer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
long currentTime = System.currentTimeMillis();
elapsedTime = currentTime - startTime;
updateStopwatchLabel(elapsedTime);
}
});
}
private void updateStopwatchLabel(long time) {
long hours = time / 3600000;
long minutes = (time % 3600000) / 60000;
long seconds = (time % 60000) / 1000;
long millis = time % 1000;
String formattedTime = String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, millis);
stopwatchLabel.setText(formattedTime);
}
private void startStopwatch() {
if (!isRunning) {
startTime = System.currentTimeMillis() - elapsedTime;
stopwatchTimer.start();
isRunning = true;
}
}
private void stopStopwatch() {
if (isRunning) {
stopwatchTimer.stop();
isRunning = false;
}
}
private void resetStopwatch() {
stopwatchTimer.stop();
elapsedTime = 0;
startTime = 0;
isRunning = false;
stopwatchLabel.setText("00:00:00.000");
lapTimes.clear();
lapTextArea.setText("");
}
private void recordLapTime() {
if (isRunning) {
String lapTime = stopwatchLabel.getText();
lapTimes.add(lapTime);
// Keep only the last 10 lap times (using queue to manage size)
if (lapTimes.size() > 10) {
lapTimes.remove();
}
// Update lap times display
StringBuilder lapDisplay = new StringBuilder();
int lapNumber = 1;
for (String lap : lapTimes) {
lapDisplay.append("Lap ").append(lapNumber++).append(": ").append(lap).append("\n");
}
lapTextArea.setText(lapDisplay.toString());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
StopWatch app = new StopWatch();
app.setVisible(true);
});
}
}