-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.java
306 lines (286 loc) · 10.9 KB
/
Log.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
package game;
import game.Server.Pair;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.*;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
/**
* This is the login page where the user inputs its username
* and its password to connect to the server.
*/
public class Log {
// The private fields
private JFrame frame = new JFrame("J Tank Trouble");
private JTextField userName = new JTextField("User Name");
private JPasswordField passwordField = new JPasswordField();
private JCheckBox remember = new JCheckBox("Remember me");
private JCheckBox showPassword = new JCheckBox("Show Password");
private JLabel userLabel = new JLabel(new ImageIcon("src/game/IconsInGame/Farshid/name_48px.png"));
private JLabel passwordLabel = new JLabel(new ImageIcon("src/game/IconsInGame/Farshid/key_100px.png"));
private JButton logIn = new JButton("Log in"), signUp = new JButton("Sign Up");
private JLabel logo = new JLabel(new ImageIcon("src/game/IconsInGame/Farshid/Logo.png"));
/**
* This method will build the login frame and will
* display it to the user.
*/
public void run() {
AudioMaker.startTheTheme();
frame.setIconImage(new ImageIcon("src/game/IconsInGame/Icon.png").getImage());
frame.setPreferredSize(new Dimension(750, 500));
frame.setLocation(390, 130);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
init();
// Creating the login page
Runnable r = () -> {
JPanel c = null;
try {
c = new MainPanel(ImageIO.read(new File("src/game/IconsInGame/Farshid/background.png")));
} catch (IOException e) {
e.printStackTrace();
}
assert c != null;
c.setLayout(null);
setLocations();
addStuff(c);
frame.add(c);
frame.pack();
frame.setVisible(true);
};
SwingUtilities.invokeLater(r);
// logIn.doClick(); //for remember.
}
/**
* this method will add the stuff that we made to the panel c
* @param c the panel that we wanna add our stuff
*/
private void addStuff(JPanel c) {
c.add(logo);
c.add(userLabel);
c.add(userName);
c.add(passwordLabel);
c.add(passwordField);
c.add(remember);
c.add(showPassword);
c.add(logIn);
c.add(signUp);
}
/**
* the name tells everything about it.
*/
private void setLocations() {
logo.setLocation(125, 0);
userLabel.setLocation(250, 125);
userName.setLocation(330, 155);
passwordLabel.setLocation(250, 175);
passwordField.setLocation(330, 205);
remember.setLocation(275, 260);
showPassword.setLocation(275, 300);
logIn.setLocation(275, 350);
signUp.setLocation(365, 350);
}
/**
* This method will set the styles to
* the components.
*/
private void init() {
logo.setSize(500, 150);
logIn.setSize(80, 30);
signUp.setSize(80, 30);
userLabel.setSize(90, 90);
userName.setSize(140, 25);
passwordLabel.setSize(90, 90);
passwordField.setSize(140, 25);
remember.setSize(160, 20);//105
showPassword.setSize(160, 20);//113
showPassword.setFont(new Font("Dialog", Font.BOLD, 15));
remember.setFont(new Font("Dialog", Font.BOLD, 15));
passwordField.setEchoChar('*');
remember.setContentAreaFilled(false);
showPassword.setContentAreaFilled(false);
remember.setForeground(Color.BLACK);
showPassword.setForeground(Color.BLACK);
userName.setBorder(null);
passwordField.setBorder(null);
passwordField.setText("Password");
logIn.setBackground(Color.BLACK);
logIn.setForeground(Color.white);
signUp.setBackground(Color.BLACK);
signUp.setForeground(Color.white);
UIManager.put("Button.select", Color.DARK_GRAY);
showPassword.addActionListener(e -> {
if (showPassword.isSelected())
passwordField.setEchoChar((char) 0);
else
passwordField.setEchoChar('*');
});
userName.addFocusListener(new FocusAdapter() {
/**
* Invoked when a component gains the keyboard focus.
*
* @param e even that happened.
*/
@Override
public void focusGained(FocusEvent e) {
if (userName.getText().equals("User Name"))
userName.setText("");
}
@Override
public void focusLost(FocusEvent e) {
if (userName.getText().equals(""))
userName.setText("User Name");
}
});
passwordField.addFocusListener(new FocusAdapter() {
/**
* Invoked when a component gains the keyboard focus.
*
* @param e even that happened.(Focus Event)
*/
@Override
public void focusGained(FocusEvent e) {
if (Arrays.equals(passwordField.getPassword(), "Password".toCharArray()))
passwordField.setText("");
}
@Override
public void focusLost(FocusEvent e) {
if (passwordField.getPassword().length == 0)
passwordField.setText("Password");
}
});
initButtons();
}
private void initButtons() {
logIn.addActionListener(e -> new SwingWorker<>() {
Setting setting = new Setting();
/**
* Computes a result, or throws an exception if unable to do so.
*
* <p>
* Note that this method is executed only once.
*
* <p>
* Note: this method is executed in a background thread.
*
* @return the computed result
*/
@Override
protected Object doInBackground() {
String ip = "127.0.0.1";//you can change it later.
int port = 1726;//you can change the port later too.
String ret = null;
try {
Socket socket = new Socket(ip, port);
ret = takeString(socket, "Log in");
if (ret.equalsIgnoreCase("user entered the game."))
setting.setConnectionSocket(socket);
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
return ret;
}
@Override
protected void done() {
try {
String ret = get().toString();
if (ret.equalsIgnoreCase("user entered the game.")) {
// Done with the login
frame.dispose();
// Enter into the game setting
TankChooser tankChooser = new TankChooser(userName.getText(), setting);
tankChooser.run();
return;
}
if (!userName.getText().equalsIgnoreCase("User Name"))
JOptionPane.showMessageDialog(null, get().toString());
} catch (InterruptedException | ExecutionException | NullPointerException ex) {
JOptionPane.showMessageDialog(null, "Your attempt to connect to our servers was failed.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}.execute());
signUp.addActionListener(e -> new SwingWorker<>() {
/**
* Computes a result, or throws an exception if unable to do so.
*
* <p>
* Note that this method is executed only once.
*
* <p>
* Note: this method is executed in a background thread.
*
* @return the computed result
*/
@Override
protected Object doInBackground() {
String ip = "127.0.0.1";//you can change it later .
int port = 1726;//you can change the port later too.
String ret = null;
try (Socket socket = new Socket(ip, port)) {
ret = takeString(socket, "Sign up");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return ret;
}
@Override
protected void done() {
try {
JOptionPane.showMessageDialog(null, get().toString());
} catch (InterruptedException | ExecutionException | NullPointerException ex) {
JOptionPane.showMessageDialog(null, "Your attempt to connect to our servers was failed.", "Error", JOptionPane.PLAIN_MESSAGE);
}
}
}.execute());
}
private String takeString(Socket socket, String s) throws IOException {
Scanner in = new Scanner(socket.getInputStream());
PrintStream out = new PrintStream(socket.getOutputStream());
String name = userName.getText();
String pass = String.valueOf(passwordField.getPassword());
var pair = readUserPass();
if (!pair.getFirst().equals("*") && !pair.getSecond().equals("*")) {
name = pair.getFirst();
pass = pair.getSecond();
}
out.println(s);
out.println(name);
out.println(pass);
String response = in.nextLine();
modifyRemember(name, pass, response);
return response;
}
private void modifyRemember(String name, String pass, String response) throws FileNotFoundException {
if (remember.isSelected() && response.equalsIgnoreCase("user entered the game.")) {
PrintWriter writer = new PrintWriter("src/game/UserInfo/userToRemember.txt");
writer.println(name);
writer.println(pass);
}
}
private Pair<String, String> readUserPass() {
Scanner scanner = new Scanner("src/game/UserInfo/userToRemember.txt");
String s1 = "*", s2 = "*";
if (scanner.hasNextLine()) {
s1 = scanner.nextLine();
}
if (scanner.hasNextLine())
s2 = scanner.nextLine();
return new Pair<>(s1, s2);
}
private static class MainPanel extends JPanel {
private Image bg;
MainPanel(Image bg) {
this.bg = bg;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
}
}
}