forked from ahmetoguzazik2005/No-Limit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopWatchPanel.java
More file actions
152 lines (128 loc) · 5.26 KB
/
Copy pathStopWatchPanel.java
File metadata and controls
152 lines (128 loc) · 5.26 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
import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
public class StopWatchPanel extends JPanel {
private int seconds = 0, minutes = 0, hours = 0;
private final Timer timer;
private final JLabel timeLabel;
private final JButton startButton;
private final JButton endButton;// makes the clock time zero and also should create or at least make the
// timeobject finish and save to the db
private final JPanel bottomPanel;// for putting buttons together
public StudyBlock currentBlock;
public StopWatchPanel() {
setBackground(new Color(245, 245, 220)); // setting nicer backgrounds
setLayout(new BorderLayout());
bottomPanel = new JPanel();
bottomPanel.setBackground(new Color(245, 245, 220)); // Beige
add(bottomPanel, BorderLayout.SOUTH);
// Create the label for the time
timeLabel = new JLabel("00:00", SwingConstants.CENTER);
timeLabel.setFont(new Font("Arial", Font.BOLD, 80));
add(timeLabel, BorderLayout.CENTER);
// Create the start/stop button
//startButton = new JButton("START");
startButton = new AnimatedPressButton("START");
startButton.setFont(startButton.getFont().deriveFont(Font.PLAIN, 24f));
startButton.setFocusPainted(false);
startButton.setMargin(new Insets(10, 12, 10, 12));
// first part should also be green
startButton.setBackground(new Color(40, 167, 69));
startButton.setOpaque(true);
startButton.setContentAreaFilled(true);
startButton.setBorderPainted(false);
startButton.setPreferredSize(new Dimension(180, 60));
bottomPanel.add(startButton);
endButton = new AnimatedPressButton("FINISH");
endButton.setFont(startButton.getFont().deriveFont(Font.PLAIN, 24f));
endButton.setFocusPainted(false);
endButton.setMargin(new Insets(10, 12, 10, 12));
endButton.setBackground(new Color(220, 53, 69));// red
endButton.setOpaque(true);
endButton.setContentAreaFilled(true);
endButton.setBorderPainted(false);
endButton.setPreferredSize(new Dimension(180, 60));
bottomPanel.add(endButton);
// Timer — fires every 1000 ms (1 second)
timer = new Timer(1000, e -> {
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
}
if (minutes == 60) {
minutes = 0;
hours++;
}
updateTimeLabel();
});
// Button click toggles the timer
startButton.addActionListener(e -> {
if (timer.isRunning()) {
// Stop the timer
currentBlock.end();
try {
Main.m.addStudyBlock(currentBlock.startTime,currentBlock.endTime);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
timer.stop();
startButton.setText("START");
// better visuals
startButton.setBackground(new Color(40, 167, 69));
startButton.setOpaque(true);
startButton.setContentAreaFilled(true);
startButton.setBorderPainted(false);
} else {
currentBlock = new StudyBlock();
// Start the timer
timer.start();
startButton.setText(" STOP ");// for making button size same for all
// better visuals
startButton.setBackground(Color.ORANGE);
startButton.setOpaque(true);
startButton.setContentAreaFilled(true);
startButton.setBorderPainted(false);
}
});
// Button click toggles the timer
endButton.addActionListener(e -> {
if (timer.isRunning() ) {
// should also handle db records
currentBlock.end();
try {
Main.m.addStudyBlock(currentBlock.startTime,currentBlock.endTime);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
startButton.setText("START");
// better visuals
startButton.setBackground(new Color(40, 167, 69));
startButton.setOpaque(true);
startButton.setContentAreaFilled(true);
startButton.setBorderPainted(false);
timer.stop();
seconds = 0;
minutes = 0;
hours = 0;
updateTimeLabel();
// if time is zero it should not create new records
}else{ // when pressed finish after stop
timer.stop();
seconds = 0;
minutes = 0;
hours = 0;
updateTimeLabel();
}
});
}
// Format the time as HH:MM:SS
private void updateTimeLabel() {
// for better visual
if (hours == 0) {
timeLabel.setText(String.format("%02d:%02d", minutes, seconds));
} else {
timeLabel.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
}
}