-
Notifications
You must be signed in to change notification settings - Fork 0
/
TankChooser.java
160 lines (144 loc) · 5.67 KB
/
TankChooser.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
package game;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Objects;
public class TankChooser extends JFrame {
private File finalImage = new File("src/game/IconsInGame/Farshid/Tank/tank_blue.png");
private File finalBullet = new File("src/game/IconsInGame/Farshid/Bullet/bulletBlue1.png");
private JLabel label = new JLabel();
private JLabel hours = new JLabel();
private JLabel results = new JLabel();
private String username;
private JButton send;
private Setting setting;
TankChooser(String username, Setting setting) {
send = new JButton();
this.username = username;
this.setting = setting;
}
public void run() {
initButtons();
int response = JOptionPane.showConfirmDialog(this, "Do you want to customize your tank ?");
if (response == JOptionPane.YES_OPTION) {
JFrame ask = new JFrame("Choose tank model");
JPanel c = new MainPanel(new ImageIcon("src/game/IconsInGame/Farshid/background.png").getImage());
JLabel logo = new JLabel(new ImageIcon("src/game/IconsInGame/Farshid/Logo.png"));
setDefaultCloseOperation(EXIT_ON_CLOSE);
c.add(logo);
logo.setLocation(300, 0);
logo.setSize(700, 150);
ask.setIconImage(new ImageIcon("src/game/IconsInGame/Icon.png").getImage());
// User name information
creatingUserInfo(c, label, "User name : " + username, 300);
creatingUserInfo(c, hours, "Hours playing : " + 0, 350);
creatingUserInfo(c, results, "Total Wins : " + 0 + " Loses : " + 0, 400);
// Getting the tanks images
c.setLayout(null);
ask.setExtendedState(Frame.MAXIMIZED_BOTH);
ask.setResizable(false);
// Creating buttons
CreatingButtons(ask, c);
ask.add(c);
ask.setAlwaysOnTop(true);
ask.setLocationRelativeTo(null);
ask.setVisible(true);
} else {
send.doClick();
}
}
private void CreatingButtons(JFrame ask, JPanel c) {
boolean[] flag = {false, false};
addTanks(ask, c, flag);
addBullet(ask, c, flag);
}
private void creatingUserInfo(JPanel c, JLabel label, String text, int y) {
label.setLocation(600, y);
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 20));
label.setSize(250, 25);
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setText(text);
label.setBackground(Color.BLACK);
label.setForeground(Color.WHITE);
label.setOpaque(true);
c.add(label);
}
private void addTanks(JFrame ask, JPanel c, boolean[] flag) {
File file = new File("src/game/IconsInGame/Farshid/Tank");
int tmp = 0;
for (String name : Objects.requireNonNull(file.list())) {
JButton button = new JButton(new ImageIcon(file.getPath() + File.separator + name));
button.setLocation(160 * (tmp % 2), 100 + 110 * (tmp++ / 2));
button.setSize(150, 100);
c.add(button);
button.addActionListener(e -> {
finalImage = new File(file.getPath() + File.separator + name);
flag[0] = true;
if (flag[1]) {
ask.setVisible(false);
send.doClick();
}
});
}
}
private void addBullet(JFrame ask, JPanel c, boolean[] flag) {
File file = new File("src/game/IconsInGame/Farshid/Bullet");
int tmp = 0;// Reading bullet files and adding them to customizer frame
for (String name : Objects.requireNonNull(file.list())) {
JButton button = new JButton(new ImageIcon(file.getPath() + File.separator + name));
button.setLocation(1250 - 40 * (tmp / 5), 100 + 55 * (tmp++ % 5));
button.setSize(30, 45);
c.add(button);
button.addActionListener(e -> {
finalBullet = new File(file.getPath() + File.separator + name);
flag[1] = true;
if (flag[0]) {
ask.setVisible(false);
send.doClick();
}
});
}
}
private void initButtons() {
send.addActionListener(e -> new SwingWorker<>() {
@Override
protected Object doInBackground() {
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 1725);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
assert socket != null;
PrintStream printStream = new PrintStream(socket.getOutputStream());
printStream.println(finalImage.getPath());
printStream.println(finalBullet.getPath());
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void done() {
setVisible(false);
JoinGame joinGame = new JoinGame(setting);
joinGame.run();
}
}.execute());
}
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);
}
}
}