-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonPanel.java
More file actions
278 lines (240 loc) · 7.79 KB
/
PokemonPanel.java
File metadata and controls
278 lines (240 loc) · 7.79 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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
/**
* Panel for PokemonGuiJatinp.
* @author Jatin Pandya
* @since 12/6/2018
*/
public class PokemonPanel extends JPanel {
/**
* Choice picker for sorting.
*/
private Choice chPoke = new Choice();
/**
* pokemon.
*/
private Pokemon p = null;
/**
* Title for program.
*/
private JLabel lTitle = new JLabel("Pokemon");
/**
* Button to find pokemon.
*/
private JButton bHunt = new JButton(" Hunt Pokemon ");
/**
* Button to catch pokemon.
*/
private JButton bCatch = new JButton(" Capture Pokemon ");
/**
* Button to show pokemon you've caught.
*/
private JButton bBackpack = new JButton(" Backpack ");
/**
* Button show pokemon you've found.
*/
private JButton bPokedex = new JButton(" Pokedex ");
/**
* Panel to house title.
*/
private JPanel topSubPanel = new JPanel();
/**
* Main panel.
*/
private JPanel centerSubPanel = new JPanel();
/**
* Panel to house buttons and error message.
*/
private JPanel bottomSubPanel = new JPanel();
/**
* Waits for buttons event.
*/
private GUIListener listener = new GUIListener();
/**
* Tree for caught pokemon.
*/
private PokeTree pt = new PokeTree();
/**
* Queue for Pokemon sorted in number order.
*/
private PriorityQueue<Pokemon> pq = new PriorityQueue<Pokemon>();
/**
* Stack for Pokemon sorted in Recent order.
*/
private Stack<Pokemon> stack = new Stack<>();
/**
* Capacity to autogenerate string because of jPanel.
*/
private final int capacity = 10;
/**
* Storing autogenerated text.
*/
private String sOut = new String("");
/**
* Outputting results from make pokemon.
*/
private JTextArea textArea = new JTextArea("");
/**
* Scroll pane for make pokemon.
*/
private JScrollPane scroll = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
/**
* Text area for listing pokemon from pokedex or backpack.
*/
private JTextArea textArea2 = new JTextArea("");
/**
* Allowing scrolling to view pokemons.
*/
private JScrollPane scroll2 = new JScrollPane(textArea2,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
/**
* Constructor holds everything.
*/
public PokemonPanel() {
setLayout(new BorderLayout());
setPreferredSize(new Dimension(400, 500));
topSubPanel.setBackground(Color.red);
centerSubPanel.setBackground(Color.blue);
bottomSubPanel.setBackground(Color.red);
topSubPanel.add(lTitle);
add("North", topSubPanel);
for (int x = 0; x < capacity; x++) {
sOut += (x + 1) + "-------------------------------------"
+ "--------------------------------\n";
}
// Setting up text area and placing them
textArea.setText(sOut);
textArea.setEditable(false);
centerSubPanel.add(scroll);
centerSubPanel.add(bHunt);
bHunt.addActionListener(listener);
centerSubPanel.add(bCatch);
bCatch.addActionListener(listener);
textArea2.setText(sOut);
textArea2.setEditable(false);
centerSubPanel.add(scroll2);
add("Center", centerSubPanel);
// Setting and placing buttons
bottomSubPanel.add(bPokedex);
bPokedex.addActionListener(listener);
bottomSubPanel.add(bBackpack);
bBackpack.addActionListener(listener);
//Positioning for drop down menu
bottomSubPanel.add(chPoke);
add("South", bottomSubPanel);
//add choices to the choice dropdown list
chPoke.add("Recent");
chPoke.add("Number");
}
/**
* Private ActionListener class.
*/
private class GUIListener implements ActionListener {
/**
* ActionPerformed method.
* @param event what button is clicked.
*/
public void actionPerformed(ActionEvent event) {
// Hunting Pokemon
if (event.getSource() == bHunt) {
int max = 18;
Random randGen = new Random();
int a = randGen.nextInt(max) + 1;
String inString = Integer.toString(a);
switch(inString) {
case "1": p = new Bulbasaur();
break;
case "2": p = new Ivysaur();
break;
case "3": p = new Venusaur();
break;
case "4": p = new Charmander();
break;
case "5": p = new Charmeleon();
break;
case "6": p = new Charizard();
break;
case "7": p = new Squirtle();
break;
case "8": p = new Wartortle();
break;
case "9": p = new Blastoise();
break;
case "10": p = new Vulpix();
break;
case "11": p = new Ninetales();
break;
case "12": p = new Growlithe();
break;
case "13": p = new Arcanine();
break;
case "14": p = new Ponyta();
break;
case "15": p = new Rapidash();
break;
case "16": p = new Magmar();
break;
case "17": p = new Psyduck();
break;
case "18": p = new Golduck();
break;
default: System.out.println("Not a valid choice");
break;
}
pq.add(p);
stack.push(p);
textArea.setText(p.toString());
}
if (event.getSource() == bCatch) {
if (p == null) {
textArea.setText("No pokemon has been found");
}
else {
boolean c;
Random randGen = new Random();
c = randGen.nextBoolean();
if (c) {
textArea.setText(p.toString() + "\n\n" + p.getName()
+ " was caught");
pt.add(p);
} else {
textArea.setText(p.toString() + "\n\n" + p.getName()
+ " ran away");
}
p = null;
}
}
if (event.getSource() == bBackpack) {
textArea2.setText(pt.printPokeTree());
}
if (event.getSource() == bPokedex) {
String s = "";
String sort = chPoke.getSelectedItem();
PriorityQueue<Pokemon> pqTemp = new PriorityQueue<Pokemon>();
Stack<Pokemon> stackCopy = new Stack<>();
stackCopy.addAll(stack);
if (sort.equals("Number")) {
while (pq.size() > 0) { // Printing out Priority Queue
Pokemon curr = pq.poll();
s = s + curr.toString() + "\n";
pqTemp.add(curr);
}
pq.addAll(pqTemp);
textArea2.setText(s);
} else {
int pSize = stack.size();
for (int k = 0; k < pSize; k++) { // Printing out Stack
Pokemon curr = stackCopy.pop();
s = s + curr.toString() + "\n";
}
textArea2.setText(s);
}
}
}
}
}