-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinScreen.java
More file actions
167 lines (142 loc) · 4.96 KB
/
Copy pathWinScreen.java
File metadata and controls
167 lines (142 loc) · 4.96 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class renders a win screen when all waves are cleared
*/
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class WinScreen extends JPanel {
public final GamePanel PARENT;
public final Font UI_FONT;
public BufferedImage backgroundImage;
public JLabel messageLabel;
public WinScreen(GamePanel parent) {
PARENT = parent;
setLayout(null);
setOpaque(false);
UI_FONT = parent.getGameFont();
loadBackgroundImage();
setupComponents();
}
public void loadBackgroundImage() {
try {
backgroundImage = ImageIO.read(new File("./assets/images/backgrounds/mainmenubg.jpg"));
} catch (IOException e) {
System.err.println("Error loading win screen background image: " + e.getMessage());
backgroundImage = null;
}
}
public void setupComponents() {
JLabel titleLabel;
GameButton playButton;
GameButton menuButton;
// Victory title
titleLabel = new JLabel("YOU WIN!");
titleLabel.setForeground(new Color(0, 220, 0));
titleLabel.setFont(UI_FONT.deriveFont(Font.BOLD, 72f));
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(titleLabel);
// Message display
messageLabel = new JLabel();
messageLabel.setForeground(Color.WHITE);
messageLabel.setFont(UI_FONT.deriveFont(Font.BOLD, 32f));
messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(messageLabel);
// Play again button
playButton = makeMenuButton("PLAY AGAIN", 36f, e -> {
PARENT.resetGame();
if (Settings.DEBUG_MODE) {
PARENT.setCurrentWave(DebugConfig.startWave);
PARENT.playScreen.applyDebugSettings();
}
PARENT.showScreen("PLAYING");
});
add(playButton);
// Main menu button
menuButton = makeMenuButton("MAIN MENU", 36f, e -> {
PARENT.showScreen("MAIN_MENU");
});
add(menuButton);
addComponentListener(new java.awt.event.ComponentAdapter() {
@Override
public void componentResized(java.awt.event.ComponentEvent e) {
positionComponents();
}
});
}
public GameButton makeMenuButton(String text, float size, ActionListener action) {
GameButton b;
b = new GameButton(text);
b.setFont(UI_FONT.deriveFont(Font.BOLD, size));
b.addActionListener(action);
b.setColor(new Color(76, 72, 144));
b.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
b.setFont(UI_FONT.deriveFont(Font.BOLD, size * 1.15f));
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {
b.setFont(UI_FONT.deriveFont(Font.BOLD, size));
}
});
return b;
}
public void positionComponents() {
int w;
int h;
Component[] components;
int btnWidth = 0;
int btnHeight = 0;
w = getWidth() > 0 ? getWidth() : GamePanel.GAME_WIDTH;
h = getHeight() > 0 ? getHeight() : GamePanel.GAME_HEIGHT;
components = getComponents();
if (components.length >= 4) {
components[0].setBounds(0, h/5, w, 80); // Title
components[1].setBounds(0, h/5 + 100, w, 40); // Message
btnWidth = 300;
btnHeight = 80;
components[2].setBounds(w/2 - btnWidth/2, h/5 + 180, btnWidth, btnHeight);
components[3].setBounds(w/2 - btnWidth/2, h/5 + 280, btnWidth, btnHeight);
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d;
g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
if (backgroundImage != null) {
g2d.setComposite(AlphaComposite.SrcOver.derive(0.3f));
g2d.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), null);
}
g2d.dispose();
super.paintComponent(g);
}
@Override
public void invalidate() {
super.invalidate();
positionComponents();
}
@Override
public void addNotify() {
super.addNotify();
updateWaveDisplay();
}
public void updateWaveDisplay() {
int total;
String plural;
total = 0;
plural = null;
if (messageLabel != null) {
total = PARENT.waves != null ? PARENT.waves.size() : 0;
plural = total == 1 ? " wave" : " waves";
messageLabel.setText("You survived " + total + plural + "!");
}
}
}