-
Notifications
You must be signed in to change notification settings - Fork 4
/
MainMenu.java
204 lines (132 loc) · 4.4 KB
/
MainMenu.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
194
195
196
197
198
199
200
201
202
203
204
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.*;
public class MainMenu extends JFrame implements ActionListener{
//MainMenu
//Allows for user to play, ask for help, or exit the game
MyButton start, help, exit; //Buttons for user interaction
MyButton[] buttons; //Arroy of all buttons
JLabel bg; //Background image
public MainMenu(){
//Basic setup of screen
//Positions and sizes are set for all buttons and images
super("Brick Breaker");
setLayout(null);
setSize(800,600);
start = new MyButton("start");
help = new MyButton("help");
exit = new MyButton("exit");
start.setSize(170,65);
help.setSize(380,65);
exit.setSize(120,65);
buttons = new MyButton[]{start, help, exit}; //Buttons are put in an Array for easy access and manipulation
for(int i=0; i<buttons.length; i++){
MyButton b = buttons[i];
b.setLocation(400-b.getWidth()/2, 300+i*75);
b.addActionListener(this);
add(b);
}
bg = new JLabel(new ImageIcon("bbg1.png"));
bg.setSize(800,600);
add(bg);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Program will exit on close
setVisible(true); //Makes it visible
}
public void actionPerformed(ActionEvent e){
//Checks for any actions
Object src = e.getSource(); //Gets source of ActionEvent
if(src == start){ //Starts game
new BB();
setVisible(false); //Stops menu
} else if(src == help){ //Loads help frame
new HelpFrame();
setVisible(false); //stops menu
} else if(src == exit){ //Exits program
System.exit(0);
}
}
public static void main(String[] args){
new MainMenu();
}
}
class MyButton extends JButton{
//Custom JButton class
//Gets rid of border and fill
//Puts in custom icon
public MyButton(String f){
ImageIcon p = new ImageIcon(f+"P.png"); //Custom icon
ImageIcon d = new ImageIcon(f+"D.png"); //Cutsom icon
this.setIcon(d); //Sets new icon
this.setRolloverIcon(p);
this.setPressedIcon(d);
this.setContentAreaFilled(false); //Gets rid of default settings
this.setBorderPainted(false);
}
}
class HelpFrame extends JFrame implements ActionListener{
//Displays controls for the game
JLabel info; //An image that has the controls
MyButton exit; //Exit button
public HelpFrame(){
//Setsup basic position and sizes of all components in the frame
super();
setLayout(null);
setSize(800,600);
exit = new MyButton("exit");
exit.setSize(120,65);
exit.setLocation(500, 400);
exit.addActionListener(this); //allows for be clicked and respond
add(exit);
info = new JLabel(new ImageIcon("info.png"));
info.setSize(800,600);
add(info);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Progran exits on close
setVisible(true); //Makes visible
}
public void actionPerformed(ActionEvent e){
//Displays screen until exited
Object src = e.getSource(); //gets source of action
if(src == exit){ //Returns to Main Menu
new MainMenu();
setVisible(false);
}
}
}
class LoseFrame extends JFrame implements ActionListener{
//Frame displayed when User loses all lives
JLabel msg, bg; //Two images
MyButton exit; //Exit button
public LoseFrame(){
//Sets up basic position and sizes of components in the frame
super();
setLayout(null);
setSize(800,600);
Font f = new Font("Bebas Neue", Font.PLAIN, 60); //Selects font for use
msg = new JLabel("You lost, you suck."); //An image that will be displayed
msg.setFont(f); //Set up
msg.setForeground(Color.WHITE);
msg.setSize(600,100);
msg.setLocation(225,250);
add(msg);
exit = new MyButton("exit"); //Exit button set up
exit.setSize(120,65);
exit.setLocation(340, 450);
exit.addActionListener(this); //Allows button to be clicked and respond
add(exit);
bg = new JLabel(new ImageIcon("bbg.png")); //Background image
bg.setSize(800,600);
add(bg);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Program exits on close
setVisible(true);
}
public void actionPerformed(ActionEvent e){
//Displays screen until exited
Object src = e.getSource(); //Finds source of action
if(src == exit){ //Returns to main menu
new MainMenu();
setVisible(false);
}
}
}