-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGui.java
422 lines (332 loc) · 12 KB
/
Gui.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
415
416
417
418
419
420
421
422
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.HashMap;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.text.DefaultCaret;
import java.awt.Insets;
/**
* The GUI class which implements the user interface and displays images of the rooms, items and inventories.
*
* @author 162224
* @version 4
*/
public class Gui
{
private boolean nameEntered = false;
private Game _game;
private JFrame frame;
private JPanel _roomPanel;
private JPanel _interactPanel;
private JPanel _inventoryPanel;
private JPanel _itemsPanel;
private JPanel _creaturePanel;
private JPanel _bossPanel;
private JPanel _itemHead;
private JLabel itemHead;
private JPanel _creatureHead;
private JLabel creatureHead;
private JPanel _bossHead;
private JLabel bossHead;
private JTextArea _log;
private JTextField _input;
private File userLog;
public Gui()
{
Create();
Logger.setLogger(_log);
_game = new Game();
_game.welcomePartOne();
updateUi();
}
/**
* Populates the Jframe
*/
public JFrame Create(){
frame = new JFrame("Magical Realm Land");
JMenuBar menu = new JMenuBar();
frame.setJMenuBar(menu);
addMenu(menu);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,850);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
_roomPanel = createRoomPanel();
_roomPanel.setPreferredSize(new Dimension(660, 520));
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.gridheight = 3;
frame.add(_roomPanel, gbc);
_inventoryPanel = createInventoryPanel();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
frame.add(_inventoryPanel, gbc);
JPanel _logPanel = createLogPanel();
//_log = new JTextArea();
//_log.setEditable(false);
//_log.setLineWrap(true);
//_log.setWrapStyleWord(true);
//JScrollPane scroll = new JScrollPane (_log,
//JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
gbc.fill = GridBagConstraints.BOTH;
//gbc.anchor = GridBagConstraints.NONE;
gbc.gridx = 3;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.gridheight = 4;
frame.add(_logPanel, gbc);
_interactPanel = createInteractPanel();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.PAGE_END;
//gbc.insets = new Insets(10,0,0,0);
gbc.weighty = 0.25;
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 6;
gbc.gridheight = 1;
frame.add(_interactPanel, gbc);
frame.setVisible(true);
frame.pack();
return frame;
}
/**
* Creates the panel to display the room. Items in the room are called upon in the Room class
*/
private JPanel createRoomPanel(){
JPanel result = new JPanel();
//result.setLayout(new BorderLayout());
//result.setBounds(0, 0, 650, 500);
result.setBackground(Color.BLACK);
return result;
}
/**
* This is the panel which contains the three types of user inventory - the items, creatures and bosses defeated
*/
private JPanel createInventoryPanel(){
JPanel result = new JPanel();
result.setLayout(new GridLayout(1, 3));
result.setPreferredSize(new Dimension(600, 200));
result.setBackground(Color.BLUE);
JPanel _allItemPanel = createItemsPanel();
result.add(_allItemPanel);
JPanel _allcreaturePanel = createCreaturePanel();
result.add(_allcreaturePanel);
JPanel _allbossPanel = createBossPanel();
result.add(_allbossPanel);
return result;
}
/**
* Creates the user creature friend panel with a seperate header
*/
private JPanel createCreaturePanel(){
JPanel result = new JPanel();
result.setLayout(new BorderLayout());
JLabel temp = new JLabel("Friends");
_creatureHead = new JPanel();
_creatureHead.add(temp);
_creatureHead.setBackground(Color.GRAY);
result.add(_creatureHead, BorderLayout.NORTH);
_creaturePanel = new JPanel();
_creaturePanel.setLayout(new FlowLayout());
_creaturePanel.setBackground(Color.BLACK);
result.add( _creaturePanel, BorderLayout.CENTER);
result.setBorder(BorderFactory.createLineBorder(Color.GRAY));
return result;
}
/**
* Creates the user item inventory panel with a seperate header
*/
private JPanel createItemsPanel(){
JPanel result = new JPanel();
result.setLayout(new BorderLayout());
JLabel temp = new JLabel("Items");
_itemHead = new JPanel();
_itemHead.add(temp);
_itemHead.setBackground(Color.LIGHT_GRAY);
result.add(_itemHead, BorderLayout.NORTH);
_itemsPanel = new JPanel();
_itemsPanel.setLayout(new FlowLayout());
_itemsPanel.setBackground(Color.BLACK);
result.add(_itemsPanel, BorderLayout.CENTER);
result.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
return result;
}
/**
* Creates the user boss defeated panel with a seperate header
*/
private JPanel createBossPanel(){
JPanel result = new JPanel();
result.setLayout(new BorderLayout());
JLabel temp = new JLabel("Defeated");
temp.setForeground(Color.GRAY);
_bossHead = new JPanel();
_bossHead.add(temp);
_bossHead.setBackground(Color.DARK_GRAY);
result.add(_bossHead, BorderLayout.NORTH);
_bossPanel = new JPanel();
_bossPanel.setLayout(new FlowLayout());
_bossPanel.setBackground(Color.BLACK);
result.add(_bossPanel, BorderLayout.CENTER);
result.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
return result;
}
/**
* Creates the panel which contains the user input field, a 'go' button (that works the same as pressing enter)
* and a header
*/
private JPanel createInteractPanel(){
JPanel result = new JPanel();
//result.setPreferredSize(new Dimension(100,100));
result.setLayout(new BorderLayout());
_input = new JTextField();
//_log.setPreferredSize( new Dimension(100, 100) );
JLabel _label = new JLabel("What would you like to do?");
JButton _go = new JButton("Go!");
_go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
processCommand();
}
});
_input.addActionListener(new ActionListener() {
// for a JTextField, this is only called when enter is pressed
public void actionPerformed(ActionEvent e){
processCommand();
}
});
result.add(_input,BorderLayout.CENTER);
result.add(_label,BorderLayout.NORTH);
result.add(_go,BorderLayout.EAST);
return result;
}
/**
* Creates the scrollable panel which contains a log of the users journey and actions
*/
private JPanel createLogPanel(){
JPanel result = new JPanel();
result.setPreferredSize(new Dimension (300,700));
result.setLayout(new BorderLayout());
_log = new JTextArea();
_log.setEditable(false);
_log.setLineWrap(true);
_log.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(_log,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
result.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
result.add(scroll, BorderLayout.CENTER);
return result;
}
/**
* Processes user commands - checks if the username has been entered so that it can be set.
*/
private void processCommand(){
String command = _input.getText();
if (!nameEntered){
_game.player.playerName = command;
nameEntered = true;
createPanelNames();
_input.setText("");
_game.welcomePartTwo();
return;
}
_input.setText("");
_game.guiCommand(command);
updateUi();
}
/**
* Customises each header for the inventory panels for the specific user once the player name is set
*/
private void createPanelNames() {
String user = _game.player.playerName;
itemHead = new JLabel (user + "'s Items");
_itemHead.removeAll();
_itemHead.add(itemHead);
JLabel creatureHead = new JLabel (user + "'s friends");
//creatureHead.setForeground(Color.WHITE);
_creatureHead.removeAll();
_creatureHead.add(creatureHead);
JLabel bossHead = new JLabel (user + " Defeated");
bossHead.setForeground(Color.GRAY);
_bossHead.removeAll();
_bossHead.add(bossHead);
repaintAll();
}
/**
* Updates GUI images
*/
private void repaintAll(){
_roomPanel.revalidate();
_roomPanel.repaint();
_inventoryPanel.revalidate();
_inventoryPanel.repaint();
_interactPanel.revalidate();
_interactPanel.repaint();
}
/**
* Creates menu items - stuck and quit
*/
public void addMenu(JMenuBar menu)
{
JMenuItem stuck = new JMenuItem("Stuck...?");
stuck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {printHelp(); }
});
menu.add(stuck);
JMenuItem quit = new JMenuItem("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
_game.quitGame();
System.exit(0); }
});
menu.add(quit);
}
/**
* Print out some help information.
*/
private void printHelp()
{
System.out.println("You have somehow ended up in this strange, magical land. But really you just want to go home.");
System.out.println();
System.out.println("Your command words are:");
_game.parser.showCommands();
System.out.println("You may view your inventory or your companions");
}
/**
* Updates the UI graphics
*/
private void updateUi(){
_roomPanel.removeAll();
JPanel thing = _game.currentRoom.getUI();
_roomPanel.add(thing);
_itemsPanel.removeAll();
_itemsPanel.add(updateDrawables(_game.getInventory()));
_creaturePanel.removeAll();
_creaturePanel.add(updateDrawables(_game.getCreatures()));
_bossPanel.removeAll();
_bossPanel.add(updateDrawables(_game.getBosses()));
repaintAll();
}
/**
* Retrieves user inventories to add them to the appropriate panels
*/
private <T extends Drawable>JPanel updateDrawables(HashMap<String, T> items){
JPanel result = new JPanel();
result.setBackground(Color.BLACK);
for(T item : items.values()){
result.add(item.getSmallUi());
}
return result;
}
}