/
ClientGUI.java
850 lines (653 loc) · 20.4 KB
/
ClientGUI.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import javax.swing.*;
import javax.imageio.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
/*
* The Client with its GUI
*/
public class ClientGUI extends JFrame implements ActionListener {
int mapSize = 40;
Tile[][] tiles;
ArrayList<Building> bList;
ArrayList<Building> uList;
ArrayList<JRadioButton> bb, ub;
private JLabel chatStatus;
private JTextField chatField;
private JTextArea chatArea;
MainPanel mainPanels, mainLogin, mainMenu, mainCM, mainTM;
SidePanel sidePanels, sideLogin, sideMenu, sideCM, sideTM;
private JTextField usernameField;
private JTextField passwordField;
javax.swing.Timer timer;
JLabel mmLabel;
int cdTime;
CButton cmButton, tmButton, gsButton;
CButton cmBack;
CButton tmBack;
Map mapTM;
private static final long serialVersionUID = 1L;
private JTextField tfServer, tfPort;
private CButton login, logout, whoIsIn;
private boolean connected;
private Client client;
private int defaultPort, defaultUDPPort;
private String defaultHost;
private String unameUDP;
// Constructor connection receiving a socket number
public ClientGUI(String host, int port, int udpPort) {
super("Clash of Clans");
defaultPort = port;
defaultUDPPort = udpPort;
defaultHost = host;
// the server name and the port number
JPanel serverAndPort = new JPanel(new GridLayout(1,5, 1, 3));
tfServer = new JTextField(host);
tfPort = new JTextField("" + port);
tfPort.setHorizontalAlignment(SwingConstants.RIGHT);
// CHAT COMPONENTS
chatStatus = new JLabel("Please login first", SwingConstants.LEFT);
chatField = new JTextField(18);
chatField.setBackground(Color.WHITE);
JPanel chatControls = new JPanel();
chatControls.add(chatStatus);
chatControls.add(chatField);
chatControls.setBounds(0,0,200,50);
chatArea = new JTextArea("Welcome to the Chat room\n", 80, 80);
chatArea.setEditable(false);
JScrollPane jsp = new JScrollPane(chatArea);
jsp.setBounds(0,50,200,550);
JPanel chatPanel = new JPanel(null);
chatPanel.setSize(1000,600);
chatPanel.add(chatControls);
chatPanel.add(jsp);
//LOGIN COMPONENTS
mainLogin = new MainPanel();
mainLogin.add(new JLabel("Main Login"));
usernameField = new JTextField("user", 15);
passwordField = new JTextField("password", 15);
login = new CButton("Login");
login.addActionListener(this);
sideLogin = new SidePanel(new FlowLayout(FlowLayout.LEFT));
sideLogin.add(usernameField);
sideLogin.add(passwordField);
sideLogin.add(login);
//MAIN MENU COMPONENTS
mainMenu = new MainPanel();
mmLabel = new JLabel("Main Menu");
timer = new javax.swing.Timer(1000, this);
mainMenu.add(mmLabel);
sideMenu = new SidePanel(new FlowLayout(FlowLayout.LEFT));
cmButton = new CButton("Customize Map");
tmButton = new CButton("Troop Movement");
gsButton = new CButton("Game Start");
logout = new CButton("Logout");
cmButton.addActionListener(this);
tmButton.addActionListener(this);
gsButton.addActionListener(this);
logout.addActionListener(this);
sideMenu.add(cmButton);
//sideMenu.add(tmButton);
sideMenu.add(gsButton);
sideMenu.add(logout);
//CM COMPONENTS
mainCM = new MainPanel(new GridLayout(mapSize,mapSize));
tiles = new Tile[mapSize][mapSize];
int tileSize = mainCM.getWidth()/mapSize;
for(int i=0; i<mapSize; i++){
tiles[i] = new Tile[mapSize];
for(int j=0; j<mapSize; j++){
tiles[i][j] = new Tile(i, j);
tiles[i][j].setPreferredSize(new Dimension(tileSize, tileSize));
tiles[i][j].setSize(tileSize, tileSize);
tiles[i][j].addActionListener(this);
if( (i+j)%2 == 0 )
tiles[i][j].setBackground(new Color(102, 255, 51));
else
tiles[i][j].setBackground(new Color(51, 204, 51));
mainCM.add(tiles[i][j]);
}
}
sideCM = new SidePanel(new FlowLayout(FlowLayout.LEFT));
cmBack = new CButton("Main Menu");
cmBack.setSize(150,30);
cmBack.setPreferredSize(new Dimension(150,30));
cmBack.addActionListener(this);
sideCM.add(cmBack);
//TM COMPONENTS
mainTM = new MainPanel(null);
mapTM = new Map(600);
mapTM.setPreferredSize(new Dimension(600,600));
mapTM.setSize(600,600);
mapTM.setBounds(0,0,600,600);
mainTM.add(mapTM);
sideTM = new SidePanel(new FlowLayout(FlowLayout.LEFT));
tmBack = new CButton("Main Menu");
tmBack.setSize(150,30);
tmBack.setPreferredSize(new Dimension(150,30));
tmBack.addActionListener(this);
sideTM.add(tmBack);
JRadioButton button;
ButtonGroup group;
ub = new ArrayList<JRadioButton>();
group = new ButtonGroup();
button = new JRadioButton("Barbarian");
button.addActionListener(this);
ub.add(button);
sideTM.add(button);
group.add(button);
button = new JRadioButton("Archer");
button.addActionListener(this);
ub.add(button);
sideTM.add(button);
group.add(button);
createBuildings();
bb = new ArrayList<JRadioButton>();
group = new ButtonGroup();
JRadioButton removeButton = new JRadioButton("Remove Building");
bb.add(removeButton);
sideCM.add(removeButton);
group.add(removeButton);
for(int i=0; i<bList.size(); i++){
button = new JRadioButton(bList.get(i).getName()+'-'+bList.get(i).getQuantity());
bb.add(button);
sideCM.add(button);
group.add(button);
}
mainPanels = new MainPanel(new CardLayout());
mainPanels.add(mainLogin, "Login");
mainPanels.add(mainMenu, "Menu");
mainPanels.add(mainCM, "CM");
mainPanels.add(mainTM, "TM");
sidePanels = new SidePanel(new CardLayout());
sidePanels.add(sideLogin, "Login");
sidePanels.add(sideMenu, "Menu");
sidePanels.add(sideCM, "CM");
sidePanels.add(sideTM, "TM");
JPanel mainPanel = new JPanel(null);
mainPanel.setSize(1000,600);
mainPanel.add(sidePanels);
mainPanel.add(mainPanels);
mainPanel.add(chatPanel);
add(mainPanel, BorderLayout.CENTER);
try {
setIconImage(ImageIO.read(new File("images/logo.png")));
}
catch (IOException exc) {
exc.printStackTrace();
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1000, 600);
setVisible(true);
setResizable(false);
chatField.requestFocus();
}
// called by the Client to append text in the TextArea
void append(String str) {
chatArea.append(str);
chatArea.setCaretPosition(chatArea.getText().length() - 1);
}
public void createBuildings(){
bList = new ArrayList<Building>();
//resource
bList.add(new Building("Gold Mine", 3, 3, 960, 7));
bList.add(new Building("Elixir Collector", 3, 3, 960, 7));
//bList.add(new Building("Dark Elixir Drill", 3, 3, 1160, 3));
bList.add(new Building("Gold Storage", 3, 3, 2100, 4));
bList.add(new Building("Elixir Storage", 3, 3, 2100, 4));
//bList.add(new Building("Dark Elixir Storage", 3, 3, 3200, 1));
//bList.add(new Building("Builder Hut", 2, 2, 250, 5));
//army
bList.add(new Building("Army Camp", 5, 5, 500, 4));
bList.add(new Building("Barracks", 3, 3, 860, 4));
//bList.add(new Building("Dark Barracks", 3, 3, 900, 2));
//bList.add(new Building("Laboratory", 4, 4, 950, 1));
//bList.add(new Building("Spell Factory", 3, 3, 615, 1));
//bList.add(new Building("Barbarian King Altar", 3, 3, 250, 1));
//bList.add(new Building("Dark Spell Factory", 3, 3, 750, 1));
//bList.add(new Building("Archer Queen Altar", 3, 3, 250, 1));
//other
bList.add(new Building("Town Hall", 4, 4, 5500, 1));
bList.add(new Building("Clan Castle", 3, 3, 3400, 1));
//defense
bList.add(new Building("Archer Tower", 3, 3, 1050, 7));
bList.add(new Building("Cannon", 3, 3, 1260, 6));
bList.add(new Building("Wall", 1, 1, 7000, 275));
//bList.add(new Building("Air Sweeper", 2, 2, 1000, 2));
//bList.add(new Building("Cannon", 3, 3, 1260, 6));
//bList.add(new Building("Cannon", 3, 3, 1260, 6));
}
/*
* Button or JTextField clicked
*/
public void switchCards(String s){
System.out.println("Switching to "+s);
CardLayout cl = (CardLayout)(mainPanels.getLayout());
cl.show(mainPanels, s);
mainPanels.revalidate();
CardLayout cl2 = (CardLayout)(sidePanels.getLayout());
cl2.show(sidePanels, s);
sidePanels.revalidate();
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
for(JRadioButton u : ub){
if(o == u){
mapTM.setUnitType(u.getText());
System.out.println("Set unit type - "+u.getText());
return;
}
}
if(o == timer){
/*
mmLabel.setText("Main Menu ("+(cdTime--)+")");
if(cdTime == 0){
timer.stop();
gsButton.setText("Game Start");
mmLabel.setText("Main Menu");
ArrayList<Building> bArr = new ArrayList<Building>();
String temp = "Elixir Collector-24,8-960|Elixir Collector-31,8-960|Gold Mine-17,10-960|Elixir Collector-25,21-960|Elixir Collector-11,22-960";
String[] bs = temp.split("\\|");
for(String b : bs){
String[] bParts = b.split("-");
String[] cParts = bParts[1].split(",");
int x = Integer.parseInt(cParts[0].trim());
int y = Integer.parseInt(cParts[1].trim());
Building tb = new Building(bParts[0], new Coordinate(x,y));
tb.setHp(Integer.parseInt(bParts[2].trim()));
bArr.add(tb);
}
mapTM.setBuildings(bArr);
switchCards("TM");
}
*/
return;
}
if(o == gsButton){
if(timer.isRunning()){
//timer.stop();
gsButton.setText("Game Start");
mmLabel.setText("Main Menu");
//sends the leave command
client.sendUDP("leave~"+unameUDP);
return;
}
//sends the joinlobby command
client.sendUDP("joinlobby~"+unameUDP);
//gsButton.setText("Game Stop");
String serverResp = client.receiveUDP();
if(serverResp.trim().equals("false")){
//place false handler here
}else{
String[] enemies = serverResp.trim().split(",");
ArrayList<Building> bArr = new ArrayList<Building>();
String mapConfig = getBaseConfig(enemies[0]);
String[] bs = mapConfig.split("\\|");
for(String b : bs){
String[] bParts = b.split("-");
String[] cParts = bParts[1].split(",");
int x = Integer.parseInt(cParts[0].trim());
int y = Integer.parseInt(cParts[1].trim());
Building tb = new Building(bParts[0], new Coordinate(x,y));
tb.setHp(Integer.parseInt(bParts[2].trim()));
bArr.add(tb);
}
mapTM.setBuildings(bArr);
switchCards("TM");
}
//System.out.println(serverResp);
//cdTime = 10;
//timer.start();
return;
}
if(o == logout) {
client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, ""));
chatArea.setText("");
switchCards("Login");
return;
}
if(o == cmButton){
String baseConfig = getBaseConfig();
System.out.println("base config: "+baseConfig);
for(int i=0; i<mapSize; i++){
for(int j=0; j<mapSize; j++){
tiles[i][j].setValue("");
}
}
if(!baseConfig.equals("")){
String[] bs = baseConfig.split("\\|");
for(String b : bs){
String[] bParts = b.split("-");
String[] cParts = bParts[1].split(",");
int x = Integer.parseInt(cParts[0].trim());
int y = Integer.parseInt(cParts[1].trim());
int index = 0;
for(int i=0; i<bb.size(); i++){
if(bb.get(i).getText().split("-")[0].trim().equals(bParts[0])){
index = i;
break;
}
}
insertBuilding(y, x, index);
}
}
switchCards("CM");
return;
}
if(o == tmButton){
ArrayList<Building> bArr = new ArrayList<Building>();
for(int i=0; i<40; i++){
for(int j=0; j<40; j++){
if(tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")){
continue;
}
//weird part here
bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i)));
}
}
mapTM.setBuildings(bArr);
switchCards("TM");
return;
}
// if it the who is in button
if(o == whoIsIn) {
client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, ""));
return;
}
if(o == cmBack){
ArrayList<Building> bArr = new ArrayList<Building>();
for(int i=0; i<40; i++){
for(int j=0; j<40; j++){
if(tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")){
continue;
}
//weird part here
bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i)));
}
}
String temp = "mapdata~"+unameUDP+"~";
int tileCount = 40;
int dim = 600;
int tileDim = (int)(dim/tileCount);
int counter = 0;
for(Building b : bArr){
int x, y, hp;
x = b.getPos().getX()/tileDim;
y = b.getPos().getY()/tileDim;
hp = b.getHp();
temp += b.getName()+"-"+x+","+y+"-"+hp+"|";
counter += 1;
}
if(counter > 0){
temp = temp.substring(0, temp.length()-1); //removes the last '|'
}else{
temp += "none";
}
client.sendUDP(temp); //allows saving of the current state of the map into the user's account
switchCards("Menu");
return;
}
if(o == tmBack){
switchCards("Menu");
return;
}
for(int i=0; i<40; i++){
for(int j=0; j<40; j++){
if(o == tiles[i][j]){
for(int k=0; k<bb.size(); k++){
if(bb.get(k).isSelected()){
if(bb.get(k).getText().equals("Remove Building")){
removeBuilding(i, j);
return;
}
insertBuilding(i, j, k);
//JOptionPane.showMessageDialog(null, bb.get(k).getText());
return;
}
}
//JOptionPane.showMessageDialog(null, "i-"+i+" j-"+j);
return;
}
}
}
// ok it is coming from the JTextField
if(connected) {
// just have to send the message
client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, chatField.getText()));
chatField.setText("");
return;
}
if(o == login) {
// ok it is a connection request
String username = usernameField.getText().trim();
String password = passwordField.getText().trim();
// empty username ignore it
if(username.length() == 0)
return;
// empty serverAddress ignore it
String server = tfServer.getText().trim();
if(server.length() == 0)
return;
// empty or invalid port numer, ignore it
String portNumber = tfPort.getText().trim();
if(portNumber.length() == 0)
return;
int port = 0;
try {
port = Integer.parseInt(portNumber);
}
catch(Exception en) {
return; // nothing I can do if port number is not valid
}
// try creating a new Client with GUI
client = new Client(server, port, username, password, this);
// test if we can start the Client
if(!client.start())
return;
unameUDP = username;
switchCards("Menu");
//fetching of the base_config string from the database
chatField.setText("");
chatArea.setText("");
}
}
public String getBaseConfig(){
client.sendUDP("getBase~"+unameUDP);
return client.receiveUDP();
}
public String getBaseConfig(String username){
client.sendUDP("getBase~"+username);
return client.receiveUDP();
}
public boolean getConnectStatus(){
return connected;
}
public void removeBuilding(int i, int j){
if(tiles[i][j].getValue().equals("")){
JOptionPane.showMessageDialog(null, "There is no building here");
return;
}
if(tiles[i][j].value.contains("-")){
String tText = tiles[i][j].value;
i = Integer.parseInt(tText.split("-")[0]);
j = Integer.parseInt(tText.split("-")[1]);
}
int index;
for(index=0; index<bb.size(); index++){
if(bb.get(index).getText().split("-")[0].equals(tiles[i][j].value)){
break;
}
}
String temp = bb.get(index).getText();
String[] parts = temp.split("-");
int newQ = Integer.parseInt(parts[1])+1;
Building tB = bList.get(0);
for(int b=0; b<bList.size(); b++){
if( bList.get(b).getName().equals(tiles[i][j].value) ){
tB = bList.get(b);
break;
}
}
for(int c=0; c<tB.getQWidth(); c++){
for(int r=0; r<tB.getQHeight(); r++){
if( (i+c+j+r)%2 == 0 )
tiles[i+c][j+r].setBackground(new Color(102, 255, 51));
else
tiles[i+c][j+r].setBackground(new Color(51, 204, 51));
tiles[i+c][j+r].setIcon(null);
tiles[i+c][j+r].value = "";
bb.get(index).setText(parts[0] + "-" + newQ );
}
}
}
public static BufferedImage resize(BufferedImage image, int width, int height) {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
Graphics2D g2d = (Graphics2D) bi.createGraphics();
g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
g2d.drawImage(image, 0, 0, width, height, null);
g2d.dispose();
return bi;
}
public void insertBuilding(int i, int j, int index){
if(!tiles[i][j].getValue().equals("")){
JOptionPane.showMessageDialog(null, "There is already a building here");
return;
}
String temp = bb.get(index).getText();
String[] parts = temp.split("-");
int newQ = Integer.parseInt(parts[1])-1;
if(newQ < 0)
return;
Building tB = bList.get(0);
for(int b=0; b<bList.size(); b++){
if( bList.get(b).getName().equals(parts[0]) ){
tB = bList.get(b);
//System.out.println("here");
break;
}
}
Image image = null;
BufferedImage buffered;
try{
image = ImageIO.read(new File("images/"+tB.getName().replace(" ", "_")+".png"));
System.out.println("Loaded image");
}
catch(IOException e){
System.out.println("Failed to load image");
}
buffered = (BufferedImage) image;
for(int c=0; c<tB.getQWidth(); c++){
for(int r=0; r<tB.getQHeight(); r++){
if(i+c == 40 || j+r == 40){
JOptionPane.showMessageDialog(null, "Placing a building here would be out of bounds");
return;
}
if(!tiles[i+c][j+r].getValue().equals("")){
JOptionPane.showMessageDialog(null, "Placing a building here will result to a overlap");
return;
}
}
}
double w = buffered.getWidth()/tB.getQWidth();
double h = buffered.getHeight()/tB.getQHeight();
for(int c=0; c<tB.getQWidth(); c++){
for(int r=0; r<tB.getQHeight(); r++){
tiles[i+c][j+r].setBackground(new Color(51, 204, 51));
tiles[i+c][j+r].setIcon(new ImageIcon( resize( buffered.getSubimage((int)w*r,(int)h*c,(int)w,(int)h), tiles[i+c][j+r].getWidth(), tiles[i+c][j+r].getHeight() ) ) );
String tValue = (c==0 && r==0)? tB.getName(): i+"-"+j;
//System.out.println(tValue);
tiles[i+c][j+r].setValue(tValue);
}
}
//tiles[i][j].setBackground(Color.BLUE);
bb.get(index).setText(parts[0] + "-" + newQ );
}
public void validAuthenticate(){
chatStatus.setText("Enter your message below");
connected = true;
// disable login button
login.setEnabled(false);
// enable the 2 buttons
logout.setEnabled(true);
//whoIsIn.setEnabled(true);
// disable the Server and Port JTextField
tfServer.setEditable(false);
tfPort.setEditable(false);
// Action listener for when the user enter a message
chatField.addActionListener(this);
switchCards("Menu");
}
void connectionFailed() {
login.setEnabled(true);
logout.setEnabled(false);
//whoIsIn.setEnabled(false);
chatStatus.setText("Please login first");
chatField.setText("");
// reset port number and host name as a construction time
tfPort.setText("" + defaultPort);
tfServer.setText(defaultHost);
// let the user change them
tfServer.setEditable(false);
tfPort.setEditable(false);
// don't react to a <CR> after the username
chatField.removeActionListener(this);
connected = false;
}
// to start the whole thing the server
public static void main(String[] args) {
String serverAddress = "localhost";
int portNumber = 1500;
int udpPortNumber = 1600; //DEFAULT (for now). NEED A WAY TO ADD A TEXT FIELD JUST LIKE IN THE SERVERGUI
// depending of the number of arguments provided we fall through
switch(args.length) {
// > javac Client username portNumber serverAddr
case 2:
try {
portNumber = Integer.parseInt(args[1]);
}
catch(Exception e) {
System.out.println("Invalid port number.");
System.out.println("Usage is: > java ClientGUI [serverAddress] [portNumber]");
return;
}
// > javac Client username portNumber
case 1:
serverAddress = args[0];
// > java Client
case 0:
break;
// invalid number of arguments
default:
System.out.println("Usage is: > java ClientGUI [serverAddress] [portNumber]");
return;
}
//new ClientGUI("localhost", 1500);
new ClientGUI(serverAddress, portNumber, udpPortNumber);
}
}
class Tile extends JButton{
String value;
int i, j;
public Tile(int i, int j){
this.value = "";
this.i = i;
this.j = j;
setBorderPainted(false);
setFocusPainted(false);
}
public void setValue(String str){
this.value = str;
}
public int geti(){
return i;
}
public int getj(){
return j;
}
public String getValue(){
return value;
}
}