Repository navigation
Expand file tree
/
Copy pathWinApp.java
More file actions
154 lines (154 loc) · 4.64 KB
/
Copy pathWinApp.java
File metadata and controls
154 lines (154 loc) · 4.64 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
package view;
import java.awt.*;
import java.util.Arrays;
import javax.swing.*;
import model.Choice;
import controller.Round;
public class WinApp {
private boolean isExtra = false;
private String playerName;
private Round round;
private int t;
private Timer timer;
private final JFrame win = new JFrame();
private final Container ctx = win.getContentPane();
private final JPanel
headerPane = new JPanel(),
footerPane = new JPanel(),
masterPane = new JPanel();
private void initialize() {
win.setTitle("Rock Paper Scissors Advance");
win.setResizable(false);
win.setSize(835, 482);
win.setLocationRelativeTo(null);
win.setIconImage(Toolkit.getDefaultToolkit().getImage("img/favicon.png"));
win.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
ctx.setLayout(new BorderLayout());
ctx.add(headerPane, BorderLayout.NORTH);
ctx.add(masterPane, BorderLayout.CENTER);
ctx.add(footerPane, BorderLayout.SOUTH);
WelcomeCtx();
win.setVisible(true);
}
private void erase(int w, int h) {
win.setSize(w, h);
headerPane.removeAll();
headerPane.revalidate();
headerPane.repaint();
masterPane.removeAll();
masterPane.revalidate();
masterPane.repaint();
footerPane.removeAll();
footerPane.revalidate();
footerPane.repaint();
}
private void WelcomeCtx() {
erase(300, 130);
JLabel title = new JLabel("Insert your name");
headerPane.add(title);
JTextField choicePlayer = new JTextField();
// choicePlayer.setPreferredSize(new Dimension(280, 30));
masterPane.setLayout(new GridLayout(1, 1));
masterPane.add(choicePlayer);
JButton start = new JButton("Enter");
JComboBox<String> modeCombo = new JComboBox<>(new String[] {"Regular", "Extra"});
start.addActionListener(e -> {
playerName = !choicePlayer.getText().isBlank()
? choicePlayer.getText().strip()
: "player";
System.out.println(playerName);
isExtra = modeCombo.getSelectedItem().equals("Extra");
startPlayCtx();
});
footerPane.setLayout(new GridLayout(1, 2));
footerPane.add(start);
footerPane.add(modeCombo);
}
private void startPlayCtx() {
erase(400, 300);
win.setTitle("Player: %s".formatted(playerName));
t = 10;
JLabel countPane = new JLabel("%d".formatted(t));
timer = new Timer(1000, e -> {
if (t > 1)
countPane.setText("%d".formatted(--t));
else {
round = new Round(Choice.rand(isExtra), Choice.rand(isExtra));
resultPlayCtx(round);
}
});
headerPane.add(new JLabel("Human vs Robot"));
footerPane.add(new JLabel("%s".formatted(round != null ? round : "prima partita")));
JPanel choicePane = new JPanel(new GridLayout(2, 3));
masterPane.setLayout(new BorderLayout());
Arrays.stream(Choice.values())
.limit(isExtra ? Choice.values().length : 3)
.forEach(c -> {
JButton btn = new JButton(
new ImageIcon(
new ImageIcon(
"img/%s.png".formatted(c.name().toLowerCase())
).getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)
)
);
btn.addActionListener(e -> {
if (timer.isRunning())
timer.stop();
round = new Round(c, Choice.rand(isExtra));
resultPlayCtx(round);
});
choicePane.add(btn);
});
final ImageIcon img = new ImageIcon("img/gear.png");
img.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
final JButton btn = new JButton(img);
btn.addActionListener(e -> {
round = new Round(Choice.rand(isExtra), Choice.rand(isExtra));
if (timer.isRunning())
timer.stop();
resultPlayCtx(round);
});
choicePane.add(btn);
masterPane.add(choicePane, BorderLayout.CENTER);
countPane.setPreferredSize(new Dimension(70, 50));
countPane.setFont(new Font("Arial", Font.BOLD, 62));
countPane.setHorizontalAlignment(SwingConstants.CENTER);
masterPane.add(countPane, BorderLayout.EAST);
timer.start();
}
private void resultPlayCtx(Round round) {
erase(400, 200);
if (timer.isRunning())
timer.stop();
System.out.println(round);
headerPane.add(new JLabel("%s you have %s".formatted(
playerName,
round.status().toString().toLowerCase()))
);
masterPane.setLayout(new GridLayout(1, 5));
for (final String figure : new String[]{
"human",
round.getHuman().toString().toLowerCase(),
"vs",
round.getRobot().toString().toLowerCase(),
"robot"
})
masterPane.add(new JLabel(new ImageIcon("img/%s.png".formatted(figure))));
JButton playAgain = new JButton("Play Again");
playAgain.addActionListener(e -> startPlayCtx());
footerPane.setLayout(new GridLayout(1, 1));
footerPane.add(playAgain);
}
public WinApp() {
initialize();
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
new WinApp();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}