-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonPanelGlevy4.java
More file actions
244 lines (190 loc) · 7.35 KB
/
PokemonPanelGlevy4.java
File metadata and controls
244 lines (190 loc) · 7.35 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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
/**
* JPanel subclass for Frame-based GUI.
* @author Grayson Levy
* @since 12/11/20
*/
public class PokemonPanelGlevy4 extends JPanel {
/** label. */
private JLabel title = new JLabel("Pokedex");
/** label. */
private JLabel errorM = new JLabel(" ");
/** button. */
private JButton add = new JButton(" Add Pokemon ");
/** button. */
private JButton clear = new JButton(" Clear ");
/** button. */
private JButton list = new JButton(" Make List ");
/** Choice drop down menu for pokemon. */
private Choice chPokemon = new Choice();
/** sub-panel. */
private JPanel topSubPanel = new JPanel();
/** sub-panel. */
private JPanel centerSubPanel = new JPanel();
/** sub-panel. */
private JPanel westSubPanel = new JPanel();
/** sub-panel. */
private JPanel southSubPanel = new JPanel();
/** ActionListener obj.*/
private GUIListener listener = new GUIListener();
/** text are for displaying Pokemon.toString()s. */
private JTextArea textAreaC = new JTextArea(20, 20);
/** text are for displaying Array list of pokemon. */
private JTextArea textAreaS = new JTextArea(20, 20);
/** Make TextArea scrollable. */
//textArea is put inside this.
private JScrollPane scroll = new JScrollPane(textAreaS,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
/** pokemon. */
private Pokemon p;
/** pokemon array list. */
private ArrayList<Pokemon> pArray = new ArrayList<>();
/** counter printing for array. */
private int counter = 0;
/**
* Constructor builds layout.
*/
public PokemonPanelGlevy4() {
this.setLayout(new BorderLayout()); //Border panel layout
this.setPreferredSize(new Dimension(600, 700));
topSubPanel.setBackground(Color.white); //north area color
centerSubPanel.setBackground(Color.gray); //center area color
westSubPanel.setBackground(Color.gray); //bottomSubPanel area color
/** adds to top panel */
topSubPanel.add(title); //adds title
this.add("North", topSubPanel);
/** adds to the center sub-panel */
//make sub=panel with grid layout
GridLayout gl = new GridLayout(2, 1);
JPanel centerSubPanel = new JPanel();
centerSubPanel.setLayout(gl);
textAreaC.setEditable(false); //make it not editable by user
textAreaC.setBackground(Color.LIGHT_GRAY);
centerSubPanel.add(textAreaC); //to display pokemone added to array
//set up the textArea for holding list
textAreaS.setBackground(Color.LIGHT_GRAY);
//make it not editable by user
textAreaS.setEditable(false);
//set up scrollPane appearance and size (textArea already inside it)
scroll.setBorder(null);
centerSubPanel.add(scroll); //add scrollPane to panel, textArea inside.
scroll.getVerticalScrollBar().setPreferredSize(new Dimension(10, 0));
add("Center", centerSubPanel);
/** adds to west subpanel */
//make sub=panel with grid layout
GridLayout g2 = new GridLayout(2, 1);
westSubPanel.setLayout(gl);
westSubPanel.setSize(200, 600);
//add choices to the choice dropdown list
chPokemon.add("Bulbasaur");
chPokemon.add("Venusaur");
chPokemon.add("Ivysaur");
chPokemon.add("Squirtle");
chPokemon.add("Wartortle");
chPokemon.add("Blastoise");
chPokemon.add("Charmander");
chPokemon.add("Charmeleon");
chPokemon.add("Charizard");
westSubPanel.add(chPokemon); //add chooice dropdown
westSubPanel.add(add); //add button
westSubPanel.add(list); //button to make list
add.addActionListener(listener); //add listener to button
add("West", westSubPanel);
GridLayout g3 = new GridLayout(1, 2);
southSubPanel.setLayout(g3);
southSubPanel.add(clear); //clears area
clear.setEnabled(false);
clear.addActionListener(listener); //add listener to button
list.addActionListener(listener); //add listener to button
add("South", southSubPanel);
} //closes JPanel constructor
/**
* Private ActionListener class.
*/
private class GUIListener implements ActionListener {
/**
* ActionPerformed method.
* @param event what button is clicked.
*/
public void actionPerformed(ActionEvent event) {
//clears array and text fields if button is clicked
if (event.getSource() == clear) {
counter = 0;
textAreaS.setText("");
textAreaC.setText("");
pArray.clear();
clear.setEnabled(false);
}
//if add button is clicked
if (event.getSource() == add) {
String species = "";
String name = "";
int hp = 0;
int pNum = 0;
Random randGen = new Random();
int min = 0;
int max = 401;
//get pokemon selection
species = chPokemon.getSelectedItem();
//set hp
hp = randGen.nextInt((max - min)) + min;
//set Pokemon number
switch (species) {
case "Bulbasaur":
pNum = 1;
break;
case "Venusaur":
pNum = 2;
break;
case "Ivysaur":
pNum = 3;
break;
case "Squirtle":
pNum = 4;
break;
case "Wartortle":
pNum = 5;
break;
case "Blastoise":
pNum = 6;
break;
case "Charmander":
pNum = 7;
break;
case "Charmeleon":
pNum = 8;
break;
case "Charizard":
pNum = 9;
break;
default :
} //end switch
//makes new pokemon object
p = new Pokemon(species, name, pNum, hp);
//print pokemon to text area
textAreaC.setText(p.toString());
//add pokemon to array
pArray.add(p);
} //end if for add button
//if make list button is clicked
if (event.getSource() == list) {
clear.setEnabled(true);
int arrayL = 0;
arrayL = pArray.size();
String pList = "";
Pokemon tempP;
while (counter < arrayL) {
tempP = pArray.get(counter);
pList += "Pokemon #" + (counter + 1) + "\n" + tempP.toString() + "\n\n";
counter++;
} //end for loop for printing array
//adds list to text area
textAreaS.setText(pList);
counter = 0;
} //end if for make list button
} //end actionEvent method
} //end GUIListener
} //close Panel Class