-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathClientGUI.java
More file actions
313 lines (254 loc) · 9.88 KB
/
ClientGUI.java
File metadata and controls
313 lines (254 loc) · 9.88 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
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* ClientGUI.java
*
* Created on 21 ãÇÑÓ, 2008, 02:26 ã
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Mohamed Talaat Saad
*/
public class ClientGUI extends JFrame implements ActionListener,WindowListener
{
/** Creates a new instance of ClientGUI */
private JLabel ipaddressLabel;
private JLabel portLabel;
private static JLabel scoreLabel;
private JTextField ipaddressText;
private JTextField portText;
private JButton registerButton;
private JPanel registerPanel;
public static JPanel gameStatusPanel;
private Client client;
private Tank clientTank;
private static int score;
int width=790,height=580;
boolean isRunning=true;
private GameBoardPanel boardPanel;
private SoundManger soundManger;
public ClientGUI()
{
score=0;
setTitle("Multiclients Tanks Game");
setSize(width,height);
setLocation(60,100);
getContentPane().setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
addWindowListener(this);
registerPanel=new JPanel();
registerPanel.setBackground(Color.YELLOW);
registerPanel.setSize(200,140);
registerPanel.setBounds(560,50,200,140);
registerPanel.setLayout(null);
gameStatusPanel=new JPanel();
gameStatusPanel.setBackground(Color.YELLOW);
gameStatusPanel.setSize(200,300);
gameStatusPanel.setBounds(560,210,200,311);
gameStatusPanel.setLayout(null);
ipaddressLabel=new JLabel("IP address: ");
ipaddressLabel.setBounds(10,25,70,25);
portLabel=new JLabel("Port: ");
portLabel.setBounds(10,55,50,25);
scoreLabel=new JLabel("Score : 0");
scoreLabel.setBounds(10,90,100,25);
ipaddressText=new JTextField("localhost");
ipaddressText.setBounds(90,25,100,25);
portText=new JTextField("11111");
portText.setBounds(90,55,100,25);
registerButton=new JButton("Register");
registerButton.setBounds(60,100,90,25);
registerButton.addActionListener(this);
registerButton.setFocusable(true);
registerPanel.add(ipaddressLabel);
registerPanel.add(portLabel);
registerPanel.add(ipaddressText);
registerPanel.add(portText);
registerPanel.add(registerButton);
gameStatusPanel.add(scoreLabel);
client=Client.getGameClient();
clientTank=new Tank();
boardPanel=new GameBoardPanel(clientTank,client,false);
getContentPane().add(registerPanel);
getContentPane().add(gameStatusPanel);
getContentPane().add(boardPanel);
setVisible(true);
}
public static int getScore()
{
return score;
}
public static void setScore(int scoreParametar)
{
score+=scoreParametar;
scoreLabel.setText("Score : "+score);
}
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();
if(obj==registerButton)
{
registerButton.setEnabled(false);
try
{
client.register(ipaddressText.getText(),Integer.parseInt(portText.getText()),clientTank.getXposition(),clientTank.getYposition());
soundManger=new SoundManger();
boardPanel.setGameStatus(true);
boardPanel.repaint();
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
new ClientRecivingThread(client.getSocket()).start();
registerButton.setFocusable(false);
boardPanel.setFocusable(true);
} catch (IOException ex)
{
JOptionPane.showMessageDialog(this,"The Server is not running, try again later!","Tanks 2D Multiplayer Game",JOptionPane.INFORMATION_MESSAGE);
System.out.println("The Server is not running!");
registerButton.setEnabled(true);
}
}
}
public void windowOpened(WindowEvent e)
{
}
public void windowClosing(WindowEvent e)
{
// int response=JOptionPane.showConfirmDialog(this,"Are you sure you want to exit ?","Tanks 2D Multiplayer Game!",JOptionPane.YES_NO_OPTION);
Client.getGameClient().sendToServer(new Protocol().ExitMessagePacket(clientTank.getTankID()));
}
public void windowClosed(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowActivated(WindowEvent e) {
}
public void windowDeactivated(WindowEvent e) {
}
public class ClientRecivingThread extends Thread
{
Socket clientSocket;
DataInputStream reader;
public ClientRecivingThread(Socket clientSocket)
{
this.clientSocket=clientSocket;
try {
reader=new DataInputStream(clientSocket.getInputStream());
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void run()
{
while(isRunning)
{
String sentence="";
try {
sentence=reader.readUTF();
} catch (IOException ex) {
ex.printStackTrace();
}
if(sentence.startsWith("ID"))
{
int id=Integer.parseInt(sentence.substring(2));
clientTank.setTankID(id);
System.out.println("My ID= "+id);
}
else if(sentence.startsWith("NewClient"))
{
int pos1=sentence.indexOf(',');
int pos2=sentence.indexOf('-');
int pos3=sentence.indexOf('|');
int x=Integer.parseInt(sentence.substring(9,pos1));
int y=Integer.parseInt(sentence.substring(pos1+1,pos2));
int dir=Integer.parseInt(sentence.substring(pos2+1,pos3));
int id=Integer.parseInt(sentence.substring(pos3+1,sentence.length()));
if(id!=clientTank.getTankID())
boardPanel.registerNewTank(new Tank(x,y,dir,id));
}
else if(sentence.startsWith("Update"))
{
int pos1=sentence.indexOf(',');
int pos2=sentence.indexOf('-');
int pos3=sentence.indexOf('|');
int x=Integer.parseInt(sentence.substring(6,pos1));
int y=Integer.parseInt(sentence.substring(pos1+1,pos2));
int dir=Integer.parseInt(sentence.substring(pos2+1,pos3));
int id=Integer.parseInt(sentence.substring(pos3+1,sentence.length()));
if(id!=clientTank.getTankID())
{
boardPanel.getTank(id).setXpoistion(x);
boardPanel.getTank(id).setYposition(y);
boardPanel.getTank(id).setDirection(dir);
boardPanel.repaint();
}
}
else if(sentence.startsWith("Shot"))
{
int id=Integer.parseInt(sentence.substring(4));
if(id!=clientTank.getTankID())
{
boardPanel.getTank(id).Shot();
}
}
else if(sentence.startsWith("Remove"))
{
int id=Integer.parseInt(sentence.substring(6));
if(id==clientTank.getTankID())
{
int response=JOptionPane.showConfirmDialog(null,"Sorry, You are loss. Do you want to try again ?","Tanks 2D Multiplayer Game",JOptionPane.OK_CANCEL_OPTION);
if(response==JOptionPane.OK_OPTION)
{
//client.closeAll();
setVisible(false);
dispose();
new ClientGUI();
}
else
{
System.exit(0);
}
}
else
{
boardPanel.removeTank(id);
}
}
else if(sentence.startsWith("Exit"))
{
int id=Integer.parseInt(sentence.substring(4));
if(id!=clientTank.getTankID())
{
boardPanel.removeTank(id);
}
}
}
try {
reader.close();
clientSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}