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
|
package esieequest.view.app;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import com.wordpress.tipsforjava.swing.StretchIcon;
import esieequest.controller.GameEngine;
import esieequest.model.Game;
import esieequest.view.View;
/**
* The Swing based graphical user interface.
*
* @author Pacien TRAN-GIRARD
*/
abstract class UserInterface implements View, ActionListener {
protected Game game;
private GameEngine gameEngine;
private JPanel layout;
private JTextPane questTextPane;
private JTextPane infoTextPane;
private JLabel imageLabel;
private HashMap<String, JButton> gameButtons;
private HashMap<String, JButton> controlButtons;
private JTextField inputField;
/**
* The default constructor.
*/
public UserInterface() {
this.buildUI();
this.inputField.addActionListener(this);
this.setControlsState(false);
this.inputField.setEnabled(true);
}
/**
* Creates the interface widgets.
*/
private void buildUI() {
this.layout = new JPanel();
this.layout.setLayout(new BorderLayout(0, 0));
final JPanel menuPanel = new JPanel();
this.layout.add(menuPanel, BorderLayout.NORTH);
menuPanel.setLayout(new BorderLayout(0, 0));
final JPanel questPanel = new JPanel();
menuPanel.add(questPanel, BorderLayout.CENTER);
this.questTextPane = new JTextPane();
this.questTextPane.setEditable(false);
this.questTextPane.setText("ESIEEquest");
questPanel.add(this.questTextPane);
final JPanel gamePanel = new JPanel();
menuPanel.add(gamePanel, BorderLayout.WEST);
final JButton newButton = new JButton("New");
newButton.setToolTipText("New game");
gamePanel.add(newButton);
final JButton soundButton = new JButton("Sound");
soundButton.setToolTipText("Toggle sound");
gamePanel.add(soundButton);
final JPanel filePanel = new JPanel();
menuPanel.add(filePanel, BorderLayout.EAST);
final JButton loadButton = new JButton("Load");
loadButton.setToolTipText("Load a game");
filePanel.add(loadButton);
final JButton saveButton = new JButton("Save");
saveButton.setToolTipText("Save the current game");
filePanel.add(saveButton);
final JPanel imagePanel = new JPanel();
this.layout.add(imagePanel, BorderLayout.CENTER);
imagePanel.setLayout(new BorderLayout(0, 0));
this.imageLabel = new JLabel();
imagePanel.add(this.imageLabel);
final JPanel userPanel = new JPanel();
this.layout.add(userPanel, BorderLayout.SOUTH);
userPanel.setLayout(new BorderLayout(0, 0));
final JPanel dispPanel = new JPanel();
dispPanel.setBorder(new EmptyBorder(5, 5, 5, 0));
userPanel.add(dispPanel, BorderLayout.CENTER);
dispPanel.setLayout(new BorderLayout(0, 0));
this.infoTextPane = new JTextPane();
this.infoTextPane.setEditable(false);
this.infoTextPane.setText("Welcome to ESIEEquest!");
dispPanel.add(this.infoTextPane);
this.inputField = new JTextField();
dispPanel.add(this.inputField, BorderLayout.SOUTH);
this.inputField.setColumns(10);
final JPanel controlPanel = new JPanel();
controlPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
userPanel.add(controlPanel, BorderLayout.EAST);
controlPanel.setLayout(new BorderLayout(0, 0));
final JPanel topControlPanel = new JPanel();
controlPanel.add(topControlPanel, BorderLayout.NORTH);
topControlPanel.setLayout(new BorderLayout(0, 0));
final JButton forwardButton = new JButton("↥");
forwardButton.setFont(new Font("Dialog", Font.BOLD, 19));
forwardButton.setToolTipText("Go forward");
topControlPanel.add(forwardButton, BorderLayout.CENTER);
final JButton inventoryButton = new JButton("⇱");
inventoryButton.setFont(new Font("Dialog", Font.BOLD, 19));
inventoryButton.setToolTipText("Use item");
topControlPanel.add(inventoryButton, BorderLayout.WEST);
final JButton actionButton = new JButton("⇲");
actionButton.setFont(new Font("Dialog", Font.BOLD, 19));
actionButton.setToolTipText("Talk/Take item");
topControlPanel.add(actionButton, BorderLayout.EAST);
final JPanel bottomControlPanel = new JPanel();
controlPanel.add(bottomControlPanel, BorderLayout.SOUTH);
bottomControlPanel.setLayout(new BorderLayout(0, 0));
final JButton backButton = new JButton("↧");
backButton.setFont(new Font("Dialog", Font.BOLD, 19));
backButton.setToolTipText("Go back");
bottomControlPanel.add(backButton, BorderLayout.CENTER);
final JButton leftButton = new JButton("↰");
leftButton.setFont(new Font("Dialog", Font.BOLD, 19));
leftButton.setToolTipText("Turn left");
bottomControlPanel.add(leftButton, BorderLayout.WEST);
final JButton rightButton = new JButton("↱");
rightButton.setFont(new Font("Dialog", Font.BOLD, 19));
rightButton.setToolTipText("Turn right");
bottomControlPanel.add(rightButton, BorderLayout.EAST);
final Dimension squareButton = new Dimension(50, 50);
forwardButton.setPreferredSize(squareButton);
inventoryButton.setPreferredSize(squareButton);
actionButton.setPreferredSize(squareButton);
backButton.setPreferredSize(squareButton);
leftButton.setPreferredSize(squareButton);
rightButton.setPreferredSize(squareButton);
newButton.setActionCommand("new");
soundButton.setActionCommand("sound");
loadButton.setActionCommand("load");
saveButton.setActionCommand("save");
forwardButton.setActionCommand("forward");
inventoryButton.setActionCommand("inventory");
actionButton.setActionCommand("do");
backButton.setActionCommand("back");
leftButton.setActionCommand("turn left");
rightButton.setActionCommand("turn right");
this.gameButtons = new HashMap<String, JButton>();
this.gameButtons.put("newButton", newButton);
this.gameButtons.put("soundButton", soundButton);
this.gameButtons.put("loadButton", loadButton);
this.gameButtons.put("saveButton", saveButton);
this.controlButtons = new HashMap<String, JButton>();
this.controlButtons.put("forwardButton", forwardButton);
this.controlButtons.put("inventoryButton", inventoryButton);
this.controlButtons.put("actionButton", actionButton);
this.controlButtons.put("backButton", backButton);
this.controlButtons.put("leftButton", leftButton);
this.controlButtons.put("rightButton", rightButton);
}
/**
* Returns the layout.
*
* @return the layout
*/
public JPanel getLayout() {
return this.layout;
}
/**
* Sets the action listener for the given JButtons.
*
* @param hashMap
* the map of the JButtons
* @param actionListener
* the action listener
*/
private void setActionListener(final HashMap<String, JButton> hashMap,
final ActionListener actionListener) {
for (final JButton vButton : hashMap.values()) {
vButton.addActionListener(actionListener);
}
}
/**
* Sets the action listener for all UI elements.
*
* @param actionListener
* the action listener
*/
public void setActionListener(final ActionListener actionListener) {
this.inputField.addActionListener(actionListener);
this.setActionListener(this.gameButtons, actionListener);
this.setActionListener(this.controlButtons, actionListener);
}
/**
* Clears the textual input field.
*/
private void clearInputField() {
this.inputField.setText(null);
}
/**
* Sets the controls state.
*
* @param state
* the controls state
*/
private void setControlsState(final boolean state) {
this.inputField.setEnabled(state);
for (final JButton vButton : this.controlButtons.values()) {
vButton.setEnabled(state);
}
}
/**
* Sets the quest title.
*
* @param quest
* the quest title
*/
private void setQuest(final String quest) {
this.questTextPane.setText(quest);
}
/**
* Updates the room illustration.
*/
private void updateIllustration() {
String imageName = this.game.getLocationImageName();
if (imageName == null) {
imageName = "res/img/placeholder.jpg";
}
final URL imageURL = this.getClass().getClassLoader().getResource(imageName);
final StretchIcon imageIcon = new StretchIcon(imageURL, true);
this.imageLabel.setIcon(imageIcon);
}
/**
* Translates the received ActionEvent into the corresponding game command
* and forwards it to the game engine.
*/
@Override
public void actionPerformed(final ActionEvent actionEvent) {
this.gameEngine.interpret(actionEvent.getActionCommand());
}
@Override
public void setModel(final Game game) {
this.game = game;
}
@Override
public void setController(final GameEngine gameEngine) {
this.gameEngine = gameEngine;
}
@Override
public void echo(final String message) {
this.infoTextPane.setText(message);
this.clearInputField();
}
@Override
public void enable() {
}
@Override
public void disable() {
}
@Override
public void refresh() {
// TODO
}
}
|