blob: 62fb754ae993a8d92f0f80df640886b1f6dd6a12 (
plain) (
blame)
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
|
package ui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import models.CONSTS;
import models.ComboBoxUser;
import models.User;
import models.UserListWrapper;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.XMLDecoder;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.awt.BorderLayout;
public class MainFrame extends JFrame implements Runnable {
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
public ByteBuffer readBuffer=ByteBuffer.allocate(1024);
public SocketChannel client;
public LoginPanel loginPanel=null;
public ChooseOpponentPanel chooseOpponentPanel=null;
GamePanel gamePanel=null;
public long userId=-1;
public boolean run=true;
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 727, 429);
loginPanel=new LoginPanel();
chooseOpponentPanel=new ChooseOpponentPanel();
gamePanel=new GamePanel();
getContentPane().setLayout(new BorderLayout(0, 0));
add(loginPanel,BorderLayout.CENTER);
setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
try {
run=false;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
try {
client=SocketChannel.open(new InetSocketAddress(CONSTS.socketPort));
} catch (IOException e) {
e.printStackTrace();
}
new Thread(this).start();
}
@Override
public void run() {
try{
while(run) {
StringBuilder sb=new StringBuilder();
client.configureBlocking(true);
readBuffer.clear();
while(run && client.read(readBuffer)>0) {
readBuffer.flip();
byte[] bytes = new byte[readBuffer.limit()];
readBuffer.get(bytes);
sb.append(new String(bytes));
readBuffer.clear();
client.configureBlocking(false);
}
if(!run)
{
client.close();
System.exit(EXIT_ON_CLOSE);
}
String response[]=sb.toString().split(":");
if(response[0].trim().equals("ACCEPTED")) {
this.setUserId(Long.parseLong(response[1].trim()));
this.getContentPane().removeAll();
this.getContentPane().add(chooseOpponentPanel,BorderLayout.CENTER);
SwingUtilities.updateComponentTreeUI(this);
chooseOpponentPanel.getUsers();
}else if(response[0].trim().equals("BADLOGIN")){
System.out.println("Bad Login");
this.loginPanel.warningTextArea.setText("Pogresan Login");
}else {
XMLDecoder decoder = null;
decoder = new XMLDecoder(new ByteArrayInputStream(sb.toString().getBytes()));
UserListWrapper wp=(UserListWrapper) decoder.readObject();
decoder.close();
chooseOpponentPanel.comboBox.removeAllItems();
if(wp.getUsers()!=null)
for(User user :wp.getUsers()) {
if(userId!=user.id)
chooseOpponentPanel.comboBox.addItem(new ComboBoxUser(user));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(EXIT_ON_CLOSE);
}
public SocketChannel getClient() {
return client;
}
public void setClient(SocketChannel client) {
this.client = client;
}
public LoginPanel getLoginPanel() {
return loginPanel;
}
public void setLoginPanel(LoginPanel loginPanel) {
this.loginPanel = loginPanel;
}
public ChooseOpponentPanel getChooseOpponentPanel() {
return chooseOpponentPanel;
}
public void setChooseOpponentPanel(ChooseOpponentPanel chooseOpponentPanel) {
this.chooseOpponentPanel = chooseOpponentPanel;
}
public GamePanel getGamePanel() {
return gamePanel;
}
public void setGamePanel(GamePanel gamePanel) {
this.gamePanel = gamePanel;
}
public ByteBuffer getReadBuffer() {
return readBuffer;
}
public void setReadBuffer(ByteBuffer readBuffer) {
this.readBuffer = readBuffer;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
}
|