-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPongClient.java
343 lines (256 loc) · 8.28 KB
/
PongClient.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pingpong;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class PongClient extends JFrame implements KeyListener, Runnable, WindowListener{
/**
* Pong Client
*/
private static final long serialVersionUID = 1L;
///////////////////
// - Variables - //
///////////////////
// - Frame - //
private static final String TITLE = "ping-pong::client";
private static final int WIDTH = 800; // - Width size for window - //
private static final int HEIGHT = 460; // - Height size for window - //
boolean isRunning = false;
// - Players - //
private PlayerServer playerS;
private PlayerClient playerC;
private int barR = 30; // - Player bar width - //
private int playerH = 120; // - Player bar height - //
private int mPLAYER = 5; // - Moving of the player bar - //
// - Server - //
private static Socket clientSoc;
private int portAdd;
private String ipAdd;
private boolean reset = false;
private int countS = 0;
// - Graphical - //
private Graphics g;
private Font sFont = new Font("TimesRoman",Font.BOLD,90);
private Font mFont = new Font("TimesRoman",Font.BOLD,50);
private Font nFont = new Font("TimesRoman",Font.BOLD,32);
private Font rFont = new Font("TimesRoman",Font.BOLD,18);
private String[] message; // - Split Message to two piece in an array - //
// - Constructor - //
public PongClient(String clientname, String portAdd, String ipAdd){
// - Players - //
playerS = new PlayerServer();
playerC = new PlayerClient(clientname);
playerS.setName(clientname);
// - Socket - //
this.ipAdd = ipAdd;
this.portAdd = Integer.parseInt(portAdd);
this.isRunning = true;
// - Frame - //
this.setTitle(TITLE);
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
addKeyListener(this);
//addWindowListener(this);
// -
}
/////////////
// - Run - //
/////////////
@Override
public void run() {
// TODO Auto-generated method stub
// Server Socket //
try {
System.out.println("Finding server...\nConnecting to "+ipAdd+":"+portAdd);
clientSoc = new Socket(ipAdd, portAdd);
System.out.println("Connected to server...");
if(clientSoc.isConnected()){
System.out.println("TEST");
// - define get send objects - //
while(true){
// - Creating Streams - //
ObjectOutputStream sendObj = new ObjectOutputStream(clientSoc.getOutputStream());
sendObj.writeObject(playerC);
sendObj = null;
ObjectInputStream getObj = new ObjectInputStream(clientSoc.getInputStream());
playerS = (PlayerServer) getObj.readObject();
getObj = null;
// - For next game -> reset restart status - //
if(reset){
if(countS>5){
playerC.restart = false;
reset = false;
countS = 0;
}
}
countS++;
repaint();
}
}
else{
System.out.println("Disconnected...");
}
}
catch (Exception e) {System.out.println(e);}
}
///////////////
// - Paint - //
///////////////
private Image createImage(){
BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = bufferedImage.createGraphics();
// - Table - //
g.setColor(new Color(15,9,9));
g.fillRect(0, 0, WIDTH, HEIGHT);
// - Lines - //
g.setColor(Color.white);
g.fillRect(WIDTH/2-5, 0, 5, HEIGHT);
g.fillRect(WIDTH/2+5, 0, 5, HEIGHT);
// - Score - //
g.setColor(new Color(228,38,36));
g.setFont(sFont);
g.drawString(""+playerS.getScoreS(), WIDTH/2-60, 120);
g.drawString(""+playerS.getScoreP(), WIDTH/2+15, 120);
// - Player Names - //
g.setFont(nFont);
g.setColor(Color.white);
g.drawString(playerS.getName(),WIDTH/10,HEIGHT-20);
g.drawString(playerC.getName(),600,HEIGHT-20);
// - Player's Bar - //
g.setColor(new Color(57,181,74));
g.fillRect(playerS.getX(), playerS.getY(), barR, playerH);
g.setColor(new Color(57,181,74));
g.fillRect(playerC.getX(), playerC.getY(), barR, playerH);
// - Ball - //
g.setColor(new Color(255,255,255));
g.fillOval(playerS.getBallx(), playerS.getBally(), 45, 45);
g.setColor(new Color(228,38,36));
g.fillOval(playerS.getBallx()+5, playerS.getBally()+5, 45-10, 45-10);
// - Message - //
message = playerS.getImessage().split("-");
g.setFont(mFont);
g.setColor(Color.white);
if(message.length!=0){
g.drawString(message[0],WIDTH/4-31,HEIGHT/2+38);
if(message.length>1){
if(message[1].length()>6){
g.setFont(rFont);
g.setColor(new Color(228,38,36));
g.drawString(message[1],WIDTH/4-31,HEIGHT/2+100);
}
}
}
return bufferedImage;
}
public void paint(Graphics g){
g.drawImage(createImage(), 0, 0, this);
playerC.ok = true;
}
///////////////////////
// - Update Player - //
///////////////////////
// - Move bar to Up - //
public void playerUP(){
if(playerC.getY() - mPLAYER > playerH/2-10){
playerC.setY(playerC.getY()-mPLAYER);
}
}
// - Move bar to Down - //
public void playerDOWN(){
if(playerC.getY() + mPLAYER < HEIGHT - playerH - 30){
playerC.setY(playerC.getY()+mPLAYER);
}
}
/////////////////////
// - KeyListener - //
/////////////////////
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
int keycode = arg0.getKeyCode();
if(keycode == KeyEvent.VK_UP){
playerUP();
repaint();
}
if(keycode == KeyEvent.VK_DOWN){
playerDOWN();
repaint();
}
if(playerS.isRestart()){
playerC.restart = true;
reset = true;
}
if(keycode == KeyEvent.VK_ESCAPE || keycode == KeyEvent.VK_N && playerS.isRestart()){
try {
this.setVisible(false);
clientSoc.close();
System.exit(EXIT_ON_CLOSE);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@SuppressWarnings("deprecation")
@Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
Thread.currentThread().stop();
this.setVisible(false);
try {
clientSoc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub
}
}