-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultPanel.java
More file actions
207 lines (170 loc) · 6.73 KB
/
ResultPanel.java
File metadata and controls
207 lines (170 loc) · 6.73 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Victory screen with celebration animations.
*/
public class ResultPanel extends JPanel {
private BattleshipGUI parent;
private JLabel winnerLabel;
private JLabel trophyLabel;
private List<Confetti> confettiList;
private Timer animationTimer;
public ResultPanel(BattleshipGUI parent) {
this.parent = parent;
this.confettiList = new ArrayList<>();
setLayout(new BorderLayout());
setOpaque(false);
// Create content
JPanel contentPanel = createContentPanel();
add(contentPanel, BorderLayout.CENTER);
// Animation timer for confetti
animationTimer = new Timer(30, e -> {
for (Confetti c : confettiList) {
c.update();
}
repaint();
});
}
private JPanel createContentPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(false);
panel.setBorder(BorderFactory.createEmptyBorder(100, 50, 100, 50));
// Trophy
trophyLabel = new JLabel("GEWONNEN");
trophyLabel.setFont(BattleshipGUI.TITLE_FONT.deriveFont(64f));
trophyLabel.setForeground(BattleshipGUI.WARNING_COLOR);
trophyLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Winner text
JLabel congratsLabel = new JLabel("GRATULATION!");
congratsLabel.setFont(BattleshipGUI.TITLE_FONT.deriveFont(48f));
congratsLabel.setForeground(BattleshipGUI.SUCCESS_COLOR);
congratsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
winnerLabel = new JLabel("Spieler");
winnerLabel.setFont(BattleshipGUI.TITLE_FONT);
winnerLabel.setForeground(BattleshipGUI.TEXT_PRIMARY);
winnerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel subLabel = new JLabel("hat die Seeschlacht gewonnen!");
subLabel.setFont(BattleshipGUI.HEADER_FONT);
subLabel.setForeground(BattleshipGUI.TEXT_SECONDARY);
subLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
// Buttons
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
buttonPanel.setOpaque(false);
ModernButton newGameButton = new ModernButton("Neues Spiel", BattleshipGUI.SUCCESS_COLOR);
newGameButton.addActionListener(e -> parent.newGame());
ModernButton exitButton = new ModernButton("Beenden", BattleshipGUI.MISS_COLOR);
exitButton.addActionListener(e -> System.exit(0));
buttonPanel.add(newGameButton);
buttonPanel.add(exitButton);
panel.add(Box.createVerticalGlue());
panel.add(trophyLabel);
panel.add(Box.createRigidArea(new Dimension(0, 30)));
panel.add(congratsLabel);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(winnerLabel);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(subLabel);
panel.add(Box.createRigidArea(new Dimension(0, 60)));
panel.add(buttonPanel);
panel.add(Box.createVerticalGlue());
return panel;
}
public void showResult(String winner) {
winnerLabel.setText(winner);
// Generate confetti
confettiList.clear();
Random rand = new Random();
for (int i = 0; i < 100; i++) {
confettiList.add(new Confetti(rand));
}
animationTimer.start();
}
public void stopCelebration() {
animationTimer.stop();
confettiList.clear();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
// Background gradient
GradientPaint bgGradient = new GradientPaint(
0, 0, BattleshipGUI.OCEAN_DARK,
0, height, new Color(10, 25, 45)
);
g2d.setPaint(bgGradient);
g2d.fillRect(0, 0, width, height);
// Draw spotlight effect
RadialGradientPaint spotlight = new RadialGradientPaint(
width / 2f, height / 3f, height / 2f,
new float[]{0f, 1f},
new Color[]{new Color(255, 215, 0, 30), new Color(0, 0, 0, 0)}
);
g2d.setPaint(spotlight);
g2d.fillRect(0, 0, width, height);
// Draw confetti
for (Confetti c : confettiList) {
c.draw(g2d, width, height);
}
g2d.dispose();
}
// Inner class for confetti particles
private class Confetti {
float x, y, speedX, speedY;
float rotation, rotationSpeed;
int size;
Color color;
Random rand;
private static final Color[] COLORS = {
new Color(255, 107, 107), // Red
new Color(78, 205, 196), // Cyan
new Color(255, 230, 109), // Yellow
new Color(170, 111, 255), // Purple
new Color(107, 255, 148), // Green
new Color(255, 180, 107), // Orange
};
Confetti(Random rand) {
this.rand = rand;
reset(true);
}
void reset(boolean randomY) {
x = rand.nextFloat();
y = randomY ? rand.nextFloat() * 0.5f - 0.5f : -0.1f;
speedX = (rand.nextFloat() - 0.5f) * 0.005f;
speedY = 0.002f + rand.nextFloat() * 0.003f;
rotation = rand.nextFloat() * 360;
rotationSpeed = (rand.nextFloat() - 0.5f) * 10;
size = 8 + rand.nextInt(8);
color = COLORS[rand.nextInt(COLORS.length)];
}
void update() {
y += speedY;
x += speedX;
speedX += (rand.nextFloat() - 0.5f) * 0.001f;
rotation += rotationSpeed;
if (y > 1.1f) {
reset(false);
}
}
void draw(Graphics2D g2d, int width, int height) {
int drawX = (int)(x * width);
int drawY = (int)(y * height);
Graphics2D g = (Graphics2D) g2d.create();
g.translate(drawX, drawY);
g.rotate(Math.toRadians(rotation));
g.setColor(color);
g.fillRect(-size/2, -size/4, size, size/2);
g.dispose();
}
}
}