-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
268 lines (178 loc) · 5.4 KB
/
GameWindow.java
File metadata and controls
268 lines (178 loc) · 5.4 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import javax.swing.*; // need this for GUI objects
import java.awt.*; // need this for Layout Managers
import java.awt.event.*; // need this to respond to GUI events
public class GameWindow extends JFrame
implements ActionListener,
KeyListener,
MouseListener
{
// declare instance variables for user interface objects
// declare labels
private JLabel statusBarL;
private JLabel keyL;
// declare text fields
private JTextField statusBarTF;
private JTextField keyTF;
// declare buttons
private JButton startB;
private JButton pauseB;
private JButton endB;
private JButton exitB;
private Container c;
private JPanel mainPanel;
private GamePanel gamePanel;
@SuppressWarnings({"unchecked"})
public GameWindow() {
setTitle ("Bravo One: Skies of Retribution");
setSize (525, 525);
// create user interface objects
// create labels
statusBarL = new JLabel ("Application Status: ");
keyL = new JLabel("Key Pressed: ");
// create text fields and set their colour, etc.
statusBarTF = new JTextField (25);
keyTF = new JTextField (25);
statusBarTF.setEditable(false);
keyTF.setEditable(false);
statusBarTF.setBackground(Color.CYAN);
keyTF.setBackground(Color.YELLOW);
// create buttons
startB = new JButton ("Start");
pauseB = new JButton ("Pause");
endB = new JButton ("End Game");
exitB = new JButton ("Exit");
// add listener to each button (same as the current object)
startB.addActionListener(this);
pauseB.addActionListener(this);
endB.addActionListener(this);
exitB.addActionListener(this);
// create mainPanel
mainPanel = new JPanel();
FlowLayout flowLayout = new FlowLayout();
mainPanel.setLayout(flowLayout);
GridLayout gridLayout;
// create the gamePanel for game entities
gamePanel = new GamePanel();
gamePanel.setPreferredSize(new Dimension(400, 400));
// create infoPanel
JPanel infoPanel = new JPanel();
gridLayout = new GridLayout(2, 2);
infoPanel.setLayout(gridLayout);
infoPanel.setBackground(Color.ORANGE);
// add user interface objects to infoPanel
infoPanel.add (statusBarL);
infoPanel.add (statusBarTF);
infoPanel.add (keyL);
infoPanel.add (keyTF);
// create buttonPanel
JPanel buttonPanel = new JPanel();
gridLayout = new GridLayout(1, 4);
buttonPanel.setLayout(gridLayout);
// add buttons to buttonPanel
buttonPanel.add (startB);
// buttonPanel.add (pauseB);
buttonPanel.add (endB);
buttonPanel.add (exitB);
// add sub-panels with GUI objects to mainPanel and set its colour
// mainPanel.add(infoPanel);
mainPanel.add(gamePanel);
mainPanel.add(buttonPanel);
mainPanel.setBackground(Color.BLACK);
// set up mainPanel to respond to keyboard and mouse
gamePanel.addMouseListener(this);
mainPanel.addKeyListener(this);
// add mainPanel to window surface
c = getContentPane();
c.add(mainPanel);
// set properties of window
setResizable(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// set status bar message
statusBarTF.setText("Application started.");
}
// implement single method in ActionListener interface
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
statusBarTF.setText(command + " button clicked.");
if (command.equals(startB.getText())) {
gamePanel.startGame();
gamePanel.scoringPanel.initialize(0);
gamePanel.lifePanel.initialize(5);
pauseB.setText ("Pause");
}
if (command.equals("Pause")) {
if(gamePanel.running()){
gamePanel.pauseGame();
pauseB.setText ("Resume");
}
}
if (command.equals("Resume")) {
if(gamePanel.running()){
gamePanel.pauseGame();
pauseB.setText ("Pause");
}
}
if (command.equals(endB.getText())) {
gamePanel.endGame();
pauseB.setText ("Pause");
}
if (command.equals(exitB.getText()))
System.exit(0);
mainPanel.requestFocus();
}
// implement methods in KeyListener interface
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
String keyText = e.getKeyText(keyCode);
keyTF.setText(keyText + " pressed.");
if(gamePanel.running()){
if (keyCode == KeyEvent.VK_LEFT || keyCode== KeyEvent.VK_A) {
gamePanel.updateBat (1);
}
if (keyCode == KeyEvent.VK_RIGHT || keyCode== KeyEvent.VK_D) {
gamePanel.updateBat (2);
}
if (keyCode == KeyEvent.VK_UP || keyCode== KeyEvent.VK_W) {
gamePanel.updateBat (3);
}
if (keyCode == KeyEvent.VK_DOWN || keyCode== KeyEvent.VK_S) {
gamePanel.updateBat (4);
}
if(keyCode == KeyEvent.VK_SPACE){
if(!gamePanel.bulletExist())
gamePanel.shootBullet();
}
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if(gamePanel.running()){
if (keyCode == KeyEvent.VK_LEFT || keyCode== KeyEvent.VK_A) {
gamePanel.batStop(0);
}
if (keyCode == KeyEvent.VK_RIGHT || keyCode== KeyEvent.VK_D) {
gamePanel.batStop(0);
}
if (keyCode == KeyEvent.VK_UP || keyCode== KeyEvent.VK_W) {
gamePanel.batStop(1);
}
if (keyCode == KeyEvent.VK_DOWN || keyCode== KeyEvent.VK_S) {
gamePanel.batStop(1);
}
}
}
public void keyTyped(KeyEvent e) {
}
// implement methods in MouseListener interface
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}