-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.java
executable file
·193 lines (167 loc) · 5.42 KB
/
Menu.java
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
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.swing.*;
public class Menu extends JPanel implements MouseListener {
BufferedImage Background; // Declaration and initialization
JLabel[] jb = new JLabel[5]; // For Menu buttons
ImageIcon Start, Exit, Scores, instructions, Starth, Exith, Scoresh, instructionsh; // Images for Menu
static JFrame fm;
int xp;
static Clip clip;
// Non static Block
{
//Adding music
try{
DataLine.Info info;
AudioInputStream stream= AudioSystem.getAudioInputStream(new File("resources/menu.wav"));
AudioFormat format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();
}catch (Exception e){}
// Getting screen size and width
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
//Giving error message if screen resolution is not our required resolution
if(width!=1366 || height!=768){
JOptionPane.showMessageDialog(null, "Your screen resolution is not 1366*768 which is required for optimal performance");
//System.exit(0);//exiting
}
// Getting center of screen to add menu relatively so that it appears on
// center in all devices
xp = (int) (width / 2);
// Reading Images from storage
try {
Background = ImageIO.read(new File("resources\\Gamef.jpg"));
Start = new ImageIcon("resources\\start.png");
Exit = new ImageIcon("resources\\exit.png");
Scores = new ImageIcon("resources\\score.png");
instructions = new ImageIcon("resources\\instructions.png");
Starth = new ImageIcon("resources\\starth.png");
Exith = new ImageIcon("resources\\exith.png");
Scoresh = new ImageIcon("resources\\scoreh.png");
instructionsh = new ImageIcon("resources\\instructionsh.png");
}// Catching Exception if found
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("Image not found");
}
// Setting Layout to null
setLayout(null);
// Adding JLAbels via for loop
int yb = 250;
for (int i = 0; i < 4; i++) {
jb[i] = new JLabel(); // Making JLabel
jb[i].addMouseListener(this); // Adding Mouse Listener
add(jb[i]); // Adding JLabel
jb[i].setBounds(xp - 100, yb, 200, 50);// Setting bounds
yb += 55;
}
// ASssigining image icons to labels
jb[0].setIcon(Start);
jb[1].setIcon(Scores);
jb[2].setIcon(instructions);
jb[3].setIcon(Exit);
}// Non-static block ends
// Paint Component method
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Drawing background image
g2d.drawImage(Background, 0, 0, getWidth(), getHeight(), null);
repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
// When mouse clciked
// If button is first button i-e start Game button
if (e.getComponent() == jb[0]) {
// Make GameModes Object
GameMode modeScreen=new GameMode();
//Stopping music
clip.stop();
// making menu invisible
this.setVisible(false);
// Adding GamoMode object
TrafficMania.frame.add(modeScreen);
}
//if button is High Scores button then showing high scores
if (e.getComponent() == jb[1]) {
fm=new JFrame();
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(new HighScorePanel());
fm.setUndecorated(true);
fm.setExtendedState(JFrame.MAXIMIZED_BOTH);
fm.setVisible(true);
}
//if button is Instructions button then showing instructions
if (e.getComponent() == jb[2]) {
jb[2].setIcon(instructionsh);
fm=new JFrame();
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(new Instructions());
fm.setUndecorated(true);
fm.setExtendedState(JFrame.MAXIMIZED_BOTH);
fm.setVisible(true);
}
// Exit button
if (e.getComponent() == jb[3]) {
jb[3].setIcon(Exith);
System.exit(0);
}
}
//changing images when mouse entered to provide hover effect
@Override
public void mouseEntered(MouseEvent e) {
// Changing images of buttons when mouse entered to make hover effect
if (e.getComponent() == jb[0]) {
jb[0].setIcon(Starth);
repaint();
}
if (e.getComponent() == jb[1]) {
jb[1].setIcon(Scoresh);
}
if (e.getComponent() == jb[2]) {
jb[2].setIcon(instructionsh);
}
if (e.getComponent() == jb[3]) {
jb[3].setIcon(Exith);
}
}
//restoring images when mouse exited to provide hover effect
@Override
public void mouseExited(MouseEvent e) {
// Restoring images of JLabels when mouse removed
if (e.getComponent() == jb[0]) {
jb[0].setIcon(Start);
repaint();
}
if (e.getComponent() == jb[1]) {
jb[1].setIcon(Scores);
}
if (e.getComponent() == jb[2]) {
jb[2].setIcon(instructions);
}
if (e.getComponent() == jb[3]) {
jb[3].setIcon(Exit);
}
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}