-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameOverScreen.java
More file actions
163 lines (143 loc) · 5.48 KB
/
Copy pathGameOverScreen.java
File metadata and controls
163 lines (143 loc) · 5.48 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class renders a game over screen when a player dies
*/
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 GameOverScreen extends JPanel {
public final GamePanel PARENT;
public final Font UI_FONT;
public BufferedImage backgroundImage;
// Constructor
public GameOverScreen(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 game over background image: " + e.getMessage());
backgroundImage = null;
}
}
public JLabel scoreLabel;
public void setupComponents() {
// Game over title
JLabel titleLabel = new JLabel("GAME OVER");
titleLabel.setForeground(Color.RED);
titleLabel.setFont(UI_FONT.deriveFont(Font.BOLD, 72f));
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(titleLabel);
// Score display - placeholder text (will be updated when shown)
scoreLabel = new JLabel("You survived 0 waves!");
scoreLabel.setForeground(Color.WHITE);
scoreLabel.setFont(UI_FONT.deriveFont(Font.BOLD, 32f));
scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(scoreLabel); // Retry button
GameButton retryButton = makeMenuButton("TRY AGAIN", 36f, e -> {
PARENT.resetGame();
if (Settings.DEBUG_MODE) {
PARENT.setCurrentWave(DebugConfig.startWave);
PARENT.playScreen.applyDebugSettings();
}
PARENT.showScreen("PLAYING");
});
add(retryButton);
// Main menu button
GameButton menuButton = makeMenuButton("MAIN MENU", 36f, e -> {
PARENT.showScreen("MAIN_MENU");
});
add(menuButton);
// Position components based on panel size
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 = 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 = getWidth() > 0 ? getWidth() : GamePanel.GAME_WIDTH;
int h = getHeight() > 0 ? getHeight() : GamePanel.GAME_HEIGHT;
Component[] components = getComponents();
if (components.length >= 4) {
// Title
components[0].setBounds(0, h/5, w, 80);
// Score
components[1].setBounds(0, h/5 + 100, w, 40);
// Retry button
int btnWidth = 300;
int btnHeight = 80;
components[2].setBounds(w/2 - btnWidth/2, h/5 + 180, btnWidth, btnHeight);
// Main menu button
components[3].setBounds(w/2 - btnWidth/2, h/5 + 280, btnWidth, btnHeight);
}
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
// Draw dark background
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
// Draw background image with overlay
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();
}
/**
* Updates the wave count display when the screen becomes visible
*/
@Override
public void addNotify() {
super.addNotify();
// Update the wave display to show the correct wave count
updateWaveDisplay();
}
/**
* Updates the wave display with the current wave number
*/
public void updateWaveDisplay() {
if (scoreLabel != null) {
// Number of completed waves. If the player died mid-wave the
// current wave hasn't been finished yet, so subtract one.
int waveReached = Math.max(0, PARENT.getCurrentWave() - 1);
String plural = waveReached == 1 ? " wave" : " waves";
scoreLabel.setText("You survived " + waveReached + plural + "!");
}
}
}