-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPomodoro.java
283 lines (254 loc) · 8.39 KB
/
Pomodoro.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Pomodoro.java
*
* Copyright (C) [2023] [Susanne Coates]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
/**
* The Pommodoro class implements a timer with a user interface to remind users to take regular breaks.
* It alternates between a work period and a break period, prompting the user to move or work at each interval.
* The GUI displays the countdown timer and provides controls to start, stop, reset, and resume the timer.
* The window remains always on top and the font size of the timer adjusts dynamically with the window size.
* @author Susanne Coates (scoates@susannecoates.com)
* @author Jennifer Zahnhiser (jaz-ai@susannecoates.com)
*/
public class Pomodoro extends JFrame {
// Constants defining the duration of work and break periods in seconds
private static final int WORK_TIME = 1800; // Duration of the work period
private static final int BREAK_TIME = 300; // Duration of the break period
// UI components for displaying time and controlling the timer state
private JLabel timerDisplay; // Shows the remaining time
private JButton startButton; // Starts the countdown
private JButton stopButton; // Stops the countdown
private JButton resetButton; // Resets the countdown to the initial value
private JButton resumeButton; // Resumes the countdown for the next period
// State management for the timer's current time and period (work/break)
private int currentTime; // Tracks the current countdown time in seconds
private boolean isWorkPeriod; // Indicates if the timer is in a work period
private Timer timer; // Manages the countdown logic
private Timer beepTimer; // Keeps track of the beeping timer
/**
* Constructs the Pomodoro Timer by setting up the GUI components and
* initializing the timer logic.
*/
public Pomodoro() {
initializeGuiComponents();
initializeTimerLogic();
}
/**
* Sets up the GUI components and their layout within the frame.
* It initializes a resizable display label for the timer and
* buttons for controlling the timer.
* It also configures the frame to always stay on top and sets up the
* component listener for resizing events.
*/
private void initializeGuiComponents() {
// Set the title and default close operation
setTitle("Work/Break Interval Timer");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Use GridBagLayout for flexibility in resizing
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// Timer display label configuration
timerDisplay = new JLabel("30:00");
timerDisplay.setFont(new Font("Monospaced", Font.BOLD, 50));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.insets = new Insets(10, 10, 10, 10);
gbc.anchor = GridBagConstraints.CENTER;
add(timerDisplay, gbc);
// Start button configuration
startButton = new JButton("Start");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(startButton, gbc);
// Stop button configuration
stopButton = new JButton("Stop");
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(stopButton, gbc);
// Reset button configuration
resetButton = new JButton("Reset");
gbc.gridx = 0;
gbc.gridy = 2;
add(resetButton, gbc);
// Resume button configuration
resumeButton = new JButton("Switch");
resumeButton.setEnabled(true); // Disabled by default
gbc.gridx = 1;
gbc.gridy = 2;
add(resumeButton, gbc);
// Add a component listener to the frame to handle resizing
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
resizeTimerFont();
}
});
// Adjust window size to fit all components
pack();
// Center window on screen
setLocationRelativeTo(null);
// Keep the timer on top
setAlwaysOnTop(true);
}
/**
* Function to resize timer font when window resized
*/
private void resizeTimerFont() {
// Get the current dimensions of the JFrame
Dimension size = getSize();
// Calculate the font size. You might need to tweak the divisor based on your preferences
int fontSize = Math.max(size.width / 10, 50);
// Set the new font size to the timer display
timerDisplay.setFont(new Font("Monospaced", Font.BOLD, fontSize));
}
/**
* Initializes timer logic and state management.
*/
private void initializeTimerLogic() {
// Set initial state
isWorkPeriod = true;
currentTime = WORK_TIME;
// Initialize the timer
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Decrement the current time
currentTime--;
// Update the display
updateTimerDisplay();
// Check if the timer has reached zero
if (currentTime <= 0) {
timer.stop();
switchPeriods();
resumeButton.setEnabled(true);
playAlarmSound(); // play the alarm sound
}
}
});
// Configure action listeners for buttons
// The stop and resume button action listeners stop the beep
startButton.addActionListener(e -> {
stopAlarmSound(); // Stop the beeping
startTimer();
});
stopButton.addActionListener(e -> {
stopAlarmSound(); // Stop the beeping
stopTimer();
});
resetButton.addActionListener(e -> {
stopAlarmSound(); // Stop the beeping
resetTimer();
});
resumeButton.addActionListener(e -> {
stopAlarmSound(); // Stop the beeping
resumeTimer();
});
}
/**
* Updates the timer display label.
*/
private void updateTimerDisplay() {
int minutes = currentTime / 60;
int seconds = currentTime % 60;
timerDisplay.setText(String.format("%02d:%02d", minutes, seconds));
}
/**
* Starts or resumes the timer.
*/
private void startTimer() {
timer.start();
//resumeButton.setEnabled(false);
}
/**
* Stops the timer.
*/
private void stopTimer() {
timer.stop();
//resumeButton.setEnabled(true);
}
/**
* Resets the timer to the initial value of the current period.
*/
private void resetTimer() {
currentTime = isWorkPeriod ? WORK_TIME : BREAK_TIME;
updateTimerDisplay();
//resumeButton.setEnabled(false);
}
/**
* Switches between work and break periods.
*/
private void switchPeriods() {
isWorkPeriod = !isWorkPeriod;
currentTime = isWorkPeriod ? WORK_TIME : BREAK_TIME;
updateTimerDisplay();
}
/**
* Resumes the timer for the next period.
*/
private void resumeTimer() {
switchPeriods();
//startTimer();
}
/**
* Method to start the alarm sound, which will beep every second.
*/
private void playAlarmSound() {
// If there's already a beeping timer running, stop it before creating a new one
if (beepTimer != null && beepTimer.isRunning()) {
beepTimer.stop();
}
// Initialize the beepTimer to beep every second
beepTimer = new Timer(1000, e -> {
try {
Toolkit.getDefaultToolkit().beep(); // Play the default beep sound
} catch (Exception ex) {
ex.printStackTrace();
}
});
beepTimer.start();
}
/**
* Method to stop the alarm sound.
*/
private void stopAlarmSound() {
if (beepTimer != null) {
beepTimer.stop();
}
}
/**
* Entry point to start the timer application.
* @param args Command line arguments (not used).
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Pomodoro frame = new Pomodoro();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
});
}
}