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
|
package esieequest.view.app;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import esieequest.controller.GameEngine;
import esieequest.model.Game;
import esieequest.view.View;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.HashMap;
import javax.swing.JTextField;
import com.wordpress.tipsforjava.swing.StretchIcon;
import java.awt.Font;
public abstract class UserInterface implements View, ActionListener {
protected Game aGame;
private GameEngine aGameEngine;
private JPanel aLayout;
private JTextPane aQuestTextPane;
private JTextPane aInfoTextPane;
private JLabel aImageLabel;
private HashMap<String, JButton> aGameButtons;
private HashMap<String, JButton> aControlButtons;
private JTextField aInputField;
/**
* Create the panel.
*/
public UserInterface() {
this.buildUI();
this.aInputField.addActionListener(this);
this.setControlsState(false);
aInputField.setEnabled(true);
}
private void buildUI() {
this.aLayout = new JPanel();
this.aLayout.setLayout(new BorderLayout(0, 0));
JPanel vMenuPanel = new JPanel();
this.aLayout.add(vMenuPanel, BorderLayout.NORTH);
vMenuPanel.setLayout(new BorderLayout(0, 0));
JPanel vQuestPanel = new JPanel();
vMenuPanel.add(vQuestPanel, BorderLayout.CENTER);
this.aQuestTextPane = new JTextPane();
this.aQuestTextPane.setEditable(false);
this.aQuestTextPane.setText("ESIEEquest");
vQuestPanel.add(this.aQuestTextPane);
JPanel vGamePanel = new JPanel();
vMenuPanel.add(vGamePanel, BorderLayout.WEST);
JButton vNewButton = new JButton("New");
vNewButton.setToolTipText("New game");
vGamePanel.add(vNewButton);
JButton vSoundButton = new JButton("Sound");
vSoundButton.setToolTipText("Toggle sound");
vGamePanel.add(vSoundButton);
JPanel filePanel = new JPanel();
vMenuPanel.add(filePanel, BorderLayout.EAST);
JButton vLoadButton = new JButton("Load");
vLoadButton.setToolTipText("Load a game");
filePanel.add(vLoadButton);
JButton vSaveButton = new JButton("Save");
vSaveButton.setToolTipText("Save the current game");
filePanel.add(vSaveButton);
JPanel vImagePanel = new JPanel();
aLayout.add(vImagePanel, BorderLayout.CENTER);
vImagePanel.setLayout(new BorderLayout(0, 0));
this.aImageLabel = new JLabel();
vImagePanel.add(this.aImageLabel);
JPanel vUserPanel = new JPanel();
this.aLayout.add(vUserPanel, BorderLayout.SOUTH);
vUserPanel.setLayout(new BorderLayout(0, 0));
JPanel vDispPanel = new JPanel();
vDispPanel.setBorder(new EmptyBorder(5, 5, 5, 0));
vUserPanel.add(vDispPanel, BorderLayout.CENTER);
vDispPanel.setLayout(new BorderLayout(0, 0));
this.aInfoTextPane = new JTextPane();
this.aInfoTextPane.setEditable(false);
this.aInfoTextPane.setText("Welcome to ESIEEquest!");
vDispPanel.add(this.aInfoTextPane);
aInputField = new JTextField();
vDispPanel.add(aInputField, BorderLayout.SOUTH);
aInputField.setColumns(10);
JPanel vControlPanel = new JPanel();
vControlPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
vUserPanel.add(vControlPanel, BorderLayout.EAST);
vControlPanel.setLayout(new BorderLayout(0, 0));
JPanel vTopControlPanel = new JPanel();
vControlPanel.add(vTopControlPanel, BorderLayout.NORTH);
vTopControlPanel.setLayout(new BorderLayout(0, 0));
JButton vForwardButton = new JButton("↥");
vForwardButton.setFont(new Font("Dialog", Font.BOLD, 19));
vForwardButton.setToolTipText("Go forward");
vTopControlPanel.add(vForwardButton, BorderLayout.CENTER);
JButton vInventoryButton = new JButton("⇱");
vInventoryButton.setFont(new Font("Dialog", Font.BOLD, 19));
vInventoryButton.setToolTipText("Use item");
vTopControlPanel.add(vInventoryButton, BorderLayout.WEST);
JButton vActionButton = new JButton("⇲");
vActionButton.setFont(new Font("Dialog", Font.BOLD, 19));
vActionButton.setToolTipText("Talk/Take item");
vTopControlPanel.add(vActionButton, BorderLayout.EAST);
JPanel vBottomControlPanel = new JPanel();
vControlPanel.add(vBottomControlPanel, BorderLayout.SOUTH);
vBottomControlPanel.setLayout(new BorderLayout(0, 0));
JButton vBackButton = new JButton("↧");
vBackButton.setFont(new Font("Dialog", Font.BOLD, 19));
vBackButton.setToolTipText("Go back");
vBottomControlPanel.add(vBackButton, BorderLayout.CENTER);
JButton vLeftButton = new JButton("↰");
vLeftButton.setFont(new Font("Dialog", Font.BOLD, 19));
vLeftButton.setToolTipText("Turn left");
vBottomControlPanel.add(vLeftButton, BorderLayout.WEST);
JButton vRightButton = new JButton("↱");
vRightButton.setFont(new Font("Dialog", Font.BOLD, 19));
vRightButton.setToolTipText("Turn right");
vBottomControlPanel.add(vRightButton, BorderLayout.EAST);
Dimension vSquareButton = new Dimension(50, 50);
vForwardButton.setPreferredSize(vSquareButton);
vInventoryButton.setPreferredSize(vSquareButton);
vActionButton.setPreferredSize(vSquareButton);
vBackButton.setPreferredSize(vSquareButton);
vLeftButton.setPreferredSize(vSquareButton);
vRightButton.setPreferredSize(vSquareButton);
vNewButton.setActionCommand("new");
vSoundButton.setActionCommand("sound");
vLoadButton.setActionCommand("load");
vSaveButton.setActionCommand("save");
vForwardButton.setActionCommand("forward");
vInventoryButton.setActionCommand("inventory");
vActionButton.setActionCommand("do");
vBackButton.setActionCommand("back");
vLeftButton.setActionCommand("turn left");
vRightButton.setActionCommand("turn right");
this.aGameButtons = new HashMap<String, JButton>();
this.aGameButtons.put("vNewButton", vNewButton);
this.aGameButtons.put("vSoundButton", vSoundButton);
this.aGameButtons.put("vLoadButton", vLoadButton);
this.aGameButtons.put("vSaveButton", vSaveButton);
this.aControlButtons = new HashMap<String, JButton>();
this.aControlButtons.put("vForwardButton", vForwardButton);
this.aControlButtons.put("vInventoryButton", vInventoryButton);
this.aControlButtons.put("vActionButton", vActionButton);
this.aControlButtons.put("vBackButton", vBackButton);
this.aControlButtons.put("vLeftButton", vLeftButton);
this.aControlButtons.put("vRightButton", vRightButton);
}
public void actionPerformed(final ActionEvent pActionEvent) {
this.aGameEngine.interpret(pActionEvent.getActionCommand());
}
public JPanel getLayout() {
return this.aLayout;
}
private void setActionListener(final HashMap<String, JButton> vHashMap,
final ActionListener pActionListener) {
for (JButton vButton : vHashMap.values()) {
vButton.addActionListener(pActionListener);
}
}
public void setActionListener(final ActionListener pActionListener) {
this.aInputField.addActionListener(pActionListener);
this.setActionListener(this.aGameButtons, pActionListener);
this.setActionListener(this.aControlButtons, pActionListener);
}
private void clearInputField() {
this.aInputField.setText(null);
}
private void setControlsState(final boolean pState) {
this.aInputField.setEnabled(pState);
for (JButton vButton : this.aControlButtons.values()) {
vButton.setEnabled(pState);
}
}
private void setQuest(final String pQuest) {
this.aQuestTextPane.setText(pQuest);
}
private void updateIllustration() {
String vImageName = this.aGame.getLocationImageName();
if (vImageName == null) {
vImageName = "res/img/placeholder.jpg";
}
URL vImageURL = this.getClass().getClassLoader().getResource(vImageName);
StretchIcon vImageIcon = new StretchIcon(vImageURL, true);
this.aImageLabel.setIcon(vImageIcon);
}
@Override
public void setModel(Game pGame) {
this.aGame = pGame;
}
@Override
public void setController(GameEngine pGameEngine) {
this.aGameEngine = pGameEngine;
}
@Override
public void echo(final String pMessage) {
this.aInfoTextPane.setText(pMessage);
this.clearInputField();
}
@Override
public void refresh() {
// TODO Auto-generated method stub
}
}
|