-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlPanel.java
executable file
·414 lines (378 loc) · 12.7 KB
/
ControlPanel.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* ControlPanel class. contains all the buttons for game controls.
*
* @author haolidu
* @version 1.00
*/
public class ControlPanel extends JPanel {
public static final int PRIVATE_SOLDIER = 0;
public static final int PRIVATE_FIRST_CLASS_SOLDIER = 1;
public static final int CORPORAL_SOLDIER = 2;
private static final long serialVersionUID = 3503469091375541801L;
private JLabel money, life, waveL, towerInfo;
private JButton startPause, slingShot, pistol, sniper, submachine, machine;
private String towerType;
private boolean initialized;
private int wave;
/**
* ControlPanel constructor. Sets up, and place the buttons in the right
* order.
*/
public ControlPanel() {
super();
setBackground(Color.LIGHT_GRAY);
setPreferredSize(new Dimension(150, TowerDefenseGame.WINDOW_HEIGHT));
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
initializeButtons();
GridBagConstraints c = new GridBagConstraints();
c.weightx = 0.4;
c.gridx = 0;
c.gridy = 0;
c.ipadx = 30;
c.ipady = 10;
add(slingShot, c);
c.gridy++;
add(pistol, c);
c.gridy++;
add(sniper, c);
c.gridy++;
add(submachine, c);
c.gridy++;
add(machine, c);
// Labels
towerInfo = new JLabel("", JLabel.CENTER);
towerType = "SlingShot";
money = new JLabel("Money: " + TowerDefenseGame
.getGamePanel().getMoney(), JLabel.CENTER);
life = new JLabel("Lives: " + TowerDefenseGame.getGamePanel().getLife(),
JLabel.CENTER);
wave = 1;
waveL = new JLabel("Wave: " + wave, JLabel.CENTER);
Font font = new Font("Verdana", Font.BOLD, 20);
waveL.setFont(font);
c.gridy++;
add(towerInfo, c);
// Place holder
for (int i = 0; i < 20; i++) {
c.gridy++;
add(new JLabel(), c);
}
c.gridy++;
add(waveL, c);
c.gridy++;
add(money, c);
c.gridy++;
add(life, c);
// Start Buttons
startPause = new JButton("Start");
startPause.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!initialized) {
initialized = true;
startPause.setText("Pause");
waveStart(wave);
} else {
if (startPause.getText().equals("Pause")) {
TowerDefenseGame.getGamePanel().getTimer().stop();
startPause.setText("Resume");
} else {
TowerDefenseGame.getGamePanel().getTimer().start();
if (startPause.getText().equals("New Wave")) {
waveStart(wave);
}
startPause.setText("Pause");
}
}
}
});
c.gridy++;
c.insets = new Insets(20, 0, 0, 0);
c.fill = GridBagConstraints.VERTICAL;
add(startPause, c);
}
/**
* Helper method to initialize all the buttons.
*
* @return none.
*/
private void initializeButtons() {
// Towers
slingShot = new JButton("Sling Shot");
slingShot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
towerType = "SlingShot";
try {
setTowerInfo();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
pistol = new JButton("Pistol");
pistol.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
towerType = "Pistol";
try {
setTowerInfo();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
sniper = new JButton("Sniper");
sniper.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
towerType = "Sniper";
try {
setTowerInfo();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
submachine = new JButton("Submachine Gun");
submachine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
towerType = "SubmachineGun";
try {
setTowerInfo();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
machine = new JButton("Machine Gun");
machine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
towerType = "MachineGun";
try {
setTowerInfo();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
/**
* Helper method to sets up and show the tower information. Creates an
* instance of the particular tower and gets the tower's information.
*
* @throws ClassNotFoundException
* @return none.
*/
private void setTowerInfo() throws ClassNotFoundException {
Class<?> cl = Class.forName(towerType);
try {
Tower tower = (Tower) (cl.getDeclaredConstructor(int.class,
int.class).newInstance(0, 0));
towerInfo.setText("Cost: " + tower.cost + " Power: "
+ tower.getAttackPower());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Defines the properties of a new wave. Creates a new thread for enemy
* addition for each wave.
*
* @param wave
* in integer
* @return none
*/
public void waveStart(int wave) {
switch (wave) {
case 1:
(new Thread(new EnemyAddition())).start();
break;
case 2:
(new Thread(new EnemyAddition(1000, wave * 5))).start();
break;
case 3:
(new Thread(new EnemyAddition(900, wave * 5, wave * 2))).start();
break;
case 4:
(new Thread(new EnemyAddition(800, wave * 6, wave * 2))).start();
break;
case 5:
(new Thread(new EnemyAddition(800, wave * wave, wave * 2))).start();
break;
case 6:
(new Thread(new EnemyAddition(700, wave * wave, wave * 5, wave * 2)))
.start();
break;
case 7:
JOptionPane.showMessageDialog(this, "A new path is given!");
TowerDefenseGame.getGamePanel().newPath();
(new Thread(new EnemyAddition(700, wave * wave, wave * 5))).start();
break;
case 8:
(new Thread(
new EnemyAddition(600, wave * wave, wave * 10, wave * 2)))
.start();
break;
default:
(new Thread(
new EnemyAddition(300, wave * wave, wave * 10, wave * 5)))
.start();
}
}
/**
* Returns the tower type that the user have clicked on. Default is
* slingshot.
*
* @return String towerType
*/
public String getTowerType() {
return towerType;
}
/**
* Refreshes the game properties(Money and lives).
*
* @return none.
*/
public void refresh() {
money.setText("Money: " + TowerDefenseGame.getGamePanel().getMoney());
life.setText("Lives: " + TowerDefenseGame.getGamePanel().getLife());
}
/**
* Setup for a new wave. This stops the timer.
*
* @return none.
*/
public void newWave() {
TowerDefenseGame.getGamePanel().getTimer().stop();
startPause.setText("New Wave");
wave++;
waveL.setText("Wave: " + wave);
refresh();
}
/**
* Adds an enemy to the enemy array. The type of enemy added is specified by
* the parameter int.
*
* @param int enemytype
* @return none
*/
public void addEnemy(int i) {
List<Enemy> enemies = TowerDefenseGame.getGamePanel().getEnemies();
try {
switch (i) {
case 0:
enemies.add(new PrivateSoldier(TowerDefenseGame.getGamePanel()
.getBounds()));
break;
case 1:
enemies.add(new PrivateFirstClassSoldier(
TowerDefenseGame.getGamePanel().getBounds()));
break;
case 2:
enemies.add(new CorporalSoldier(TowerDefenseGame.getGamePanel()
.getBounds()));
break;
default:
enemies.add(new PrivateSoldier(TowerDefenseGame.getGamePanel()
.getBounds()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* EnemyAddition Class: A runnable class that dynamically adds the enemies
* during the wave. The type of enemies and the type of enemies and the
* interval of addition can be specified. Default enemy is 10 privateSoldier
* added at an interval of 1000ms.
*
* @author haolidu
* @version 1.00
*/
private class EnemyAddition implements Runnable {
private int interval;
private int[] numEnemyWave;
/**
* EnemyAddition default constructor. 10 privateSoldiers and 1000ms
* interval.
*/
public EnemyAddition() {
interval = 1000;
numEnemyWave = new int[2];
numEnemyWave[0] = 2;
}
/**
* EnemyAddition constructor. numEnemyWave can be any number of paired
* integers. The first number in the pair is the enenmyType and the
* second number is the number of enemies of that type.
*
* @param interval
* @param int... numEnemyWave
*/
public EnemyAddition(int interval, int... numEnemyWave) {
this.interval = interval;
this.numEnemyWave = numEnemyWave;
}
/**
* Starts the thread for adding enemies.
*
* @return void
*/
@Override
public void run() {
for (int enemyType = numEnemyWave.length; enemyType > 0; enemyType--) {
for (int j = 0; j < numEnemyWave[enemyType - 1]; j++) {
// Only allow one thread to access the enemies list
synchronized (this) {
addEnemy(enemyType - 1);
}
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}