-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWQClient.java
More file actions
635 lines (597 loc) · 23 KB
/
Copy pathWQClient.java
File metadata and controls
635 lines (597 loc) · 23 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
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
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JList;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.SwingConstants;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.Set;
import java.util.Map;
import java.util.Map.Entry;
import java.nio.channels.SocketChannel;
import java.nio.ByteBuffer;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
/*
** WQClient gestisce tramite GUI le richieste di un utente al Word Quizzle server.
** @Attributi:
** -client: SocketChannel dell'utente che si connette al server.
** -username: nome dell'utente loggato.
** -t: thread in ascolto di richieste di sfida.
*/
public class WQClient {
private static SocketChannel client=null;
private static String[] username=new String[1];
private static Thread t;
public static void main(String[] args) {
ByteBuffer buf=ByteBuffer.allocate(500);
// Frame principale.
JFrame window=new JFrame("Word Quizzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(600,400);
window.setLocationRelativeTo(null);
// Creazione dei JPanel, gestiti tramite CardLayout.
JPanel loginPanel=new JPanel();
loginPanel.setLayout(new BoxLayout(loginPanel,BoxLayout.Y_AXIS));
JPanel registerPanel=new JPanel();
registerPanel.setLayout(new BoxLayout(registerPanel,BoxLayout.Y_AXIS));
JPanel menuPanel=new JPanel();
menuPanel.setLayout(new BoxLayout(menuPanel,BoxLayout.Y_AXIS));
menuPanel.setMaximumSize(new Dimension(500,500));
JLabel lblMenu=new JLabel("Bentornato "+username[0]);
JPanel challengePanel=new JPanel();
challengePanel.setLayout(new BoxLayout(challengePanel,BoxLayout.Y_AXIS));
JPanel friendsPanel=new JPanel();
friendsPanel.setLayout(new BoxLayout(friendsPanel,BoxLayout.Y_AXIS));
JPanel rankPanel=new JPanel();
rankPanel.setLayout(new BoxLayout(rankPanel,BoxLayout.Y_AXIS));
JPanel contentPane=new JPanel(new CardLayout());
contentPane.setBorder(new EmptyBorder(5,5,5,5));
loginPanel.setVisible(true);
registerPanel.setVisible(false);
menuPanel.setVisible(false);
challengePanel.setVisible(false);
friendsPanel.setVisible(false);
rankPanel.setVisible(false);
contentPane.add(loginPanel,"loginPanel");
contentPane.add(registerPanel,"registerPanel");
contentPane.add(menuPanel,"menuPanel");
contentPane.add(challengePanel,"challengePanel");
contentPane.add(friendsPanel,"friendsPanel");
contentPane.add(rankPanel,"rankPanel");
window.add(contentPane);
/*************************************************************
** *
** LOGIN *
** *
*************************************************************/
JLabel lblLogin=new JLabel("Benvenuto");
lblLogin.setAlignmentX(Component.CENTER_ALIGNMENT);
lblLogin.setFont(new Font("",Font.PLAIN,20));
loginPanel.add(lblLogin);
loginPanel.add(Box.createVerticalStrut(20));
JLabel lblUser=new JLabel("Username:");
lblUser.setAlignmentX(Component.CENTER_ALIGNMENT);
loginPanel.add(lblUser);
JTextField usrLogin=new JTextField();
usrLogin.setAlignmentX(Component.CENTER_ALIGNMENT);
usrLogin.setMaximumSize(new Dimension(250,30));
loginPanel.add(usrLogin);
loginPanel.add(Box.createVerticalStrut(10));
JLabel lblPsw=new JLabel("Password:");
lblPsw.setAlignmentX(Component.CENTER_ALIGNMENT);
loginPanel.add(lblPsw);
JPasswordField pswLogin=new JPasswordField();
pswLogin.setAlignmentX(Component.CENTER_ALIGNMENT);
pswLogin.setMaximumSize(new Dimension(250,30));
loginPanel.add(pswLogin);
loginPanel.add(Box.createVerticalStrut(20));
JPanel loginButtonsPanel=new JPanel();
loginButtonsPanel.setLayout(new BoxLayout(loginButtonsPanel,BoxLayout.X_AXIS));
loginButtonsPanel.setVisible(true);
loginPanel.add(loginButtonsPanel);
JButton btnRegister=new JButton("Registrati");
btnRegister.setAlignmentX(Component.CENTER_ALIGNMENT);
btnRegister.addActionListener(e -> {
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"registerPanel");
});
loginButtonsPanel.add(btnRegister);
JButton btnLogin=new JButton("Accedi");
btnLogin.setAlignmentX(Component.CENTER_ALIGNMENT);
btnLogin.addActionListener(e -> {
try {
client=SocketChannel.open(new InetSocketAddress(2020));
String req="0 "+usrLogin.getText()+" "+new String(pswLogin.getPassword());
buf.clear();
buf.put(req.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
buf.clear();
client.read(buf);
buf.flip();
byte[] resp=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(resp);
String str=new String(resp);
if(str.equals("OK")) {
username[0]=usrLogin.getText();
lblMenu.setText("Bentornato "+username[0]);
t=new Thread(new ChallengeListener(client,window,challengePanel));
t.start();
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"menuPanel");
}
else
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
}
catch(IOException exc) {
JOptionPane.showMessageDialog(window,"Il server non è disponibile","Errore",JOptionPane.ERROR_MESSAGE);
}
});
loginButtonsPanel.add(btnLogin);
/*************************************************************
** *
** REGISTER *
** *
*************************************************************/
JLabel lblRegister=new JLabel("Registrazione:");
lblRegister.setAlignmentX(Component.CENTER_ALIGNMENT);
lblRegister.setFont(new Font("",Font.PLAIN,20));
registerPanel.add(lblRegister);
registerPanel.add(Box.createVerticalStrut(10));
JLabel lblRegisterInfo=new JLabel("Inserisci i seguenti dati per registrarti");
lblRegisterInfo.setAlignmentX(Component.CENTER_ALIGNMENT);
registerPanel.add(lblRegisterInfo);
registerPanel.add(Box.createVerticalStrut(20));
JLabel lblUserR=new JLabel("Username:");
lblUserR.setAlignmentX(Component.CENTER_ALIGNMENT);
registerPanel.add(lblUserR);
JTextField usrRegister=new JTextField();
usrRegister.setAlignmentX(Component.CENTER_ALIGNMENT);
usrRegister.setMaximumSize(new Dimension(250,30));
registerPanel.add(usrRegister);
registerPanel.add(Box.createVerticalStrut(10));
JLabel lblPswR=new JLabel("Password:");
lblPswR.setAlignmentX(Component.CENTER_ALIGNMENT);
registerPanel.add(lblPswR);
JPasswordField pswRegister=new JPasswordField();
pswRegister.setAlignmentX(Component.CENTER_ALIGNMENT);
pswRegister.setMaximumSize(new Dimension(250,30));
registerPanel.add(pswRegister);
registerPanel.add(Box.createVerticalStrut(10));
JLabel lblConfirmPsw=new JLabel("Conferma Password:");
lblConfirmPsw.setAlignmentX(Component.CENTER_ALIGNMENT);
registerPanel.add(lblConfirmPsw);
JPasswordField pswConfirmRegister=new JPasswordField();
pswConfirmRegister.setAlignmentX(Component.CENTER_ALIGNMENT);
pswConfirmRegister.setMaximumSize(new Dimension(250,30));
registerPanel.add(pswConfirmRegister);
registerPanel.add(Box.createVerticalStrut(20));
JPanel registerButtonsPanel=new JPanel();
registerButtonsPanel.setLayout(new BoxLayout(registerButtonsPanel,BoxLayout.X_AXIS));
registerButtonsPanel.setVisible(true);
registerPanel.add(registerButtonsPanel);
JButton btnBackToLogin=new JButton("Indietro");
btnBackToLogin.setAlignmentX(Component.CENTER_ALIGNMENT);
btnBackToLogin.addActionListener(e -> {
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"loginPanel");
});
registerButtonsPanel.add(btnBackToLogin);
JButton btnRegisterR=new JButton("Registrati");
btnRegisterR.setAlignmentX(Component.CENTER_ALIGNMENT);
btnRegisterR.addActionListener(e -> {
if(usrRegister.getText().equals(""))
JOptionPane.showMessageDialog(window,"Username non valido","Attenzione",JOptionPane.WARNING_MESSAGE);
else {
if(pswRegister.getPassword().length==0)
JOptionPane.showMessageDialog(window,"La password non può essere vuota","Attenzione",JOptionPane.WARNING_MESSAGE);
else {
if(!Arrays.equals(pswRegister.getPassword(),pswConfirmRegister.getPassword()))
JOptionPane.showMessageDialog(window,"Le password non corrispondono","Attenzione",JOptionPane.WARNING_MESSAGE);
else {
boolean flag=true;
try {
Registry r=LocateRegistry.getRegistry(2021);
Register reg=(Register)r.lookup(Register.SERVICE_NAME);
flag=reg.register(usrRegister.getText(),new String(pswRegister.getPassword()));
}
catch(RemoteException | NotBoundException exc) {
JOptionPane.showMessageDialog(window,"Il server non è disponibile","Errore",JOptionPane.ERROR_MESSAGE);
}
if(!flag)
JOptionPane.showMessageDialog(window,"Nome utente non disponibile","Attenzione",JOptionPane.WARNING_MESSAGE);
else {
JOptionPane.showMessageDialog(window,"Registrazione completata","Registrazione",JOptionPane.INFORMATION_MESSAGE);
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"loginPanel");
}
}
}
}
});
registerButtonsPanel.add(btnRegisterR);
lblMenu.setAlignmentX(Component.CENTER_ALIGNMENT);
lblMenu.setFont(new Font("",Font.PLAIN,20));
menuPanel.add(lblMenu);
menuPanel.add(Box.createVerticalStrut(20));
/*************************************************************
** *
** CHALLENGE *
** *
*************************************************************/
JButton btnChallenge=new JButton("Sfida");
btnChallenge.setAlignmentX(Component.CENTER_ALIGNMENT);
btnChallenge.setMaximumSize(new Dimension(300,50));
btnChallenge.setFont(new Font("",Font.PLAIN,15));
btnChallenge.addActionListener(e -> {
try {
challengePanel.removeAll();
JLabel lblChallenge=new JLabel("Attendi il tuo avversario...");
lblChallenge.setAlignmentX(Component.CENTER_ALIGNMENT);
lblChallenge.setFont(new Font("",Font.PLAIN,20));
challengePanel.add(lblChallenge);
JTextArea txtArea=new JTextArea();
txtArea.setFont(new Font("",Font.PLAIN,16));
txtArea.setEditable(false);
JScrollPane scrollTxt=new JScrollPane(txtArea);
scrollTxt.setMaximumSize(new Dimension(250,300));
challengePanel.add(scrollTxt);
JButton btnBackToMenu1=new JButton("Indietro");
btnBackToMenu1.setAlignmentX(Component.CENTER_ALIGNMENT);
btnBackToMenu1.addActionListener(e1 -> {
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"menuPanel");
});
challengePanel.add(btnBackToMenu1);
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"challengePanel");
String challengedFriend=JOptionPane.showInputDialog(window,"Chi vuoi sfidare?","Sfida un amico",JOptionPane.QUESTION_MESSAGE);
if(challengedFriend!=null) {
if(!challengedFriend.equals("")) {
String req="6 "+username[0]+" "+challengedFriend;
buf.clear();
buf.put(req.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
buf.clear();
client.read(buf);
buf.flip();
byte[] resp=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(resp);
String str=new String(resp);
// Gestione traduzioni per chi ha sfidato.
if(str.contains("TR")) {
while(str.contains("TR")) {
lblChallenge.setText("SFIDA");
String tr=JOptionPane.showInputDialog(window,"Traduci: "+str.replace("TR ",""),"Traduzione",JOptionPane.QUESTION_MESSAGE);
if(tr==null)
tr="/";
else{
if(tr.equals(""))
tr="/";
}
String sendTr="TR "+tr;
buf.clear();
buf.put(sendTr.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
String translateText=str.replace("TR ","")+" --> "+tr+"\n";
txtArea.append(translateText);
buf.clear();
client.read(buf);
buf.flip();
byte[] newTr=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(newTr);
str=new String(newTr);
}
if(str.contains("OK END"))
JOptionPane.showMessageDialog(window,str.replace("OK END ",""),"Risultato",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
}
else {
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
c.show(contentPane,"menuPanel");
}
}
else {
JOptionPane.showMessageDialog(window,"Non hai inserito nessun amico","Attenzione",JOptionPane.WARNING_MESSAGE);
c.show(contentPane,"menuPanel");
}
}
else
c.show(contentPane,"menuPanel");
}
catch(IOException exc) {
JOptionPane.showMessageDialog(window,"Errore nella comunicazione con il server","Errore",JOptionPane.ERROR_MESSAGE);
}
});
menuPanel.add(btnChallenge);
/*************************************************************
** *
** ADD FRIEND *
** *
*************************************************************/
JButton btnAddFriend=new JButton("Aggiungi Amico");
btnAddFriend.setAlignmentX(Component.CENTER_ALIGNMENT);
btnAddFriend.setMaximumSize(new Dimension(300,50));
btnAddFriend.setFont(new Font("",Font.PLAIN,15));
btnAddFriend.addActionListener(e -> {
try {
String newFriend=JOptionPane.showInputDialog(window,"Con chi vuoi fare amicizia?","Aggiungi Amico",JOptionPane.QUESTION_MESSAGE);
if(newFriend!=null) {
if(!newFriend.equals("")){
String req="2 "+username[0]+" "+newFriend;
buf.clear();
buf.put(req.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
buf.clear();
client.read(buf);
buf.flip();
byte[] resp=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(resp);
String str=new String(resp);
if(str.equals("OK"))
JOptionPane.showMessageDialog(window,"Ora tu e "+newFriend+" siete amici!","Amico Aggiunto",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(window,"Non hai inserito nessun amico","Attenzione",JOptionPane.WARNING_MESSAGE);
}
}
catch(IOException exc) {
JOptionPane.showMessageDialog(window,"Errore nella comunicazione con il server","Errore",JOptionPane.ERROR_MESSAGE);
}
});
menuPanel.add(btnAddFriend);
/*************************************************************
** *
** FRIENDLIST *
** *
*************************************************************/
JLabel lblFriendlist=new JLabel("I tuoi amici:");
lblFriendlist.setAlignmentX(Component.CENTER_ALIGNMENT);
lblFriendlist.setFont(new Font("",Font.PLAIN,20));
friendsPanel.add(lblFriendlist);
friendsPanel.add(Box.createVerticalStrut(20));
DefaultListModel<String> friendsModel=new DefaultListModel<String>();
JList<String> jFriendlist=new JList<String>(friendsModel);
jFriendlist.setFont(new Font("",Font.PLAIN,15));
jFriendlist.setOpaque(false);
DefaultListCellRenderer renderList = new DefaultListCellRenderer();
renderList.setOpaque(false);
renderList.setHorizontalAlignment(SwingConstants.CENTER);
jFriendlist.setCellRenderer(renderList);
JScrollPane scrollFriends=new JScrollPane(jFriendlist);
scrollFriends.setMaximumSize(new Dimension(250,300));
scrollFriends.setOpaque(false);
scrollFriends.getViewport().setOpaque(false);
friendsPanel.add(scrollFriends);
JButton btnBackToMenu=new JButton("Indietro");
btnBackToMenu.setAlignmentX(Component.CENTER_ALIGNMENT);
btnBackToMenu.addActionListener(e -> {
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"menuPanel");
});
friendsPanel.add(btnBackToMenu);
JButton btnFriendlist=new JButton("Visualizza Amici");
btnFriendlist.setAlignmentX(Component.CENTER_ALIGNMENT);
btnFriendlist.setMaximumSize(new Dimension(300,50));
btnFriendlist.setFont(new Font("",Font.PLAIN,15));
btnFriendlist.addActionListener(e -> {
try {
String req="3 "+username[0];
buf.clear();
buf.put(req.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
buf.clear();
client.read(buf);
buf.flip();
byte[] resp=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(resp);
String str=new String(resp);
if(str.contains("OK")) {
JsonArray jsonArr=new Gson().fromJson(str.replace("OK ",""),JsonArray.class);
String[] fList=new Gson().fromJson(jsonArr,String[].class);
friendsModel.clear();
for(String s:fList)
friendsModel.addElement(s);
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"friendsPanel");
}
else
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
}
catch(IOException exc) {
JOptionPane.showMessageDialog(window,"Il server non è disponibile","Errore",JOptionPane.ERROR_MESSAGE);
}
});
menuPanel.add(btnFriendlist);
/*************************************************************
** *
** SCORE *
** *
*************************************************************/
JButton btnScore=new JButton("Punteggio");
btnScore.setAlignmentX(Component.CENTER_ALIGNMENT);
btnScore.setMaximumSize(new Dimension(300,50));
btnScore.setFont(new Font("",Font.PLAIN,15));
btnScore.addActionListener(e -> {
try {
String req="4 "+username[0];
buf.clear();
buf.put(req.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
buf.clear();
client.read(buf);
buf.flip();
byte[] resp=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(resp);
String str=new String(resp);
if(str.contains("OK"))
JOptionPane.showMessageDialog(window,"Il tuo punteggio è: "+str.replace("OK ",""),"Punteggio",JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
}
catch(IOException exc) {
JOptionPane.showMessageDialog(window,"Errore nella comunicazione con il server","Errore",JOptionPane.ERROR_MESSAGE);
}
});
menuPanel.add(btnScore);
/*************************************************************
** *
** RANKING *
** *
*************************************************************/
JLabel lblRanking=new JLabel("Classifica:");
lblRanking.setAlignmentX(Component.CENTER_ALIGNMENT);
lblRanking.setFont(new Font("",Font.PLAIN,20));
rankPanel.add(lblRanking);
rankPanel.add(Box.createVerticalStrut(20));
DefaultTableModel rankModel=new DefaultTableModel();
JTable jRank=new JTable(rankModel);
jRank.getTableHeader().setFont(new Font("",Font.BOLD,16));
jRank.setFont(new Font("",Font.PLAIN,14));
jRank.setOpaque(false);
DefaultTableCellRenderer renderTable = new DefaultTableCellRenderer();
renderTable.setOpaque(false);
renderTable.setHorizontalAlignment(SwingConstants.CENTER);
jRank.setDefaultRenderer(Object.class,renderTable);
JScrollPane scrollRank=new JScrollPane(jRank);
scrollRank.setMaximumSize(new Dimension(250,300));
scrollRank.setOpaque(false);
scrollRank.getViewport().setOpaque(false);
rankPanel.add(scrollRank);
JButton btnBackToMenu1=new JButton("Indietro");
btnBackToMenu1.setAlignmentX(Component.CENTER_ALIGNMENT);
btnBackToMenu1.addActionListener(e -> {
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"menuPanel");
});
rankPanel.add(btnBackToMenu1);
JButton btnRank=new JButton("Classifica");
btnRank.setAlignmentX(Component.CENTER_ALIGNMENT);
btnRank.setMaximumSize(new Dimension(300,50));
btnRank.setFont(new Font("",Font.PLAIN,15));
btnRank.addActionListener(e -> {
try {
String req="5 "+username[0];
buf.clear();
buf.put(req.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
buf.clear();
client.read(buf);
buf.flip();
byte[] resp=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(resp);
String str=new String(resp);
if(str.contains("OK")) {
JsonObject obj=new Gson().fromJson(str.replace("OK ",""),JsonObject.class);
Set<Map.Entry<String,JsonElement>> rankSet=obj.entrySet();
rankModel.setColumnCount(0);
rankModel.addColumn("Nome");
rankModel.addColumn("Punteggio");
for(Map.Entry<String,JsonElement> me:rankSet) {
Object[] row={me.getKey(),me.getValue().getAsString()};
rankModel.addRow(row);
}
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"rankPanel");
}
else
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
}
catch(IOException exc) {
JOptionPane.showMessageDialog(window,"Il server non è disponibile","Errore",JOptionPane.ERROR_MESSAGE);
}
});
menuPanel.add(btnRank);
/*************************************************************
** *
** LOGOUT *
** *
*************************************************************/
JButton btnLogout=new JButton("Esci");
btnLogout.setAlignmentX(Component.CENTER_ALIGNMENT);
btnLogout.setMaximumSize(new Dimension(300,50));
btnLogout.setFont(new Font("",Font.PLAIN,15));
btnLogout.addActionListener(e -> {
try {
String req="1 "+username[0];
buf.clear();
buf.put(req.getBytes());
buf.flip();
while(buf.hasRemaining())
client.write(buf);
buf.clear();
client.read(buf);
buf.flip();
byte[] resp=new byte[buf.remaining()];
while(buf.hasRemaining())
buf.get(resp);
String str=new String(resp);
if(str.equals("OK")) {
t.interrupt();
usrLogin.setText("");
pswLogin.setText("");
CardLayout c=(CardLayout) contentPane.getLayout();
c.show(contentPane,"loginPanel");
}
else
JOptionPane.showMessageDialog(window,str.replace("KO",""),"Attenzione",JOptionPane.WARNING_MESSAGE);
}
catch(IOException exc) {
JOptionPane.showMessageDialog(window,"Errore nella comunicazione con il server","Errore",JOptionPane.ERROR_MESSAGE);
}
});
menuPanel.add(btnLogout);
window.setVisible(true);
}
}