-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserSelection.java
433 lines (390 loc) · 15.5 KB
/
UserSelection.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//To use buttons and labels
import java.awt.*;
//To use events
import java.awt.event.*;
//To use array lists
import java.util.*;
//To read from file
import java.io.*;
public class UserSelection implements ActionListener{
//Labels to display information
ArrayList<Label> lbl = new ArrayList<Label>();
//Button to select current selection
ArrayList<Button> select = new ArrayList<Button>();
//Button to delete current selection
ArrayList<Button> delete = new ArrayList<Button>();
//Buttons to scroll up and down
Button up = new Button("Up");
Button down = new Button("Down");
//Text box for user to jump to a page
TextField txtPage = new TextField();
//Button to go to page in text field
Button go = new Button("GO");
//Button to exit selection
Button back = new Button("Back");
//Button properties
//The starting part in the vertical part
int verticalStart;
//The separation between label y coordinates
int verticalSep;
//The starting point horizontally of the labels
int widthStart;
//The label height
int height;
//Width allocated for labels and buttons
int width;
//The width and height of the screen
int widthLimit, heightLimit;
//The integer to indicate the page
int page = 1;
int maxPage = 1;
//The integer to indicate how many ecords may appear on one page
int max;
//The name of the file where records will be loaded from
String mainFilename, customFilename;
//The level number of the current labels being displayed(only applies to custom levels)
int levelNumber;
//Boolean to check if the level is a custom one or not
boolean lvlcustom;
public static boolean canPlaySound = true;
public UserSelection(int widthLimit2, int heightLimit2, int max2, String mainName, String customName){
//Set the max amount of records that can fit in the screen at one time
max = max2;
//Set the file names
mainFilename = mainName;
customFilename = customName;
//Set the width and height of the screen
widthLimit = widthLimit2;
heightLimit = heightLimit2;
//Set the positions of items
back.setBounds(3, heightLimit-40, 100, 40);
back.addActionListener(this);
up.setBounds(widthLimit/2 - 50, 35 , 100, 30);
up.addActionListener(this);
down.setBounds(widthLimit/2 - 50, heightLimit - 80, 100, 30);
down.addActionListener(this);
txtPage.setBounds(widthLimit - 160, heightLimit - 95, 80, 45);
go.setBounds(widthLimit - 75, heightLimit - 90, 50, 35);
go.addActionListener(this);
//make everything invisible
hideAll();
}
void addLabel(String s){
lbl.add(new Label(s));
select.add(new Button("Select"));
delete.add(new Button("Delete"));
}
void loadLabels(boolean custom, int levelNo){
lbl.clear();
select.clear();
delete.clear();
try{
if(custom == false){
BufferedReader f = new BufferedReader(new FileReader(mainFilename));
int z;
try{
z = Integer.parseInt(f.readLine());
}catch(Exception ex){
z = 0;
}
for(int i = 0; i < z; i++){
//read username
addLabel(f.readLine());
//Read difficulty
f.readLine();
//Read user round
f.readLine();
//Read Allies
for(int i3 = 0; i3 < 3; i3++){
f.readLine();
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
f.readLine();
}
}
}
f.close();
}else{
levelNumber = levelNo;
lvlcustom = true;
BufferedReader f = new BufferedReader(new FileReader(customFilename));
int xyz;
try{
xyz = Integer.parseInt(f.readLine());
}catch(Exception ex){
xyz = 0;
}
for(int i2 = 0; i2 < xyz; i2++){
int z;
try{
z = Integer.parseInt(f.readLine());
}catch(Exception ex){
z = 0;
}
for(int i = 0; i < z; i++){
if(levelNo == i2){
//read username
addLabel(f.readLine());
}else{
f.readLine();
}
//Read difficulty
f.readLine();
//Read user round
f.readLine();
//Read Allies
for(int i3 = 0; i3 < 3; i3++){
f.readLine();
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
f.readLine();
}
}
}
}
f.close();
}
}catch(Exception e){}
//Set the positions of the labels
for(int i = 0, x = 0; i < lbl.size(); i++, x++){
if(x == max){ x = 0; }
//vertical space allocated for labels
int verticalSpace = 520;
//label height
height = (verticalSpace - (max-1)*10)/max;
//label width
width = widthLimit - 500;
lbl.get(i).setBounds(100, 80 + (height+10)*x, width, height);
select.get(i).setBounds(100 + width + 10, 80 + (height+10)*x, 140, height);
delete.get(i).setBounds(100 + width + 20 + 140, 80 + (height+10)*x, 140, height);
}
maxPage = lbl.size()/max;
if(lbl.size()%max != 0){
maxPage++;
}
}
void hideLabels(){
for(int i = 0; i < lbl.size(); i++){
lbl.get(i).setVisible(false);
select.get(i).setVisible(false);
delete.get(i).setVisible(false);
}
}
void hideAll(){
hideLabels();
up.setVisible(false);
down.setVisible(false);
back.setVisible(false);
go.setVisible(false);
txtPage.setVisible(false);
}
//The method to first display everything
void showPage1(boolean custom, int levelNo){
page = 1;
lbl.clear();
select.clear();
delete.clear();
loadLabels(custom, levelNo);
displayPage();
if(lbl.size() == 0){
page = 0;
down.setVisible(false);
}
up.setVisible(false);
if(page != maxPage){
down.setVisible(true);
}
back.setVisible(true);
go.setVisible(true);
txtPage.setVisible(true);
for(int i = 0; i < lbl.size() && i < max;i++){
lbl.get(i).setVisible(true);
select.get(i).setVisible(true);
delete.get(i).setVisible(true);
}
}
public void displayPage(){
int y;
if(page == 0){
y = max;
}else{
y = (page-1)*max;
}
hideLabels();
for(int i = y; i < y+max && i < lbl.size(); i++){
lbl.get(i).setVisible(true);
select.get(i).setVisible(true);
delete.get(i).setVisible(true);
delete.get(i).addActionListener(this);
}
if(page == 1){
up.setVisible(false);
}else{
up.setVisible(true);
}
if(page == maxPage || maxPage == 1){
down.setVisible(false);
}else{
down.setVisible(true);
}
}
public void paint(Graphics g){
g.setFont(new Font("Times New Roman", Font.BOLD, 30));
g.drawString("Choose a Save File ", 250, 55);
g.drawString("Page " + page + "/" + maxPage, 35, 55);
g.drawString("Go to page: ", 1010, 653);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == up){
page--;
displayPage();
}else if(e.getSource() == down){
page++;
displayPage();
}else if(e.getSource() == back){
hideAll();
}else if(e.getSource() == go){
try{
int r = Integer.parseInt(txtPage.getText());
if(r > 0 && r <= maxPage){
page = r;
displayPage();
}else{
canPlaySound = false;
}
}catch(Exception ex){
canPlaySound = false;
}
}
for(int i = 0; i < lbl.size(); i++){
if(e.getSource() == delete.get(i)){
try{
if(lvlcustom == false){
ArrayList<String> y = new ArrayList<String>();
BufferedReader f = new BufferedReader(new FileReader(mainFilename));
//read number of users
int z;
try{
z = Integer.parseInt(f.readLine());
}catch(Exception ex){
z = 0;
}
for(int i3 = 0; i3 < z; i3++){
if(i3 != i){
//read user name
y.add(f.readLine());
//Read difficulty
y.add(f.readLine());
//Read user round
y.add(f.readLine());
//Read Allies
for(int i2 = 0; i2 < 3; i2++){
y.add(f.readLine());
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
y.add(f.readLine());
}
}
}else{
//read user name
f.readLine();
//Read difficulty
f.readLine();
//Read user round
f.readLine();
//Read Allies
for(int i2 = 0; i2 < 3; i2++){
f.readLine();
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
f.readLine();
}
}
}
}
f.close();
z--;
FileWriter f2 = new FileWriter(mainFilename);
f2.write(z +"\r\n");
for(int i2 = 0; i2 < y.size(); i2++){
f2.write(y.get(i2) +"\r\n");
}
f2.close();
}else{
ArrayList<String> y = new ArrayList<String>();
BufferedReader f = new BufferedReader(new FileReader(customFilename));
//The amount of levels
int xyz = Integer.parseInt(f.readLine());
for(int i2 = 0; i2 < xyz; i2++){
int z = Integer.parseInt(f.readLine());
if(levelNumber == i2){
y.add((z-1) + "");
}else{
y.add(z + "");
}
for(int i3 = 0; i3 < z; i3++){
if(i3 == i && levelNumber == i2){
//read user name
f.readLine();
//Read difficulty
f.readLine();
//Read user round
f.readLine();
//Read Allies
for(int i4 = 0; i4 < 3; i4++){
f.readLine();
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
f.readLine();
}
}
}else{
//read user name
y.add(f.readLine());
//Read difficulty
y.add(f.readLine());
//Read user round
y.add(f.readLine());
//Read Allies
for(int i4 = 0; i4 < 3; i4++){
y.add(f.readLine());
}
//Read abilities
for(int i7 = 0; i7 < 3; i7++){
for(int i6 = 0; i6 < 6; i6++){
y.add(f.readLine());
}
}
}
}
}
f.close();
FileWriter f2 = new FileWriter(customFilename);
f2.write(xyz +"\r\n");
for(int i2 = 0; i2 < y.size(); i2++){
f2.write(y.get(i2) +"\r\n");
}
f2.close();
}
}catch(Exception ex){}
lbl.get(i).setVisible(false);
select.get(i).setVisible(false);
delete.get(i).setVisible(false);
lbl.remove(i);
select.remove(i);
delete.remove(i);
displayPage();
}
}
}
}