-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathGameWindow.java
More file actions
249 lines (190 loc) · 8.69 KB
/
GameWindow.java
File metadata and controls
249 lines (190 loc) · 8.69 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
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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class GameWindow {
private JFrame gameWindow;
public Clock blackClock;
public Clock whiteClock;
private Timer timer;
private Board board;
public GameWindow(String blackName, String whiteName, int hh,
int mm, int ss) {
blackClock = new Clock(hh, ss, mm);
whiteClock = new Clock(hh, ss, mm);
gameWindow = new JFrame("Chess");
try {
Image whiteImg = ImageIO.read(getClass().getResource("wp.png"));
gameWindow.setIconImage(whiteImg);
} catch (Exception e) {
System.out.println("Game file wp.png not found");
}
gameWindow.setLocation(100, 100);
gameWindow.setLayout(new BorderLayout(20,20));
// Game Data window
JPanel gameData = gameDataPanel(blackName, whiteName, hh, mm, ss);
gameData.setSize(gameData.getPreferredSize());
gameWindow.add(gameData, BorderLayout.NORTH);
this.board = new Board(this);
gameWindow.add(board, BorderLayout.CENTER);
gameWindow.add(buttons(), BorderLayout.SOUTH);
gameWindow.setMinimumSize(gameWindow.getPreferredSize());
gameWindow.setSize(gameWindow.getPreferredSize());
gameWindow.setResizable(false);
gameWindow.pack();
gameWindow.setVisible(true);
gameWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
// Helper function to create data panel
private JPanel gameDataPanel(final String bn, final String wn,
final int hh, final int mm, final int ss) {
JPanel gameData = new JPanel();
gameData.setLayout(new GridLayout(3,2,0,0));
// PLAYER NAMES
JLabel w = new JLabel(wn);
JLabel b = new JLabel(bn);
w.setHorizontalAlignment(JLabel.CENTER);
w.setVerticalAlignment(JLabel.CENTER);
b.setHorizontalAlignment(JLabel.CENTER);
b.setVerticalAlignment(JLabel.CENTER);
w.setSize(w.getMinimumSize());
b.setSize(b.getMinimumSize());
gameData.add(w);
gameData.add(b);
// CLOCKS
final JLabel bTime = new JLabel(blackClock.getTime());
final JLabel wTime = new JLabel(whiteClock.getTime());
bTime.setHorizontalAlignment(JLabel.CENTER);
bTime.setVerticalAlignment(JLabel.CENTER);
wTime.setHorizontalAlignment(JLabel.CENTER);
wTime.setVerticalAlignment(JLabel.CENTER);
if (!(hh == 0 && mm == 0 && ss == 0)) {
timer = new Timer(1000, null);
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean turn = board.getTurn();
if (turn) {
whiteClock.decr();
wTime.setText(whiteClock.getTime());
if (whiteClock.outOfTime()) {
timer.stop();
int n = JOptionPane.showConfirmDialog(
gameWindow,
bn + " wins by time! Play a new game? \n" +
"Choosing \"No\" quits the game.",
bn + " wins!",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
new GameWindow(bn, wn, hh, mm, ss);
gameWindow.dispose();
} else gameWindow.dispose();
}
} else {
blackClock.decr();
bTime.setText(blackClock.getTime());
if (blackClock.outOfTime()) {
timer.stop();
int n = JOptionPane.showConfirmDialog(
gameWindow,
wn + " wins by time! Play a new game? \n" +
"Choosing \"No\" quits the game.",
wn + " wins!",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
new GameWindow(bn, wn, hh, mm, ss);
gameWindow.dispose();
} else gameWindow.dispose();
}
}
}
});
timer.start();
} else {
wTime.setText("Untimed game");
bTime.setText("Untimed game");
}
gameData.add(wTime);
gameData.add(bTime);
gameData.setPreferredSize(gameData.getMinimumSize());
return gameData;
}
private JPanel buttons() {
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1, 3, 10, 0));
final JButton quit = new JButton("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(
gameWindow,
"Are you sure you want to quit?",
"Confirm quit", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
if (timer != null) timer.stop();
gameWindow.dispose();
}
}
});
final JButton nGame = new JButton("New game");
nGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(
gameWindow,
"Are you sure you want to begin a new game?",
"Confirm new game", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
SwingUtilities.invokeLater(new StartMenu());
gameWindow.dispose();
}
}
});
final JButton instr = new JButton("How to play");
instr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(gameWindow,
"Move the chess pieces on the board by clicking\n"
+ "and dragging. The game will watch out for illegal\n"
+ "moves. You can win either by your opponent running\n"
+ "out of time or by checkmating your opponent.\n"
+ "\nGood luck, hope you enjoy the game!",
"How to play",
JOptionPane.PLAIN_MESSAGE);
}
});
buttons.add(instr);
buttons.add(nGame);
buttons.add(quit);
buttons.setPreferredSize(buttons.getMinimumSize());
return buttons;
}
public void checkmateOccurred (int c) {
if (c == 0) {
if (timer != null) timer.stop();
int n = JOptionPane.showConfirmDialog(
gameWindow,
"White wins by checkmate! Set up a new game? \n" +
"Choosing \"No\" lets you look at the final situation.",
"White wins!",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
SwingUtilities.invokeLater(new StartMenu());
gameWindow.dispose();
}
} else {
if (timer != null) timer.stop();
int n = JOptionPane.showConfirmDialog(
gameWindow,
"Black wins by checkmate! Set up a new game? \n" +
"Choosing \"No\" lets you look at the final situation.",
"Black wins!",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
SwingUtilities.invokeLater(new StartMenu());
gameWindow.dispose();
}
}
}
}