generated from techfolios/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonPanel.java
More file actions
326 lines (279 loc) · 11.5 KB
/
PokemonPanel.java
File metadata and controls
326 lines (279 loc) · 11.5 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
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
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; //add this for the listener
import java.util.Random;
/**
* Assignment 9: Pokemon GUI - the Panel.
* @author Kairi Tanaka & Jamie Laurin
* @since 12/03/2021
*/
public class PokemonPanel extends JPanel {
/********** Top Section Instance Variables *************/
/******* button. ****************/
private JButton bHunt = new JButton(" Hunt ");
/******* button. ****************/
private JButton bCatch = new JButton(" Catch ");
/****** text are for displaying Pokemon. *******/
private JTextField tfPokemon = new JTextField(25);
/****** text are for displaying Pokemon. *******/
private JTextArea textArea = new JTextArea(14, 15);
/****** text are for displaying Pokemon. *******/
private JTextArea textArea2 = new JTextArea(2, 15);
/** Make TextArea scrollable. **********************/
private JScrollPane tScroll = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
/** Make TextArea scrollable. **********************/
private JScrollPane tScroll2 = new JScrollPane(textArea2,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
/********* Bottom Section Instance Variables ************/
/******* button. ****************/
private JButton bPokedex = new JButton(" Pokedex ");
/******* button. ****************/
private JButton bBackpack = new JButton(" Backpack ");
/******* pop up button. *********/
private JButton bRecent = new JButton(" Recent ");
/******* pop up button. *********/
private JButton bNumber = new JButton(" Number ");
/********** Choice drop down menu for Pokemon Ordering. **/
private Choice chOrdering = new Choice();
/****** text are for displaying Pokemon. *******/
private JTextArea btextArea = new JTextArea(15, 15);
/** Make TextArea scrollable. **********************/
private JScrollPane botScroll = new JScrollPane(btextArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
/******** Pokemon Instance Variables **************/
/******** Pokemon. *********/
private Pokemon p = new Bulbasaur();
/******** Pokemon tree. *********/
private PokeTree pokeTree = new PokeTree();
/******** Pokemon Queue. *********/
private PriorityQueue<Pokemon> pq = new PriorityQueue<>();
/******** Pokemon Stack. *********/
private Deque<Pokemon> stack = new ArrayDeque<>();
/********* sub-panel. *********/
private JPanel topSubPanel = new JPanel();
/********* sub-panel. *********/
private JPanel centerSubPanel = new JPanel();
/********* sub-panel. *********/
private JPanel bottomSubPanel = new JPanel();
/** we can declare and initialize ActionListener obj. */
private GUIListener listener = new GUIListener();
/** Constructor. */
public PokemonPanel() {
GridLayout gl = new GridLayout(1, 2);
this.setLayout(new BorderLayout()); //Border panel layout
this.setPreferredSize(new Dimension(400, 700));
topSubPanel.setBackground(Color.orange); //north area color
topSubPanel.setPreferredSize(new Dimension(400, 300));
centerSubPanel.setBackground(Color.lightGray); //center area color
centerSubPanel.setPreferredSize(new Dimension(500, 30));
bottomSubPanel.setBackground(Color.gray); //south area color
bottomSubPanel.setPreferredSize(new Dimension(500, 100));
bottomSubPanel.setLayout(gl);
//top panel
topSubPanel.setBorder((BorderFactory.createTitledBorder("Catch a Pokemon!")));
textArea2.setBackground(Color.white); //set up text area
textArea2.setBorder(BorderFactory.createLineBorder(Color.black));
textArea2.setEditable(false);
topSubPanel.add(tScroll2); //add scroll bar in text area
tScroll2.getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
textArea.setBackground(Color.white); //set up text area
textArea.setBorder(BorderFactory.createLineBorder(Color.black));
textArea.setEditable(false);
topSubPanel.add(tScroll); //add scroll bar in text area
tScroll.getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
topSubPanel.add(bHunt); //add hunt button
bHunt.addActionListener(listener);
topSubPanel.add(bCatch); //add catch buttton
bCatch.addActionListener(listener);
this.add("North", topSubPanel);
//center panel
centerSubPanel.setBorder((BorderFactory.createTitledBorder("Your Pokemon!")));
btextArea.setBackground(Color.white); //set up text area
btextArea.setBorder(BorderFactory.createLineBorder(Color.black));
btextArea.setEditable(false);
centerSubPanel.add(botScroll); //add scroll bar in text area
botScroll.getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
add("Center", centerSubPanel);
//bottom panel
bottomSubPanel.setBorder((BorderFactory.createTitledBorder("Select one to see your Pokemon!")));
bottomSubPanel.add(bPokedex); //add pokedex button
bPokedex.addActionListener(listener);
bottomSubPanel.add(bBackpack); //add backpack button
bBackpack.addActionListener(listener);
bottomSubPanel.add(chOrdering);
bBackpack.addActionListener(listener);
chOrdering.add("Number");
bNumber.addActionListener(listener);
chOrdering.add("Recent");
bRecent.addActionListener(listener);
add("South", bottomSubPanel);
}
/** Private ActionListener class. */
private class GUIListener implements ActionListener {
/** random number generated. */
private int num = 0;
/** random pokemon. */
private Pokemon pTemp;
/**
* Action Performed method.
* @param event button clicked by user.
*/
public void actionPerformed(ActionEvent event) {
String poke = "";
Random ranGen = new Random();
//if "Hunt" button is selected
if (event.getSource() == bHunt) {
int index = ranGen.nextInt(9) + 1;
switch (index) {
case 1:
poke = "Bulbasaur ";
num = 1;
break;
case 2:
poke = "Ivysaur ";
num = 2;
break;
case 3:
poke = "Venusaur ";
num = 3;
break;
case 4:
poke = "Charmander ";
num = 4;
break;
case 5:
poke = "Charmeleon ";
num = 5;
break;
case 6:
poke = "Charizard ";
num = 6;
break;
case 7:
poke = "Squirtle ";
num = 7;
break;
case 8:
poke = "Wartortle ";
num = 8;
break;
case 9:
poke = "Blastoise ";
num = 9;
break;
default:
System.out.println("Invalid input");
break;
}
//displays a random Pokemon which appears
textArea2.setText("A wild " + poke + "appeared!");
}
//if "Catch" button is selected
if (event.getSource() == bCatch) {
Random randGen = new Random();
int ranCatch = randGen.nextInt(2);
String msgCatch = "";
String pInfo = "";
switch (num) {
case 1:
poke = "Bulbasaur";
pTemp = new Bulbasaur();
pInfo = pTemp.toString();
break;
case 2:
poke = "Ivysaur";
pTemp = new Ivysaur();
pInfo = pTemp.toString();
break;
case 3:
poke = "Venusaur";
pTemp = new Venusaur();
pInfo = pTemp.toString();
break;
case 4:
poke = "Charmander";
pTemp = new Charmander();
pInfo = pTemp.toString();
break;
case 5:
poke = "Charmeleon";
pTemp = new Charmeleon();
pInfo = pTemp.toString();
break;
case 6:
poke = "Charizard";
pTemp = new Charizard();
pInfo = pTemp.toString();
break;
case 7:
poke = "Squirtle";
pTemp = new Squirtle();
pInfo = pTemp.toString();
break;
case 8:
poke = "Wartortle";
pTemp = new Wartortle();
case 9:
poke = "Blastoise";
pTemp = new Blastoise();
pInfo = pTemp.toString();
break;
default:
System.out.println("Invalid input");
break;
}
//randomly decides whether or not Pokemon is caught
switch (ranCatch) {
case 0:
msgCatch = poke + " Escaped!";
textArea.setText("Keep Hunting!");
textArea2.setText(msgCatch);
break;
case 1:
msgCatch = "You Caught " + poke + "!";
textArea.setText(pInfo);
//displays Pokemon Caught
textArea2.setText(msgCatch);
//adds Pokemon to each list
pokeTree.add(pTemp);
pq.add(pTemp);
stack.push(pTemp);
break;
default:
System.out.println("Invalid input");
break;
}
}
//if "Pokedex" button is selected
if (event.getSource() == bPokedex) {
String printTree = pokeTree.printPokemonTree();
btextArea.setText(printTree + "\n");
}
//if "Backpack" button is selected
if (event.getSource() == bBackpack) {
String sSort = chOrdering.getSelectedItem();
String sNumber = "";
String sRecent = "";
if (sSort == "Number") { //sorted by Pokedex number
while (pq.size() > 0) {
Pokemon curr = pq.poll();
sNumber = sNumber + curr.toString() + "\n\n";
btextArea.setText(sNumber);
}
}
else if (sSort == "Recent") { //sorted by most recently caught
while (!stack.isEmpty()) {
Pokemon curr = stack.pop();
sRecent = sRecent + curr.toString() + "\n\n";
btextArea.setText(sRecent);
}
}
}
} //closes actionPerformed method
} //closes GUI Listener
} //closes Panel Class