- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenuGUI.java
More file actions
Latest commit
203 lines (169 loc) · 8.14 KB
/
Copy pathMainMenuGUI.java
File metadata and controls
203 lines (169 loc) · 8.14 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
//The gui in this file is by Jacob Mobin, The Background is by Lucas Chu
importjavax.imageio.ImageIO;
importjavax.sound.sampled.AudioInputStream;
importjavax.sound.sampled.AudioSystem;
importjavax.sound.sampled.Clip;
importjavax.sound.sampled.FloatControl;
importjavax.swing.*;
importjavax.swing.event.MouseInputAdapter;
importjava.awt.*;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.Map;
publicclassMainMenuGUI {
privatestaticfinalintFRAME_WIDTH = 1080;
privatestaticfinalintFRAME_HEIGHT = 720;
privatestaticfinalintFRAME_RATE = 60; // Frames per second (adjust as needed)
//which button is hovered over
privatestaticbooleanisOverlayTopSelected = false;
privatestaticbooleanisOverlayMiddleSelected = false;
privatestaticbooleanisOverlayBottomSelected = false;
privatestaticJPanelcanvasPanel;
privatestaticMap<String, BufferedImage> imageCache = newHashMap<>(); //optimising buffered images so compuyter dosent lag
privatestaticString[] musicFiles = {"Music/1.wav", "Music/2.wav", "Music/3.wav", "Music/4.wav", "Music/5.wav", "Music/6.wav"};
privatestaticintcurrentMusicIndex = 0;
privatestaticClipmusicClip;
publicstaticvoidmainMenu() {
// Create and set up the window
JFrameframe = newJFrame("Minecraft Block Repository");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setResizable(false);
// Create a JPanel to hold the canvas for drawing
canvasPanel = newJPanel() {
@Override
protectedvoidpaintComponent(Graphicsg) { //new paint component
super.paintComponent(g);
// Load background image
BufferedImagebackgroundImage = loadImage("MenuAssets\\TempBg.png"); //load bg
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
// Load overlay image based on selection
if (isOverlayTopSelected) {
BufferedImageoverlayImage = loadImage("MenuAssets\\ButtonsTopSelected.png");
if (overlayImage != null) {
g.drawImage(overlayImage, 0, 0, getWidth(), getHeight(), this);
}
} elseif (isOverlayMiddleSelected) {
BufferedImageoverlayImage = loadImage("MenuAssets\\ButtonsMiddleSelected.png");
if (overlayImage != null) {
g.drawImage(overlayImage, 0, 0, getWidth(), getHeight(), this);
}
} elseif (isOverlayBottomSelected) {
BufferedImageoverlayImage = loadImage("MenuAssets\\ButtonsBottomSelected.png");
if (overlayImage != null) {
g.drawImage(overlayImage, 0, 0, getWidth(), getHeight(), this);
}
} else {
BufferedImageoverlayImage = loadImage("MenuAssets\\ButtonsNoneSelected.png");
if (overlayImage != null) {
g.drawImage(overlayImage, 0, 0, getWidth(), getHeight(), this);
}
}
}
};
// Set the preferred size of the canvas panel
canvasPanel.setPreferredSize(newDimension(FRAME_WIDTH, FRAME_HEIGHT));
canvasPanel.addMouseListener(newMouseInputAdapter() {
@Override
publicvoidmouseClicked(java.awt.event.MouseEvente) {
handleClickEvent(e.getX(), e.getY());
}
});
// Add mouse listener to canvas panel to track mouse movements
canvasPanel.addMouseMotionListener(newMouseInputAdapter() {
@Override
publicvoidmouseMoved(java.awt.event.MouseEvente) {
updateOverlaySelection(e.getX(), e.getY());
}
});
// Add the canvas panel to the frame
frame.getContentPane().add(canvasPanel);
// Display the window
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Set up a timer to update and repaint the canvas at a fixed interval
Timertimer = newTimer(1000 / FRAME_RATE, e -> {
canvasPanel.repaint(); // Repaint the canvas every frame
});
timer.start(); // Start the timer
// Start playing music in a loop
newThread(MainMenuGUI::playMusicLoop).start();
}
privatestaticvoidhandleClickEvent(intx, inty) { //handle which button is clicked on mouseevent
if (isOverlayTopSelected = isWithinButtonRange(x, y, 665, 909, 244, 310)) {
SwingUtilities.invokeLater(BlockListGUI::blockListGUI);
} elseif (isOverlayMiddleSelected = isWithinButtonRange(x, y, 665, 909, 329, 393)) {
SwingUtilities.invokeLater(BlockEditGUI::blockEditGUI);
} elseif (isOverlayBottomSelected = isWithinButtonRange(x, y, 665, 909, 409, 476)) {
System.exit(0);
}
}
privatestaticvoidupdateOverlaySelection(intmouseX, intmouseY) { //update which button is hovered over
isOverlayTopSelected = isWithinButtonRange(mouseX, mouseY, 665, 909, 244, 310);
isOverlayMiddleSelected = isWithinButtonRange(mouseX, mouseY, 665, 909, 329, 393);
isOverlayBottomSelected = isWithinButtonRange(mouseX, mouseY, 665, 909, 409, 476);
}
privatestaticbooleanisWithinButtonRange(intx, inty, intminX, intmaxX, intminY, intmaxY) { // is within a range
return (x >= minX && x <= maxX && y >= minY && y <= maxY);
}
privatestaticBufferedImageloadImage(Stringfilename) { //optimised buffered image reader
if (imageCache.containsKey(filename)) {
returnimageCache.get(filename);
} else {
try {
BufferedImageimage = ImageIO.read(newFile(filename));
imageCache.put(filename, image);
returnimage;
} catch (IOExceptione) {
e.printStackTrace();
returnnull;
}
}
}
publicstaticvoidplayMusicLoop() {
while (true) {
try {
// Load and play the current music file
AudioInputStreamaudioStream = AudioSystem.getAudioInputStream(newFile(musicFiles[currentMusicIndex]));
musicClip = AudioSystem.getClip();
musicClip.open(audioStream);
musicClip.start();
// Wait for the clip to finish playing
Thread.sleep(musicClip.getMicrosecondLength() / 1000);
// Set volume to 25%
FloatControlvolumeControl = (FloatControl) musicClip.getControl(FloatControl.Type.MASTER_GAIN);
volumeControl.setValue(-12.0f); // Reduce volume by 12 decibels
// Close the clip
musicClip.close();
// Move to the next music file (loop back to the first file if at the end)
currentMusicIndex = (currentMusicIndex + 1) % musicFiles.length;
} catch (Exceptione) {
e.printStackTrace();
System.out.println("Runtime error when adding audio file " + musicFiles[currentMusicIndex]);
}
}
}
publicClipplaySFX(Stringsong, floatvolume) {
Clipclip = null;
try {
AudioInputStreamaudioStream = AudioSystem.getAudioInputStream(this.getClass().getResource("/res/" + song));
clip = AudioSystem.getClip();
clip.open(audioStream);
FloatControlsetVolume = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
setVolume.setValue(volume); // Reduces the volume by the input value
clip.start();
} catch (Exceptione) {
e.printStackTrace();
System.out.println("Runtime error when adding audio file " + song);
}
returnclip;
}
}