-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuActionListener.java
More file actions
330 lines (255 loc) · 8.02 KB
/
MenuActionListener.java
File metadata and controls
330 lines (255 loc) · 8.02 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
package chessGame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;
import java.util.*;
import java.lang.Thread.*;
import java.net.ServerSocket;
import javax.imageio.*;
import javax.swing.*;
class MenuActionListener implements ActionListener {
JFrame frame;
menu Menu;
DisplayGraphics graphics;
ClientServerMenu csmenu;
DifficultyMenu Dmenu;
Server srv = null;
Client c;
JTextField jf;
JPanel textfield;
MenuActionListener(JFrame f, menu m, DisplayGraphics g) {
this.frame = f;
this.Menu = m;
this.graphics = g;
Menu.b1.addActionListener(this);
Menu.b2.addActionListener(this);
Menu.b3.addActionListener(this);
csmenu = new ClientServerMenu();
Dmenu = new DifficultyMenu();
}
//***This is where the chess game is executed****
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand() == "1") {
//take menu off of frame
frame.remove(Menu.menuscreen);
frame.add(Dmenu.menuscreen);
frame.revalidate();
frame.setVisible(true);
Dmenu.b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
frame.remove(Dmenu.menuscreen);
Board brd = new Board(1,true);
frame.add(brd.checkDisplay);
frame.add(brd.pane);
frame.add(brd.activePlayer);
frame.add(brd.capPane);
frame.add(graphics);
frame.setVisible(true);
}
});
Dmenu.b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
frame.remove(Dmenu.menuscreen);
Board brd = new Board(1,true);
frame.add(brd.checkDisplay);
frame.add(brd.pane);
frame.add(brd.activePlayer);
frame.add(brd.capPane);
frame.add(graphics);
frame.setVisible(true);
}
});
Dmenu.b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
frame.remove(Dmenu.menuscreen);
Board brd = new Board(2,true);
frame.add(brd.checkDisplay);
frame.add(brd.pane);
frame.add(brd.activePlayer);
frame.add(brd.capPane);
frame.add(graphics);
frame.setVisible(true);
}
});
}
else if (e.getActionCommand() == "2") {
frame.getContentPane();
frame.remove(Menu.menuscreen);
frame.add(csmenu.menuscreen);
frame.revalidate();
frame.setVisible(true);
csmenu.b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
srv = new Server(111, 2);
try {
srv.run();
//srv.ServerHandler();
(new Thread(new ServerHandler(srv))).start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
csmenu.b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
jf = new JTextField(30);
jf.setPreferredSize( new Dimension( 100, 50 ) );
frame.remove(csmenu.menuscreen);
textfield = new JPanel();
JLabel add = new JLabel("Enter IP address");
textfield.add(add);
textfield.add(jf);
frame.add(textfield);
frame.revalidate();
frame.setVisible(true);
jf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String address = jf.getText();
c = new Client("",address,111);
try {
c.run();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int data = 0;
try {
data = c.dataAvailable();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(data == 0) {
try {
data = c.dataAvailable();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
String color = c.ClientRead();
int colorInt = Integer.parseInt(color);
c.setColor(colorInt);
RemoteBoard brd = new RemoteBoard(colorInt,c);
frame.remove(textfield);
frame.revalidate();
frame.add(brd.colordisplay);
frame.add(brd.checkDisplay);
frame.add(brd.pane);
frame.add(brd.activePlayer);
frame.add(brd.capPane);
frame.add(graphics);
//frame.add(brd.colordisplay);
frame.setVisible(true);
System.out.print("Waiting on other player..\n");
c.ClientRead(); // block untill both players are connected
(new Thread(new HandleBoardState(brd,c))).start();
}
});
}
});
}
else if (e.getActionCommand() == "3") {
System.out.print("Closing..");
System.exit(0);
}
}
}
class ServerHandler implements Runnable {
Server srv;
public ServerHandler(Server srv) {
this.srv = srv;
}
public void run() {
while(true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
int data1 = srv.in1.available();
int data2 = srv.in2.available();
if(data1 != 0) {
//String[] values = CSV.split(",");
System.out.print("Data read from client1\n");
String move1 = srv.in1.readUTF();
srv.out2.writeUTF(move1); //send move to other client
srv.out2.flush();
System.out.print("Client1 move sent to client2\n");
String c = srv.in2.readUTF();
System.out.print("Client2 comfirmed\n");
srv.out1.writeUTF("ok"); //send ok back to calling client
srv.out1.flush();
System.out.print("Sent ok to client1\n");
}
else if (data2 != 0) {
//String[] values = CSV.split(",");
String move2 = srv.in2.readUTF();
System.out.printf("Data read %s from client2\n", move2);
srv.out1.writeUTF(move2);
srv.out1.flush();
System.out.printf("%s sent to client1\n", move2);
String c = srv.in1.readUTF();
System.out.print("Client1 comfirmed\n");
srv.out2.writeUTF("ok");
srv.out2.flush();
System.out.print("Sent ok to Client2");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class HandleBoardState implements Runnable {
RemoteBoard brd;
Client client;
public HandleBoardState(RemoteBoard brd, Client client) {
this.brd = brd;
this.client = client;
}
public void run() {
while(true) {
//System.out.print("f");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(brd.moves % 2 != client.color) {
System.out.print("About to read move\n");
String move = client.ClientRead();
System.out.printf("%s Recieved\n", move);
if(move.length() > 2){
String[] values = move.split(",");
String oldX = values[0];
String oldY = values[1];
String newX = values[2];
String newY = values[3];
brd.moveUnit(Integer.parseInt(oldX),Integer.parseInt(oldY));
brd.mover.setUnits(brd.units);
brd.placeUnit(Integer.parseInt(newX), Integer.parseInt(newY));
brd.client.ClientWrite("confirm");
System.out.print("Confirm sent\n");
brd.moves++;
brd.updatePlayer();
}
}
}
}
}