-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainMenu.java
More file actions
159 lines (137 loc) · 6.88 KB
/
Copy pathMainMenu.java
File metadata and controls
159 lines (137 loc) · 6.88 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
/*
* Authors: Jerry Li & Victor Jiang
* Date: June 13, 2025
* Description: This class represents the game main menu screen
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MainMenu extends JPanel {
// Constants for button dimensions.
public static final int BTN_H_SMALL = 100, BTN_W_SMALL = 300;
public static final int BTN_H_BIG = 140, BTN_W_BIG = 400;
// Reference to the parent GamePanel.
public final GamePanel PARENT;
// Font used for UI elements.
public final Font UI_FONT;
// Title label for the main menu.
public final JLabel TITLE_LABEL;
// Buttons for different menu options.
public GameButton how_button;
public GameButton debug_button;
public GameButton start_button;
// Background image for the main menu.
public BufferedImage background_image;
// Constructor initializes the main menu and its components.
public MainMenu(GamePanel parent) {
PARENT = parent; // Set the parent GamePanel reference.
setLayout(null); // Use absolute positioning for components.
setOpaque(false); // Make the panel transparent.
UI_FONT = parent.getGameFont(); // Retrieve the font from the parent.
// Initialize and configure the title label.
TITLE_LABEL = new JLabel("Endless Night");
TITLE_LABEL.setFont(UI_FONT.deriveFont(Font.BOLD, 90f)); // Set font size and style.
TITLE_LABEL.setForeground(Color.WHITE); // Set text color to white.
add(TITLE_LABEL); // Add the title label to the panel.
// Initialize and configure the "How to Play" button.
how_button = makeMenuButton("HOW 2 PLAY", 35f, e -> PARENT.game_state = GameState.HOW_TO_PLAY);
add(how_button); // Add the button to the panel.
// Initialize and configure the debug menu button (debug builds only).
debug_button = makeMenuButton("DEBUG MENU", 40f, e -> PARENT.game_state = GameState.DEBUG_MENU);
if (Settings.DEBUG_MODE) {
add(debug_button); // Only visible in debug mode
}
// Initialize and configure the start button.
start_button = makeMenuButton("START", 64f, e -> {
if (Settings.DEBUG_MODE) {
PARENT.setCurrentWave(DebugConfig.startWave);
PARENT.playScreen.applyDebugSettings();
}
PARENT.game_state = GameState.PLAYING; // Change game state to "PLAYING" when clicked.
});
start_button.setColor(new Color(76, 72, 144)); // Set custom color for the button.
add(start_button); // Add the button to the panel.
// Load the background image for the main menu.
loadBackground();
}
// Loads the background image from the resources folder.
public void loadBackground() {
InputStream in = null;
try {
in = getClass().getResourceAsStream("/assets/images/backgrounds/mainmenubg.jpg");
if (in != null) {
background_image = ImageIO.read(in);
} else {
background_image = null; // Set to null if the image cannot be loaded.
}
} catch (IOException e) {
background_image = null; // Set to null if the image cannot be loaded.
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
}
// Creates a menu button with the specified text, font size, and action
// listener.
public GameButton makeMenuButton(String text, float size, ActionListener action) {
GameButton b = new GameButton(text); // Create a new GameButton with the specified text.
b.setFont(UI_FONT.deriveFont(Font.BOLD, size)); // Set the font size and style.
b.addActionListener(action); // Add the action listener to the button.
b.addMouseListener(new MouseAdapter() {
// Increase font size when the mouse enters the button.
public void mouseEntered(MouseEvent e) {
b.setFont(UI_FONT.deriveFont(Font.BOLD, size * 1.15f));
}
// Reset font size when the mouse exits the button.
public void mouseExited(MouseEvent e) {
b.setFont(UI_FONT.deriveFont(Font.BOLD, size));
}
});
return b; // Return the configured button.
}
// Paints the background image and applies transparency.
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create(); // Create a copy of the Graphics object.
if (background_image != null) {
g2.setComposite(AlphaComposite.SrcOver.derive(0.5f)); // Set transparency to 50%.
g2.drawImage(background_image, 0, 0, getWidth(), getHeight(), null); // Draw the background image.
}
g2.dispose(); // Dispose of the Graphics2D object.
super.paintComponent(g); // Call the superclass method to paint other components.
}
// Repositions components when the panel is invalidated.
@Override
public void invalidate() {
super.invalidate(); // Call the superclass method.
positionComponents(); // Reposition the components.
}
// Positions the components on the panel based on its dimensions.
public void positionComponents() {
int w = getWidth() > 0 ? getWidth() : GamePanel.GAME_WIDTH; // Get the panel width or default to game width.
int h = getHeight() > 0 ? getHeight() : GamePanel.GAME_HEIGHT; // Get the panel height or default to game
// height.
int y_small; // Vertical position for small buttons.
int x_start; // Horizontal position for the start button.
int y_start; // Vertical position for the start button.
// Position the title label at the top-left corner.
TITLE_LABEL.setBounds(20, 10, TITLE_LABEL.getPreferredSize().width, TITLE_LABEL.getPreferredSize().height);
// Position the "How to Play" and debug buttons at the bottom.
y_small = h - BTN_H_SMALL - 60; // Calculate the vertical position for small buttons.
how_button.setBounds(60, y_small, BTN_W_SMALL, BTN_H_SMALL); // Position the "How to Play" button.
if (Settings.DEBUG_MODE) {
debug_button.setBounds(w - BTN_W_SMALL - 60, y_small, BTN_W_SMALL, BTN_H_SMALL); // Debug button
}
// Position the start button at the center-bottom of the panel.
x_start = (w - BTN_W_BIG) / 2; // Calculate the horizontal position for the start button.
y_start = y_small - (BTN_H_BIG - BTN_H_SMALL) / 2 - 20; // Adjust the vertical position for the start
// button.
start_button.setBounds(x_start, y_start, BTN_W_BIG, BTN_H_BIG); // Set the bounds for the start button.
}
}