-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
268 lines (199 loc) · 6.7 KB
/
Copy pathGameWindow.java
File metadata and controls
268 lines (199 loc) · 6.7 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.*;
import java.awt.event.*;
import java.util.HashSet;
import java.awt.*;
public class GameWindow extends JFrame implements ActionListener, KeyListener, MouseListener{
//
private int level;
//public HashSet<Integer> directions;
public boolean[] directions = {false, false, false, false};
// labels
private JLabel inventory, questItems, health, status, gamelevel;
// // text fields
// private JTextField statusTF, keyTF, mouseTF;
// text areas
private JTextArea inventoryText, questItemsText, levelText;
// scroll panes
private JScrollPane inventoryScroll, questItemsScroll, healthScroll, statusScroll, levelScroll;
// buttons
private JButton start, quit, pause, restart;
// panels
private JPanel inventoryPanel, questItemsPanel, healthPanel, main, controls;
private GamePanel2 gamePanel;
private GamePanel gp;
private CardLayout cardLayout;
private JPanel cardPanel;
//private Keylisteners keylisteners;
private Container c;
@SuppressWarnings("unchecked")
public GameWindow(){
setTitle("Nomad");
setSize(1200, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200, 0);
//directions = new HashSet<>();
level = 1;
// User Interface Components
inventory = new JLabel("Inventory");
questItems = new JLabel("Quest Items");
health = new JLabel("Health");
status = new JLabel("Status");
gamelevel = new JLabel("Game-Level");
// Panel Fields
inventoryPanel = new JPanel();
inventoryPanel.setLayout(new BoxLayout(inventoryPanel, BoxLayout.Y_AXIS));
inventoryPanel.add(inventory);
//inventoryPanel.add(inventoryScroll);
questItemsPanel = new JPanel();
questItemsPanel.setLayout(new BoxLayout(questItemsPanel, BoxLayout.Y_AXIS));
questItemsPanel.add(questItems);
//questItemsPanel.add(questItemsScroll);
healthPanel = new JPanel();
healthPanel.setLayout(new BoxLayout(healthPanel, BoxLayout.Y_AXIS));
healthPanel.add(health);
controls = new JPanel();
controls.setBackground(Color.ORANGE);
main = new JPanel();
main.setBackground(Color.RED);
// Text Fields
inventoryText = new JTextArea(10, 20);
questItemsText = new JTextArea(10, 20);
levelText = new JTextArea(10, 20);
inventoryText.setEditable(false);
questItemsText.setEditable(false);
levelText.setEditable(false);
// buttons
start = new JButton("Start Game");
quit = new JButton("Quit");
pause = new JButton("Pause");
restart = new JButton("Restart");
start.addActionListener(this);
quit.addActionListener(this);
pause.addActionListener(this);
restart.addActionListener(this);
controls.add(start);
controls.add(quit);
controls.add(pause);
controls.add(restart);
// Scroll Panes
inventoryScroll = new JScrollPane(inventoryText);
questItemsScroll = new JScrollPane(questItemsText);
levelScroll = new JScrollPane(levelText);
// Initialize CardLayout and JPanel to hold different panels
cardLayout = new CardLayout();
cardPanel = new JPanel(cardLayout);
// Game Panel
gp = new GamePanel(this);
gp.setPreferredSize(new Dimension(1000, 700));
gamePanel = new GamePanel2();
gamePanel.setPreferredSize(new Dimension(1000, 700));
// Add GamePanel instances to cardPanel
cardPanel.add(gp, "gamePanel");
cardPanel.add(gamePanel, "gamePanel2");
//Main Panel
main.add(cardPanel);
// Show the first panel (GamePanel) initially
cardLayout.show(cardPanel, "gamePanel");
//main.add(gamePanel);
main.add(controls);
// msn must respond to keyboard and mouse
gamePanel.addMouseListener(this);
main.addKeyListener(this);
// Container
c = getContentPane();
c.setLayout(new BorderLayout());
c.add(main, BorderLayout.CENTER);
//c.add(controls, BorderLayout.SOUTH);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == start){
gp.gameSetup();
gp.gameState = gp.getPlayState();
gp.startGameThread();
// gamePanel.startGame();
}
if(e.getSource() == quit){
System.exit(0);
}
if(e.getSource() == pause){
if (level == 2) {
gamePanel.pauseGame();
}
}
if(e.getSource() == restart){
if (level == 2) {
gamePanel.startNewGame();
}
}
main.requestFocus();
}
// implement methods in KeyListener interface
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
directions[1] = true;
gamePanel.setDirections(directions);
}
//else
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
directions[2] = true;
gamePanel.setDirections(directions);
}
//else
if (e.getKeyCode() == KeyEvent.VK_UP) {
directions[3] = true;
gamePanel.setDirections(directions);
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
directions[0] = true;
gamePanel.setDirections(directions);
}
}
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
directions[1] = false;
gamePanel.setDirections(directions);
}
//else
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
directions[2] = false;
gamePanel.setDirections(directions);
}
//else
if (e.getKeyCode() == KeyEvent.VK_UP) {
directions[3] = false;
gamePanel.setDirections(directions);
}
//else
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
directions[0] = false;
gamePanel.setDirections(directions);
}
}
public void keyTyped(KeyEvent e) {
}
// implement methods in MouseListener interface
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void startLevel2(){
gp.endGameThread();
gamePanel.startGame();
cardLayout.show(cardPanel, "gamePanel2");
main.requestFocus();
level = 2;
return;
}
}