-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokeGUI.java
412 lines (357 loc) · 15 KB
/
PokeGUI.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
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.util.PriorityQueue;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayDeque;
/**
* Panels for Pokemon GUI.
* @author Ashley Bare, Hok Wai Chan
* @since 12/14/20
*/
public class PokeGUI extends JPanel {
/*** label for top with Pokemon logo image. ***/
private JLabel topHeader = new JLabel();
/*** label to display Pokemon image when hunting. ***/
private JLabel pokeDisplay = new JLabel();
/*** label to display caught or flee image. ***/
private JLabel caughtOrNot = new JLabel();
/*** button to hunt pokemon. ***/
private JButton bHunt = new JButton(" Hunt ");
/*** button to catch pokemon. ***/
private JButton bCatch = new JButton(" Catch ");
/*** button to print pokedex. ***/
private JButton bPokedex = new JButton(" Pokedex ");
/*** button to print backpack. ***/
private JButton bBackpack = new JButton(" Backpack ");
/*** radio button for sorting by number. ***/
private JRadioButton sortByNum = new JRadioButton("Number");
/*** radio button for sorting by most recent. ***/
private JRadioButton sortByRecent = new JRadioButton("Most Recent");
/*** button group to make sure only one selected. ***/
private ButtonGroup bGroup = new ButtonGroup();
/*** top sub-panel. ***/
private JPanel topSubPanel = new JPanel();
/*** center sub-panel. ***/
private JPanel centerSubPanel = new JPanel();
/*** bottom sub-panel. ***/
private JPanel bottomSubPanel = new JPanel();
/*** left sub-panel. ***/
private JPanel leftSubPanel = new JPanel(new GridLayout(2, 1));
/*** right sub-panel. ***/
private JPanel rightSubPanel = new JPanel(new GridLayout(2, 1));
/*** pop-up. ***/
private JPanel popUp = new JPanel();
/*** ActionListener to add to buttons. ***/
private GUIListener listener = new GUIListener();
/*** pokemon. ***/
private Pokemon pm;
/*** BST of Pokemon. ***/
private PokeTree pokedex = new PokeTree();
/*** PriorityQueue for backpack sort by number. ***/
private PriorityQueue<Pokemon> backpackNum = new PriorityQueue<Pokemon>();
/*** alt PriorityQueue for backpack sort by number. ***/
private PriorityQueue<Pokemon> altBackpackNum = new PriorityQueue<Pokemon>();
/*** stack for backpack sort by recent. ***/
private ArrayDeque<Pokemon> backpackRecent = new ArrayDeque<Pokemon>();
/*** alt stack for backpack sort by recent. ***/
private ArrayDeque<Pokemon> altBackpackRecent = new ArrayDeque<Pokemon>();
/*** counter for Pokemon in Pokedex. ***/
private int dexCount = 0;
/*** counter for Pokemon in Backpack. ***/
private int packCount = 0;
/*** String for displaying new pokemon. ***/
private String pokeOut = new String("");
/*** String for displaying Pokedex or backpack. ***/
private String pokeList = new String("");
/*** text area for displaying hunt and catch messages. ***/
private JTextArea textHuntCatch = new JTextArea(15, 20);
/*** text area for displaying pokedex or backpack. ***/
private JTextArea textArea = new JTextArea(10, 30);
/*** text area for popUp. ***/
private JTextArea popUpMessage = new JTextArea(2, 20);
/*** image icon for header. ***/
private ImageIcon header = new ImageIcon("IMAGE\\headerimage.png");
/*** image icon for catch button. ***/
private ImageIcon pokeball = new ImageIcon("IMAGE\\pokeball.png");
/*** image icon for backpack. ***/
private ImageIcon backpack = new ImageIcon("IMAGE\\backpack.png");
/*** image icon for pokedex. ***/
private ImageIcon pokedexImage = new ImageIcon("IMAGE\\pokedex.png");
/** image icon for pokemon ran. ***/
private ImageIcon ran = new ImageIcon("IMAGE\\ran.jpg");
/** image icon for bulbasaur 001. ***/
private ImageIcon bulbasaurIcon = new ImageIcon("IMAGE\\001.png");
/** image icon for ivysaur 002. ***/
private ImageIcon ivysaurIcon = new ImageIcon("IMAGE\\002.png");
/** image icon for venusaur 003. ***/
private ImageIcon venusaurIcon = new ImageIcon("IMAGE\\003.png");
/** image icon for charmander 004. ***/
private ImageIcon charmanderIcon = new ImageIcon("IMAGE\\004.png");
/** image icon for charmeleon 005. ***/
private ImageIcon charmeleonIcon = new ImageIcon("IMAGE\\005.png");
/** image icon for charizard 006. ***/
private ImageIcon charizardIcon = new ImageIcon("IMAGE\\006.png");
/** image icon for squirtle 007. ***/
private ImageIcon squirtleIcon = new ImageIcon("IMAGE\\007.png");
/** image icon for wartortle 008. ***/
private ImageIcon wartortleIcon = new ImageIcon("IMAGE\\008.png");
/** image icon for blastoise 009. ***/
private ImageIcon blastoiseIcon = new ImageIcon("IMAGE\\009.png");
/*** image icon for transferring icon. ***/
private ImageIcon activeIcon = new ImageIcon();
/*** Make bottom textArea scrollable. ***/
private JScrollPane scroll = new JScrollPane(textArea, JScrollPane.
VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.
HORIZONTAL_SCROLLBAR_NEVER);
/**
* Constructor for panels.
*/
public PokeGUI() {
this.setLayout(new BorderLayout()); //Border panel layout
this.setPreferredSize(new Dimension(600, 700));
topSubPanel.setBackground(Color.red);
centerSubPanel.setBackground(Color.red);
leftSubPanel.setBackground(Color.red);
rightSubPanel.setBackground(Color.red);
bottomSubPanel.setBackground(Color.white);
bottomSubPanel.setPreferredSize(new Dimension(600, 275));
//adding action listeners
bHunt.addActionListener(listener);
bCatch.addActionListener(listener);
bPokedex.addActionListener(listener);
bBackpack.addActionListener(listener);
sortByNum.addActionListener(listener);
sortByRecent.addActionListener(listener);
//TOP SUBPANEL
//header only in top subpanel
topHeader.setIcon(header);
topSubPanel.add(topHeader);
this.add("North", topSubPanel);
//LEFT SUBPANEL
leftSubPanel.setPreferredSize(new Dimension(175, 200));
bHunt.setBackground(Color.yellow);
leftSubPanel.add(bHunt);
pokeDisplay.setHorizontalAlignment(JLabel.CENTER);
leftSubPanel.add(pokeDisplay);
this.add("West", leftSubPanel);
//CENTER SUBPANEL
//format text area for display hunt and catch messages
textHuntCatch.setBackground(Color.white);
textHuntCatch.setEditable(false);
textHuntCatch.setBorder(BorderFactory.createLineBorder(Color.black));
centerSubPanel.add(textHuntCatch);
this.add("Center", centerSubPanel);
//RIGHT SUBPANEL
bCatch.setIcon(pokeball);
//validate();
bCatch.setEnabled(false);
rightSubPanel.setPreferredSize(new Dimension(175, 200));
rightSubPanel.add(bCatch);
caughtOrNot.setHorizontalAlignment(JLabel.CENTER);
rightSubPanel.add(caughtOrNot);
this.add("East", rightSubPanel);
//BOTTOM SUBPANEL
//adding message and buttons to bottom sub-panel
bPokedex.setIcon(pokedexImage);
validate();
bottomSubPanel.add(bPokedex);
bBackpack.setIcon(backpack);
validate();
bottomSubPanel.add(bBackpack);
//format textArea for holding list
textArea.setBackground(Color.red);
textArea.setEditable(false);
scroll.setBorder(null);
//add scrollPane to panel, textArea inside.
bottomSubPanel.add(scroll);
scroll.getVerticalScrollBar().setPreferredSize(new Dimension(14, 0));
this.add("South", bottomSubPanel);
//POPUP SUBPANEL
String s = "How would you like to sort?";
popUpMessage.setText(s);
popUpMessage.setEditable(false);
popUp.add(popUpMessage);
bGroup.add(sortByNum);
bGroup.add(sortByRecent);
popUp.add(sortByNum);
popUp.add(sortByRecent);
} //close PokeGUI constructor
/**
* Private ActionListener class.
*/
private class GUIListener implements ActionListener {
/**
* ActionPerformed method.
* @param event which button is clicked.
*/
public void actionPerformed(ActionEvent event) {
//if "Hunt" button clicked, random new pokemon
if (event.getSource() == bHunt) {
pm = newPoke();
pokeOut = ("A wild " + pm.getSpecies() + " appeared!\n"
+ "You can try to catch it or keep hunting.");
textHuntCatch.setText(pokeOut);
caughtOrNot.setIcon(null);
bCatch.setEnabled(true);
//else if "Catch" pressed, random catch/escape
} else if (event.getSource() == bCatch) {
boolean caughtIt;
Random randgen = new Random();
pokeOut = ("Trying to catch the " + pm.getSpecies() + "...\n");
textHuntCatch.setText(pokeOut);
caughtIt = randgen.nextBoolean();
//if caught, add to pokedex and backpack + update displays
if (caughtIt) {
pokeOut += ("\nNice, you caught it!\n\n"
+ "Here are the stats for"
+ " your new Pokemon: \n" + pm.toString());
textHuntCatch.setText(pokeOut);
pokeDisplay.setIcon(null);
caughtOrNot.setIcon(activeIcon);
validate();
pokedex.add(pm);
backpackNum.add(pm);
backpackRecent.push(pm);
dexCount++;
packCount++;
//else not caught, update displays
} else {
pokeDisplay.setIcon(null);
caughtOrNot.setIcon(ran);
validate();
pokeOut += ("\nAw man, it got away." + "\nTry hunting again.");
textHuntCatch.setText(pokeOut);
} //close if/else for catching pokemon
bCatch.setEnabled(false);
//END TOP BUTTONS
//else if "Pokedex" pressed, print BST inOrder
} else if (event.getSource() == bPokedex) {
if (dexCount > 0) {
textArea.setText(pokedex.toString());
} else {
textArea.setText("You haven't caught any pokemon yet.");
}
//else if "Backpack" pressed,popUp asking choice then print appropriate
} else if (event.getSource() == bBackpack) {
JOptionPane.showMessageDialog(textHuntCatch, popUp);
//nested if/else actionlistener for radio buttons
if (!sortByNum.isSelected() && !sortByRecent.isSelected()) {
pokeList = "You didn't select anything...";
textArea.setText(pokeList);
} else if (sortByNum.isSelected()) {
pokeList = "";
while (backpackNum.size() > 0) {
pm = backpackNum.poll();
altBackpackNum.add(pm);
pokeList += (pm.toString() + "\n\n");
}
textArea.setText(pokeList);
for (int i = 0; i < packCount; i++) {
pm = altBackpackNum.poll();
backpackNum.add(pm);
} //close for refilling
} else if (sortByRecent.isSelected()) {
pokeList = "";
while (backpackRecent.size() > 0) {
pm = backpackRecent.poll();
altBackpackRecent.push(pm);
pokeList += (pm.toString() + "\n\n");
}
textArea.setText(pokeList);
for (int i = 0; i < packCount; i++) {
pm = altBackpackRecent.poll();
backpackRecent.add(pm);
} //close for refilling
} //close nested radio button if/else
bGroup.clearSelection();
validate();
} //close backpack action listener
} //close actionEvent method
/**
* helper method for ActionListener.
* randomly generates new pokemon.
* @return the new pokemon
*/
private Pokemon newPoke() {
Random randgen = new Random();
int randNum = 0;
Pokemon p = null;
randNum = randgen.nextInt(9) + 1;
switch(randNum) {
case 1:
activeIcon = bulbasaurIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Bulbasaur();
break;
case 2:
activeIcon = ivysaurIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Ivysaur();
break;
case 3:
activeIcon = venusaurIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Venusaur();
break;
case 4:
activeIcon = charmanderIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Charmander();
break;
case 5:
activeIcon = charmanderIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Charmeleon();
break;
case 6:
activeIcon = charizardIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Charizard();
break;
case 7:
activeIcon = squirtleIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Squirtle();
break;
case 8:
activeIcon = wartortleIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Wartortle();
break;
case 9:
activeIcon = blastoiseIcon;
pokeDisplay.setIcon(activeIcon);
validate();
p = new Blastoise();
break;
default: //not a valid menu entry
System.out.println("\nSorry, no pokemon here.");
break;
} //close switch
return p;
} //close newPoke method
} // end GUIListener private class
} //end PokeGUI class