-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTimer.java
More file actions
70 lines (60 loc) · 1.93 KB
/
CustomTimer.java
File metadata and controls
70 lines (60 loc) · 1.93 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
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.*;
public class CustomTimer extends JLabel {
private int secondsPassed;
private int delay;
private ActionListener taskPerformer;
private Timer timer;
private boolean timerPaused;
public CustomTimer(int delay) {
this.delay = delay;
this.taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!timerPaused)
secondsPassed++;
String minutes = String.format("%02d", secondsPassed / 60);
String seconds = String.format("%02d", secondsPassed % 60);
setText(minutes + ":" + seconds);
}
};
this.timer = new Timer(delay, taskPerformer);
this.timerPaused = false;
this.setForeground(MisalignGraphics.getSettings().getColor(7));
this.setFont(new Font("Courier New", Font.BOLD, 26)); //might eventually change to custom 7 sgement font
this.setText("00:00");
this.setBackground(MisalignGraphics.getSettings().getColor(8));
this.setOpaque(true);
this.setBorder(BorderFactory.createEmptyBorder(2, 5, 0, 5));
}
// Default timer increments every second
public CustomTimer() {
this(1000);
}
// Starts timer
public void start() {
this.timerPaused = false;
this.timer.start();
}
// Pause/Unpauses timer
public void togglePause() {
this.timerPaused = !this.timerPaused;
}
// Restarts timer
public void restart() {
this.secondsPassed = 0;
this.setText("00:00");
this.timer.start();
}
// Stops the timer
public void stop() {
this.timer.stop();
}
// Returns the swing Timer within the CustomTimer
public Timer getSwingTimer() {
return this.timer;
}
}