-
Notifications
You must be signed in to change notification settings - Fork 0
/
JungleIG.java
370 lines (316 loc) · 12.7 KB
/
JungleIG.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
//FINI
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* Classe JungleIG : Interface graphique du jeu
*/
class JungleIG extends JFrame implements ActionListener {
/* attributs de communication */
private Socket comm;
public ObjectOutputStream oos;
public ObjectInputStream ois;
/* panneau connection */
protected JPanel panConn;
protected JTextField textServerIP;
protected JTextField textPseudo;
protected JButton butConnect;
/* panneau avant partie */
protected JPanel panInit;
public JTextArea textInfoInit;
protected JButton butListParty;
protected JButton butCreateParty;
protected JSpinner spinNbPlayer;
protected JTextField textCreate;
protected JButton butJoinParty;
protected JTextField textJoin;
/* widgets for party panel */
protected JPanel panParty;
public JTextArea textInfoParty;
protected JTextField textPlay;
protected JButton butPlay;
protected JButton butQuit;
/* Autres attributs */
// valeur false à chaque fois IG débloque le bouton play.
// la valeur true si le bouton de lecture est cliqué tout déverrouillé
public boolean orderSent;
public JungleIG() {
createWidget();
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* methode creatWidget
* dimmensionne panelConnect et panelInit
*/
public void createWidget() {
panConn = createPanelConnect();
panConn.setSize(new Dimension(350, 100));
panInit = createPanelInit();
panInit.setPreferredSize(new Dimension(1200, 300));
panParty = createPanelPlay();
setContentPane(panConn);
}
/**
* Méthode de création du panel de Connection
* @return
*/
private JPanel createPanelConnect() {
JPanel panAll = new JPanel(new BorderLayout());
JPanel panPseudo = new JPanel();
textPseudo = new JTextField("", 20);
textPseudo.setMaximumSize(textPseudo.getPreferredSize());
panPseudo.add(new JLabel("Pseudo: "));
panPseudo.add(textPseudo);
JPanel panConn = new JPanel();
textServerIP = new JTextField("127.0.0.1", 15);
panConn.add(new JLabel("Server IP: "));
panConn.add(textServerIP);
butConnect = new JButton("Connect");
butConnect.addActionListener(this);
panAll.add(panPseudo, BorderLayout.NORTH);
panAll.add(panConn, BorderLayout.CENTER);
panAll.add(butConnect, BorderLayout.SOUTH);
return panAll;
}
/**
* Méthode de création du panelInit
* @return
*/
private JPanel createPanelInit() {
JPanel panRight = new JPanel();
panRight.setLayout(new BoxLayout(panRight, BoxLayout.Y_AXIS));
butListParty = new JButton("Liste des parties");
butListParty.addActionListener(this);
JPanel panCreate = new JPanel();
panCreate.setLayout(new BoxLayout(panCreate, BoxLayout.X_AXIS));
textCreate = new JTextField("", 40);
textCreate.setMaximumSize(textCreate.getPreferredSize());
butCreateParty = new JButton("Creation de parties ");
butCreateParty.addActionListener(this);
SpinnerModel model = new SpinnerNumberModel(3, 2, 8, 1);
spinNbPlayer = new JSpinner(model);
spinNbPlayer.setMaximumSize(spinNbPlayer.getPreferredSize());
panCreate.add(new JLabel("Nouveau nom de partie : "));
panCreate.add(textCreate);
panCreate.add(Box.createHorizontalStrut(20));
panCreate.add(new JLabel("Nombres de joueurs : "));
panCreate.add(spinNbPlayer);
panCreate.add(butCreateParty);
JPanel panJoin = new JPanel();
panJoin.setLayout(new BoxLayout(panJoin, BoxLayout.X_AXIS));
textJoin = new JTextField("", 2);
textJoin.setMaximumSize(textJoin.getPreferredSize());
butJoinParty = new JButton("Rejoindre la partie ");
butJoinParty.addActionListener(this);
panJoin.add(new JLabel("Num Partie : "));
panJoin.add(textJoin);
panJoin.add(butJoinParty);
panRight.add(butListParty);
panRight.add(panCreate);
panRight.add(panJoin);
panRight.add(Box.createVerticalGlue());
textInfoInit = new JTextArea(20, 100);
textInfoInit.setLineWrap(true);
JScrollPane scroll = new JScrollPane(textInfoInit, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel panAll = new JPanel();
panAll.setLayout(new BoxLayout(panAll, BoxLayout.X_AXIS));
panAll.add(scroll);
panAll.add(panRight);
return panAll;
}
/**
* Méthode de création du panel de Jeu
* @return
*/
private JPanel createPanelPlay() {
JPanel panAll = new JPanel(new BorderLayout());
textInfoParty = new JTextArea(20, 100);
textInfoParty.setLineWrap(true);
JScrollPane scroll = new JScrollPane(textInfoParty, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JPanel panPlay = new JPanel();
textPlay = new JTextField("", 5);
textPlay.setMaximumSize(textPlay.getPreferredSize());
butPlay = new JButton("Jouer");
butPlay.addActionListener(this);
panPlay.add(new JLabel("Ordre : "));
panPlay.add(textPlay);
enableOrder(false);
JPanel panRight = new JPanel(new BorderLayout());
panRight.add(panPlay, BorderLayout.CENTER);
panRight.add(butPlay, BorderLayout.SOUTH);
JPanel panMain = new JPanel();
panMain.add(scroll);
panMain.add(panRight);
butQuit = new JButton("Quitter");
butQuit.addActionListener(this);
panAll.add(panMain, BorderLayout.CENTER);
panAll.add(butQuit, BorderLayout.SOUTH);
return panAll;
}
public void setConnectionPanel() {
setContentPane(panConn);
pack();
}
public void setInitPanel() {
setContentPane(panInit);
pack();
}
public void setPartyPanel() {
setContentPane(panParty);
pack();
}
public void enableOrder(boolean state) {
textPlay.setEnabled(state);
butPlay.setEnabled(state);
if (state) {
orderSent = false;
}
}
/**
* Methode de connection avec le serveur
* @return
* @throws IOException
*/
public boolean serverConnection() throws IOException {
boolean ok = false;
// créer la connexion au serveur, ainsi que les flux uniquement si elle n'est pas active
if (comm == null) {
try {
//Récupération de l'adresse du serveur
comm = new Socket(textServerIP.getText(), 12345);
oos = new ObjectOutputStream(comm.getOutputStream());
ois = new ObjectInputStream(comm.getInputStream());
// envoyer le pseudo du joueur
oos.writeObject(textPseudo.getText());
oos.flush();
// lire un booléen -> ok
ok = true;
} catch (IOException e) {
System.out.println("problème de connexion au serveur : (JungleIG)" + e.getMessage());
System.exit(1);
}
}
return ok;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == butConnect) {
try {
boolean ok = serverConnection();
if (ok) {
setInitPanel();
} else {
System.out.println("Le pseudo existe déjà, choisissez en un autre.");
}
} catch (IOException err) {
System.err.println("Problème de connection avec le serveur: " + err.getMessage() + "\n.Arrêt...");
System.exit(1);
}
} else if (e.getSource() == butListParty) {
try {
// envoyer requête LIST PARTY
oos.writeInt(JungleServer.REQ_LISTPARTY);
oos.flush();
// recevoir résultat et l'afficher dans textInfoInit
System.out.println("flush");
boolean pret = ois.readBoolean();
System.out.println(pret);
if (pret) {
String nomParty = (String) ois.readObject();
System.out.println("apres read");
textInfoInit.append(nomParty+" ");
}
} catch (ClassNotFoundException err) {
} catch (IOException err) {
System.err.println("Problème de connection avec le serveur: " + err.getMessage() + "\n.Arrêt...");
System.exit(1);
}
} else if (e.getSource() == butCreateParty) {
try {
boolean ok;
// envoyer requête CREATE PARTY (paramètres : nom partie et nb joueurs nécessaires)
oos.writeInt(JungleServer.REQ_CREATEPARTY);
oos.writeObject(textCreate.getText());
int nbJoueurs = (Integer) spinNbPlayer.getValue();
oos.writeInt(nbJoueurs);
oos.flush();
// recevoir résultat -> ok
ok = ois.readBoolean();
System.out.println(ok);
// si ok == true :
if (ok) {
// mettre le panneau party au centre
setPartyPanel();
// afficher un message dans textInfoParty comme quoi il faut attendre le début de partie
textInfoParty.append("Attendre le début de la partie");
// créer un ThreadClient et lancer son exécution
ThreadClient threadClient = new ThreadClient(this);
threadClient.start();
}
} catch (IOException err) {
System.err.println("probleme de connection serveur : " + err.getMessage() + "\n.Aborting...");
System.exit(1);
}
} else if (e.getSource() == butJoinParty) {
try {
int idPlayer;
// envoyer requête JOIN PARTY (paramètres : numero partie)
oos.writeInt(JungleServer.REQ_JOINPARTY);
oos.flush();
System.out.println("requete envoyée");
int numPartie = Integer.parseInt(textJoin.getText());
oos.writeInt(numPartie);
oos.flush();
System.out.println("numPartie flushé");
// recevoir résultat -> idPlayer
idPlayer = ois.readInt();
System.out.println("idplayer reçu : " + idPlayer);
// si idPlayer >= 1 :
if (idPlayer >= 1) {
// mettre le panneau party au centre
setPartyPanel();
// afficher un message dans textInfoParty comme quoi il faut attendre le début de partie
textInfoParty.append("Attendre la début de la partie");
// créer un ThreadClient et lancer son exécution
System.out.println("avant démarrage du thread");
ThreadClient threadClient = new ThreadClient(this);
threadClient.start();
System.out.println("Apres le thread");
}
} catch (IOException err) {
System.err.println("Problème de connection serveur: " + err.getMessage() + "\n.Arrêt...");
System.exit(1);
}
} else if (e.getSource() == butPlay) {
try {
// envoyer requête PLAY (paramètre : contenu de textPlay)
oos.writeInt(JungleServer.REQ_PLAY);
oos.writeObject(textPlay.getText());
oos.flush();
orderSent = true;
butPlay.setEnabled(false);
textPlay.setEnabled(false);
// mettre orderSent à true
// bloquer le bouton play et le textfiled associé
} catch (IOException err) {
System.err.println("Problème connection serveur: " + err.getMessage() + "\n.Arrêt...");
System.exit(1);
}
} else if (e.getSource() == butQuit) {
try {
oos.close();
ois.close();
setConnectionPanel();
comm = null;
} catch (IOException err) {
System.err.println("Problème connection serveur: " + err.getMessage() + "\n.Arrêt...");
System.exit(1);
}
}
}
}