-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.java
More file actions
359 lines (304 loc) · 8.36 KB
/
MainMenu.java
File metadata and controls
359 lines (304 loc) · 8.36 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// MainMenu.java
// Wendy Xu
// Displays main menu of game
// Player can start game from here (adventure or survival),
// change settings via options, view help or quit the game.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.applet.*;
import javax.sound.sampled.AudioSystem;
class MainMenu extends JFrame implements ActionListener{
MenuPanel menupanel;
HelpMenu helpmenu;
AlmanacMenu almanac;
HighScore highscore;
AudioClip menumusic,pickingmusic;
boolean music,sound;
PvZ play;
PickPlants pick;
javax.swing.Timer myTimer;
public MainMenu(PvZ play){
super();
this.play = play;
setLayout(null);
menupanel = new MenuPanel(this);
menupanel.setSize(818,647);
menupanel.setLocation(0,0);
add(menupanel);
menumusic = Applet.newAudioClip(getClass().getResource("MP3/Music/MainMenu.wav"));
pickingmusic = Applet.newAudioClip(getClass().getResource("MP3/Music/ChoosingPlants.wav"));
music = true;
sound = true;
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(818,647);
myTimer = new javax.swing.Timer(100,this);
myTimer.start();
}
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
menupanel.draw();
if(pick != null){
if(pick.getStart()){
pickingmusic.stop();
}
}
}
// goes to play adventure mode
public void adventure(){
stopMusic();
if(getMusic()){
pickingmusic.loop();
}
pick = new PickPlants(play);
play.getPanel().setMode("levels");
play.getPanel().getMap().setMode("levels");
pick.setVisible(true);
setVisible(false);
}
public void setMenu(){
setVisible(true);
}
public void HighScore(){
highscore = new HighScore(this);
highscore.setVisible(true);
setVisible(false);
}
public MenuPanel getMenuPanel(){
return menupanel;
}
// goes to play survival mode
public void survival(){
stopMusic();
if(getMusic()){
pickingmusic.loop();
}
pick = new PickPlants(play);
play.getPanel().setMode("survival");
play.getPanel().getMap().setMode("survival");
pick.setVisible(true);
setVisible(false);
}
// goes to the help menu
public void help(){
helpmenu = new HelpMenu(this);
setVisible(false);
}
// goes to the almanac
public void almanac(){
stopMusic();
System.out.println("meow");
if(getMusic()){
play.getPanel().setInAlmanac(true);
pickingmusic.loop();
}
almanac = new AlmanacMenu(this,"menu",play);
setVisible(false);
}
// quits the game
public void exit(){
setVisible(false);
dispose();
System.exit(0);
}
public boolean getMusic(){
return menupanel.getMusic();
}
public void setMusic(boolean music){
menupanel.setMusic(music);
this.music = music;
}
public boolean getSound(){
return menupanel.getSound();
}
public void setSound(boolean sound){
menupanel.setSound(sound);
this.sound = sound;
}
public void playMusic(){
menumusic.loop();
}
public void stopMusic(){
menumusic.stop();
}
public void stopAlmanacMusic(){
pickingmusic.stop();
}
public void startAlmanacMusic(){
pickingmusic.loop();
}
public static void main(String[] args){
MainMenu menu = new MainMenu(null);
}
}
class MenuPanel extends JPanel implements MouseListener, MouseMotionListener{
MainMenu menu;
Image menuPic; // menu image
boolean clicked,options,mplaying, ingame;
; Image almanacHover = new ImageIcon("Images/Menus/AlmanacHover.png").getImage();
Image adventureHover = new ImageIcon("Images/Menus/StartAdventureHover.png").getImage();
Image survivalHover = new ImageIcon("Images/Menus/SurvivalHover.png").getImage();
Image optionsHover = new ImageIcon("Images/Menus/OptionsHover.png").getImage();
Image helpHover = new ImageIcon("Images/Menus/HelpHover.png").getImage();
Image quitHover = new ImageIcon("Images/Menus/QuitHover.png").getImage();
Image optionsPic,optionsSound,optionsMusic,optionsOK; // options images
int[] pos;
boolean sound = true;
boolean music = true;
public void mouseClicked(MouseEvent evt){
clicked = true;
}
public void mouseExited(MouseEvent evt){}
public void mouseEntered(MouseEvent evt){}
public void mouseReleased(MouseEvent evt){
clicked = true;
}
public void mousePressed(MouseEvent evt){
clicked = false;
}
public void mouseMoved(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public void mouseDragged(MouseEvent evt){
pos = new int[]{evt.getX(),evt.getY()};
}
public MenuPanel(MainMenu menu){
super();
setFocusable(true);
grabFocus();
addMouseListener(this);
addMouseMotionListener(this);
this.menu = menu;
mplaying = false;
ingame = false;
menuPic = new ImageIcon("Images/Menus/mainmenu.png").getImage();
clicked = false;
pos = new int[]{0,0};
optionsPic = new ImageIcon("Images/Menus/OptionsMenu.png").getImage();
optionsSound = new ImageIcon("Images/Menus/OptionsMenu - Sound.png").getImage();
optionsMusic = new ImageIcon("Images/Menus/OptionsMenu - Music.png").getImage();
optionsOK = new ImageIcon("Images/Menus/OptionsMenu - OK.png").getImage();
}
public boolean getSound(){
return sound;
}
public void setSound(boolean sound){
this.sound = sound;
}
public boolean getMusic(){
return music;
}
public void setMusic(boolean music){
this.music = music;
}
public void setInGame(boolean ingame){
this.ingame = ingame;
}
public void draw(){
repaint();
if(music && !mplaying && !ingame){
menu.playMusic();
mplaying = true;
}
else if(!music){
menu.stopMusic();
mplaying = false;
}
}
public void paintComponent(Graphics g){
g.drawImage(menuPic,0,0,this);
Rectangle adventureRect = new Rectangle(415,100,320,100);
Rectangle survivalRect = new Rectangle(415,320,260,80);
Rectangle almanacRect = new Rectangle(390,440,110,120);
Rectangle optionsRect = new Rectangle(565,475,78,56);
Rectangle helpRect = new Rectangle(645,495,60,60);
Rectangle exitRect = new Rectangle(710,488,65,60);
Rectangle musicRect = new Rectangle(486,304,34,30);
Rectangle soundRect = new Rectangle(488,350,34,30);
Rectangle okRect = new Rectangle(262,456,310,60);
if(!options){
if(almanacRect.contains(pos[0],pos[1])){ // mouse is in almanac rect
g.drawImage(almanacHover,383,436,this); // draws hovering image
if(clicked == true){ // goes to almanac
menu.almanac();
}
}
if(adventureRect.contains(pos[0],pos[1])){ // mouse is in start adventure rect
g.drawImage(adventureHover,407,75,this); // draws hovering image
if(clicked == true){ // starts adventure
menu.adventure();
ingame = true;
}
}
if(survivalRect.contains(pos[0],pos[1])){ // mouse is in survival rect
g.drawImage(survivalHover,413,300,this); // draws hovering image
if(clicked == true){ // starts survival
menu.survival();
ingame = true;
}
}
if(helpRect.contains(pos[0],pos[1])){ // mouse is in help rect
g.drawImage(helpHover,637,519,this); // draws hovering image
if(clicked == true){ // goes to help
menu.help();
}
}
if(exitRect.contains(pos[0],pos[1])){ // mouse is in exit rect
g.drawImage(quitHover,712,516,this); // draws hovering image
if(clicked == true){ // exits
menu.exit();
}
}
if(optionsRect.contains(pos[0],pos[1])){ // mouse is in options rect
g.drawImage(optionsHover,556,481,this); // draws hovering image
if(clicked == true){ // goes to options
options = true;
}
}
}
if(options){ // draws options images if options is on
g.drawImage(optionsPic,200,50,this);
if(!music){
g.drawImage(optionsPic,200,50,this);
if(clicked && musicRect.contains(pos[0],pos[1])){
music = true;
clicked = false;
}
}
if(!sound){
g.drawImage(optionsPic,200,50,this);
if(clicked && soundRect.contains(pos[0],pos[1])){
sound = true;
clicked = false;
}
}
if(music){
g.drawImage(optionsMusic,200,50,this);
if(clicked && musicRect.contains(pos[0],pos[1])){
music = false;
mplaying = false;
clicked = false;
}
}
if(sound){
g.drawImage(optionsSound,200,50,this);
if(clicked && soundRect.contains(pos[0],pos[1])){
sound = false;
clicked = false;
}
}
if(okRect.contains(pos[0],pos[1])){
g.drawImage(optionsOK,200,50,this);
if(clicked){
options = false;
clicked = false;
}
}
}
if(clicked){
clicked = false;
}
}
}