-
Notifications
You must be signed in to change notification settings - Fork 9
/
Field.java
629 lines (489 loc) · 15.8 KB
/
Field.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Jeka on 14.10.2014.
*/
public class Field extends FieldFrame{
private List<Ship> MyShips;
private List<JLabel> Labels;
private List<JButton> MyShots;
public Field() {
MyShots=new ArrayList<JButton>();
MyShips=new ArrayList<Ship>();
//Labels for right panel
Labels=new ArrayList<JLabel>();
Dimension dimension=new Dimension(SIZE_OF_BUTTONS_AND_LABELS,SIZE_OF_BUTTONS_AND_LABELS);
for (int i=0; i<100; i++){
Labels.add(new JLabel("",SwingConstants.CENTER));
}
for (int i=0; i<100; i++){
Labels.get(i).setPreferredSize(dimension);
Labels.get(i).setOpaque(true);
//Labels.get(i).setBackground(Color.blue);
}
JLabel aLabelRightPanel=new JLabel("a",SwingConstants.CENTER);
JLabel bLabelRightPanel=new JLabel("b",SwingConstants.CENTER);
JLabel cLabelRightPanel=new JLabel("c",SwingConstants.CENTER);
JLabel dLabelRightPanel=new JLabel("d",SwingConstants.CENTER);
JLabel eLabelRightPanel=new JLabel("e",SwingConstants.CENTER);
JLabel fLabelRightPanel=new JLabel("f",SwingConstants.CENTER);
JLabel gLabelRightPanel=new JLabel("g",SwingConstants.CENTER);
JLabel hLabelRightPanel=new JLabel("h",SwingConstants.CENTER);
JLabel iLabelRightPanel=new JLabel("i",SwingConstants.CENTER);
JLabel jLabelRightPanel=new JLabel("j",SwingConstants.CENTER);
JLabel emptyLabelRightPanel=new JLabel();
JLabel oneLabelRightPanel=new JLabel("1",SwingConstants.CENTER);
JLabel twoLabelRightPanel=new JLabel("2",SwingConstants.CENTER);
JLabel threeLabelRightPanel=new JLabel("3",SwingConstants.CENTER);
JLabel fourLabelRightPanel=new JLabel("4",SwingConstants.CENTER);
JLabel fiveLabelRightPanel=new JLabel("5",SwingConstants.CENTER);
JLabel sixLabelRightPanel=new JLabel("6",SwingConstants.CENTER);
JLabel sevenLabelRightPanel=new JLabel("7",SwingConstants.CENTER);
JLabel eightLabelRightPanel=new JLabel("8",SwingConstants.CENTER);
JLabel nineLabelRightPanel=new JLabel("9",SwingConstants.CENTER);
JLabel tenLabelRightPanel=new JLabel("10",SwingConstants.CENTER);
//Panels
JPanel rightPanel=new JPanel();
rightPanel.setPreferredSize(new Dimension(275,275));
rightPanel.setLayout(new GridLayout(11,11));
//Add labels at the rightPanel
//1th row
rightPanel.add(emptyLabelRightPanel);
rightPanel.add(oneLabelRightPanel);
rightPanel.add(twoLabelRightPanel);
rightPanel.add(threeLabelRightPanel);
rightPanel.add(fourLabelRightPanel);
rightPanel.add(fiveLabelRightPanel);
rightPanel.add(sixLabelRightPanel);
rightPanel.add(sevenLabelRightPanel);
rightPanel.add(eightLabelRightPanel);
rightPanel.add(nineLabelRightPanel);
rightPanel.add(tenLabelRightPanel);
//2th row
rightPanel.add(aLabelRightPanel);
for (int i=0; i<10; i++){
rightPanel.add(Labels.get(i));
}
//3th row
rightPanel.add(bLabelRightPanel);
for (int i=10; i<20; i++){
rightPanel.add(Labels.get(i));
}
//4th row
rightPanel.add(cLabelRightPanel);
for (int i=20; i<30; i++){
rightPanel.add(Labels.get(i));
}
//5th row
rightPanel.add(dLabelRightPanel);
for (int i=30; i<40; i++){
rightPanel.add(Labels.get(i));
}
//6th row
rightPanel.add(eLabelRightPanel);
for (int i=40; i<50; i++){
rightPanel.add(Labels.get(i));
}
//7th row
rightPanel.add(fLabelRightPanel);
for (int i=50; i<60; i++){
rightPanel.add(Labels.get(i));
}
//8th row
rightPanel.add(gLabelRightPanel);
for (int i=60; i<70; i++){
rightPanel.add(Labels.get(i));
}
//9th row
rightPanel.add(hLabelRightPanel);
for (int i=70; i<80; i++){
rightPanel.add(Labels.get(i));
}
//10th row
rightPanel.add(iLabelRightPanel);
for (int i=80; i<90; i++){
rightPanel.add(Labels.get(i));
}
//11th row
rightPanel.add(jLabelRightPanel);
for (int i=90; i<100; i++){
rightPanel.add(Labels.get(i));
}
//Add panels at the frame
this.setTitle("Sea Battle");
this.getContentPane().add(rightPanel);
this.setSize(550,225);
this.setResizable(false);
pack();
}
public void addA1Listener(ActionListener a) {
a1.addActionListener(a);
}
public void addA2Listener(ActionListener a) {
a2.addActionListener(a);
}
public void addA3Listener(ActionListener a) {
a3.addActionListener(a);
}
public void addA4Listener(ActionListener a) {
a4.addActionListener(a);
}
public void addA5Listener(ActionListener a) {
a5.addActionListener(a);
}
public void addA6Listener(ActionListener a) {
a6.addActionListener(a);
}
public void addA7Listener(ActionListener a) {
a7.addActionListener(a);
}
public void addA8Listener(ActionListener a) {
a8.addActionListener(a);
}
public void addA9Listener(ActionListener a) {
a9.addActionListener(a);
}
public void addA10Listener(ActionListener a) {
a10.addActionListener(a);
}
public void addB1Listener(ActionListener a) {
b1.addActionListener(a);
}
public void addB2Listener(ActionListener a) {
b2.addActionListener(a);
}
public void addB3Listener(ActionListener a) {
b3.addActionListener(a);
}
public void addB4Listener(ActionListener a) {
b4.addActionListener(a);
}
public void addB5Listener(ActionListener a) {
b5.addActionListener(a);
}
public void addB6Listener(ActionListener a) {
b6.addActionListener(a);
}
public void addB7Listener(ActionListener a) {
b7.addActionListener(a);
}
public void addB8Listener(ActionListener a) {
b8.addActionListener(a);
}
public void addB9Listener(ActionListener a) {
b9.addActionListener(a);
}
public void addB10Listener(ActionListener a) {
b10.addActionListener(a);
}
public void addC1Listener(ActionListener a) {
c1.addActionListener(a);
}
public void addC2Listener(ActionListener a) {
c2.addActionListener(a);
}
public void addC3Listener(ActionListener a) {
c3.addActionListener(a);
}
public void addC4Listener(ActionListener a) {
c4.addActionListener(a);
}
public void addC5Listener(ActionListener a) {
c5.addActionListener(a);
}
public void addC6Listener(ActionListener a) {
c6.addActionListener(a);
}
public void addC7Listener(ActionListener a) {
c7.addActionListener(a);
}
public void addC8Listener(ActionListener a) {
c8.addActionListener(a);
}
public void addC9Listener(ActionListener a) {
c9.addActionListener(a);
}
public void addC10Listener(ActionListener a) {
c10.addActionListener(a);
}
public void addD1Listener(ActionListener a) {
d1.addActionListener(a);
}
public void addD2Listener(ActionListener a) {
d2.addActionListener(a);
}
public void addD3Listener(ActionListener a) {
d3.addActionListener(a);
}
public void addD4Listener(ActionListener a) {
d4.addActionListener(a);
}
public void addD5Listener(ActionListener a) {
d5.addActionListener(a);
}
public void addD6Listener(ActionListener a) {
d6.addActionListener(a);
}
public void addD7Listener(ActionListener a) {
d7.addActionListener(a);
}
public void addD8Listener(ActionListener a) {
d8.addActionListener(a);
}
public void addD9Listener(ActionListener a) {
d9.addActionListener(a);
}
public void addD10Listener(ActionListener a) {
d10.addActionListener(a);
}
public void addE1Listener(ActionListener a) {
e1.addActionListener(a);
}
public void addE2Listener(ActionListener a) {
e2.addActionListener(a);
}
public void addE3Listener(ActionListener a) {
e3.addActionListener(a);
}
public void addE4Listener(ActionListener a) {
e4.addActionListener(a);
}
public void addE5Listener(ActionListener a) {
e5.addActionListener(a);
}
public void addE6Listener(ActionListener a) {
e6.addActionListener(a);
}
public void addE7Listener(ActionListener a) {
e7.addActionListener(a);
}
public void addE8Listener(ActionListener a) {
e8.addActionListener(a);
}
public void addE9Listener(ActionListener a) {
e9.addActionListener(a);
}
public void addE10Listener(ActionListener a) {
e10.addActionListener(a);
}
public void addF1Listener(ActionListener a) {
f1.addActionListener(a);
}
public void addF2Listener(ActionListener a) {
f2.addActionListener(a);
}
public void addF3Listener(ActionListener a) {
f3.addActionListener(a);
}
public void addF4Listener(ActionListener a) {
f4.addActionListener(a);
}
public void addF5Listener(ActionListener a) {
f5.addActionListener(a);
}
public void addF6Listener(ActionListener a) {
f6.addActionListener(a);
}
public void addF7Listener(ActionListener a) {
f7.addActionListener(a);
}
public void addF8Listener(ActionListener a) {
f8.addActionListener(a);
}
public void addF9Listener(ActionListener a) {
f9.addActionListener(a);
}
public void addF10Listener(ActionListener a) {
f10.addActionListener(a);
}
public void addG1Listener(ActionListener a) {
g1.addActionListener(a);
}
public void addG2Listener(ActionListener a) {
g2.addActionListener(a);
}
public void addG3Listener(ActionListener a) {
g3.addActionListener(a);
}
public void addG4Listener(ActionListener a) {
g4.addActionListener(a);
}
public void addG5Listener(ActionListener a) {
g5.addActionListener(a);
}
public void addG6Listener(ActionListener a) {
g6.addActionListener(a);
}
public void addG7Listener(ActionListener a) {
g7.addActionListener(a);
}
public void addG8Listener(ActionListener a) {
g8.addActionListener(a);
}
public void addG9Listener(ActionListener a) {
g9.addActionListener(a);
}
public void addG10Listener(ActionListener a) {
g10.addActionListener(a);
}
public void addH1Listener(ActionListener a) {
h1.addActionListener(a);
}
public void addH2Listener(ActionListener a) {
h2.addActionListener(a);
}
public void addH3Listener(ActionListener a) {
h3.addActionListener(a);
}
public void addH4Listener(ActionListener a) {
h4.addActionListener(a);
}
public void addH5Listener(ActionListener a) {
h5.addActionListener(a);
}
public void addH6Listener(ActionListener a) {
h6.addActionListener(a);
}
public void addH7Listener(ActionListener a) {
h7.addActionListener(a);
}
public void addH8Listener(ActionListener a) {
h8.addActionListener(a);
}
public void addH9Listener(ActionListener a) {
h9.addActionListener(a);
}
public void addH10Listener(ActionListener a) {
h10.addActionListener(a);
}
public void addI1Listener(ActionListener a) {
i1.addActionListener(a);
}
public void addI2Listener(ActionListener a) {
i2.addActionListener(a);
}
public void addI3Listener(ActionListener a) {
i3.addActionListener(a);
}
public void addI4Listener(ActionListener a) {
i4.addActionListener(a);
}
public void addI5Listener(ActionListener a) {
i5.addActionListener(a);
}
public void addI6Listener(ActionListener a) {
i6.addActionListener(a);
}
public void addI7Listener(ActionListener a) {
i7.addActionListener(a);
}
public void addI8Listener(ActionListener a) {
i8.addActionListener(a);
}
public void addI9Listener(ActionListener a) {
i9.addActionListener(a);
}
public void addI10Listener(ActionListener a) {
i10.addActionListener(a);
}
public void addJ1Listener(ActionListener a) {
j1.addActionListener(a);
}
public void addJ2Listener(ActionListener a) {
j2.addActionListener(a);
}
public void addJ3Listener(ActionListener a) {
j3.addActionListener(a);
}
public void addJ4Listener(ActionListener a) {
j4.addActionListener(a);
}
public void addJ5Listener(ActionListener a) {
j5.addActionListener(a);
}
public void addJ6Listener(ActionListener a) {
j6.addActionListener(a);
}
public void addJ7Listener(ActionListener a) {
j7.addActionListener(a);
}
public void addJ8Listener(ActionListener a) {
j8.addActionListener(a);
}
public void addJ9Listener(ActionListener a) {
j9.addActionListener(a);
}
public void addJ10Listener(ActionListener a) {
j10.addActionListener(a);
}
public void setShips(List<Ship> Ships) {
this.MyShips = Ships;
}
public void arrangeShips(){
for (int n=0;n<MyShips.size();n++){
Ship ship=MyShips.get(n);
List<Integer> cells=ship.getCoordinates();
for (Integer cell : cells) {
Labels.get(cell).setText("x");
Labels.get(cell).setBackground(Color.green);
}
}
}
public boolean opponentsShot(String shotInString){
Integer shot=Integer.valueOf(shotInString);
for(Ship ship:MyShips){
List<Integer> cells=ship.getCoordinates();
for (Integer cell:cells){
if(shot==cell){
//messageDisplay("You hit!");
Labels.get(shot).setBackground(Color.RED);
return true;
}else {
Labels.get(shot).setBackground(Color.BLUE);
}
}
}
return false;
}
public void messageDisplay(String message){
JOptionPane.showMessageDialog(this, message);
}
public void addMyShot(JButton shot){
MyShots.add(shot);
}
public void processingResult(String result){
int lengthOfWord=result.length();
char lastChar=result.charAt((lengthOfWord-1));
char lastCharButOne=result.charAt((lengthOfWord-2));
String trueOrFalse=result.substring(0,(lengthOfWord-2));
String lastCharString=String.valueOf(lastChar);
String lastCharButOneString=String.valueOf(lastCharButOne);
String numberOfCellString=lastCharButOneString+lastCharString;
int numberOfCell=Integer.valueOf(numberOfCellString);
if(trueOrFalse.equals("true")){
buttons.get(numberOfCell).setBackground(Color.red);
}
else {
buttons.get(numberOfCell).setBackground(Color.blue);
}
}
@Override
public void setEnabledAllButtons(boolean condition) {
for (int i=0; i<buttons.size();i++){
buttons.get(i).setEnabled(condition);
}
}
public void closePushedButtons(){
if(!MyShots.isEmpty()){
for(JButton b:MyShots){
b.setEnabled(false);
}
}
}
}