-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountdownClock.java
175 lines (120 loc) · 5.43 KB
/
CountdownClock.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
package sudoku;
import javax.swing.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CountdownClock extends JPanel {
private static final long serialVersionUID = 1L;
private JLabel label;
private Timer timer;
private int countdownTime;
private JButton stop_start_Button;
private int run_or_pause = 0;
private MusicPlayer GameOveralMusic, GameLastSecondsMusic;
private int initial_time = 0;
private Color[][] to_store_color = new Color[9][9];
private String[][] to_store_number = new String[9][9];
public CountdownClock(int seconds, GameBoardPanel gameboard) {
initial_time = seconds;
GameOveralMusic = new MusicPlayer("D:/temp/KaNong.wav");
GameLastSecondsMusic = new MusicPlayer("D:/temp/KaNong.wav");
// GameLastSecondsMusic = new MusicPlayer("/Users/OswinChen/Desktop/nervous.wav");
countdownTime = seconds;
label = new JLabel(String.format("%02d:%02d", countdownTime / 60, countdownTime % 60));
add(label);
label.setFont(new Font("Consolas", Font.BOLD, 20));
stop_start_Button = new JButton("Stop");
stop_start_Button.setFont(new Font("Times New Roman", Font.BOLD, 19));
stop_start_Button.setOpaque(true);
stop_start_Button.setForeground(Color.BLACK);
stop_start_Button.setPreferredSize(new Dimension(80, 30));
stop_start_Button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
stop_start_Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
run_or_pause ++;
if((run_or_pause % 2) == 1) {
timer.stop();
stop_start_Button.setText("Start");
}else {
timer.start();
stop_start_Button.setText("Stop");
}
if (!timer.isRunning()) {
for (int row = 0; row < 9; ++row) { //使格子有意義了,數字、顏色
for (int col = 0; col < 9; ++col) {
if(gameboard.cells[row][col].getBackground() != new Color(216, 0, 0)&& gameboard.cells[row][col].getBackground() != new Color(0, 216, 0)) {
to_store_color[row][col] = gameboard.cells[row][col].getBackground();
} else {
to_store_color[row][col] = Color.YELLOW;
}
to_store_number[row][col] = gameboard.cells[row][col].getText();
gameboard.cells[row][col].setBackground(Color.WHITE);
gameboard.cells[row][col].setText("");
gameboard.cells[row][col].setEditable(false);
// Change the color of the cell to light gray
gameboard.getGB_cells_Panel().setBackground(new Color(176, 224, 230));
}
}
} else {
for (int row = 0; row < 9; ++row) { //使格子有意義了,數字、顏色
for (int col = 0; col < 9; ++col) {
gameboard.cells[row][col].setBackground(to_store_color[row][col]);
gameboard.cells[row][col].setText(to_store_number[row][col]);
gameboard.getGB_cells_Panel().setBackground(Color.ORANGE);
gameboard.cells[row][col].setEditable(true);
}
}
}
}
});
add(stop_start_Button);
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PlayGameOveralMusic();
countdownTime++;
if (countdownTime == -2) {
timer.stop();
JOptionPane.showMessageDialog(null, "Time is up! You have failed.", "Failure", JOptionPane.ERROR_MESSAGE);
System.exit(0);
} else {
label.setText(String.format("%02d:%02d", countdownTime / 60, countdownTime % 60));
}
ChangeToNervous();
StopNervous();
}
});
timer.start();
}
public int getCountdownTime() {
return countdownTime;
}
public void setCountdownTime(int countdownTime) {
this.countdownTime = countdownTime;
}
public MusicPlayer getGameOveralMusic() {
return GameOveralMusic;
}
public MusicPlayer getGameLastSecondsMusic() {
return GameLastSecondsMusic;
}
public void PlayGameOveralMusic() {
if(countdownTime == initial_time) {
GameOveralMusic.GameOveralMusicplay();
}
}
public void ChangeToNervous() {
if(countdownTime == 30) {
GameOveralMusic.stop();
GameLastSecondsMusic.GameLastSecondsMusicplay();
}
}
public void StopNervous() {
if(countdownTime == 0) {
GameLastSecondsMusic.stop();
}
}
}