-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFrame.java
More file actions
155 lines (133 loc) · 4.75 KB
/
Copy pathMyFrame.java
File metadata and controls
155 lines (133 loc) · 4.75 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class MyFrame extends JFrame implements ActionListener, ComponentListener {
LoginPanel loginPanel;
JMenuBar menuBar;
JMenuItem registerItem;
JMenuItem playGameItem;
JMenuItem highScoreItem;
JMenuItem quitItem;
JMenuItem aboutItem;
JMenu fileMenu;
JMenu helpMenu;
JButton startButton;
GamePanel gamePanel;
RegisterPanel register;
HighScorePanel highScore;
Font f = new Font(Font.DIALOG, Font.BOLD, 23);
MyFrame(){
loginPanel = new LoginPanel();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("CSE 212 Term Project - Space Invader Game");
this.setLayout(new FlowLayout());
this.add(loginPanel);
this.pack();
this.setLocationRelativeTo(null);
this.setContentPane(loginPanel);
startButton = new JButton("PUSH START");
startButton.setFocusable(true);
startButton.setBounds(250, 400, 200, 60);
startButton.setPreferredSize(new Dimension(100, 50));
startButton.setBorderPainted(false);
startButton.setContentAreaFilled(false);
startButton.setFocusPainted(false);
startButton.setOpaque(false);
startButton.setForeground(Color.DARK_GRAY);
startButton.setFont(f);
startButton.addActionListener(this);
this.getContentPane().add(startButton);
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
helpMenu = new JMenu("Help");
registerItem = new JMenuItem("Register");
playGameItem = new JMenuItem("Play Game");
highScoreItem = new JMenuItem("High Score");
quitItem = new JMenuItem("Quit");
aboutItem = new JMenuItem("About");
fileMenu.add(registerItem);
fileMenu.add(playGameItem);
fileMenu.add(highScoreItem);
helpMenu.add(aboutItem);
fileMenu.add(quitItem);
menuBar.add(fileMenu);
menuBar.add(helpMenu);
playGameItem.addActionListener(this);
quitItem.addActionListener(this);
registerItem.addActionListener(this);
highScoreItem.addActionListener(this);
this.setJMenuBar(menuBar);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==quitItem) {
int option = JOptionPane.showConfirmDialog(
this,
"Are you sure you want to exit?",
"Confirm Exit",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE
);
if (option == JOptionPane.YES_OPTION)
System.exit(0);
}
else if(e.getSource() == playGameItem || e.getSource() == startButton) {
loginPanel.setVisible(false);
register = new RegisterPanel(false);
register.addComponentListener(this);
this.setContentPane(register);
revalidate();
}
else if(e.getSource() == registerItem){
loginPanel.setVisible(false);
register = new RegisterPanel(true);
register.addComponentListener(this);
this.setContentPane(register);
revalidate();
}
else if(e.getSource() ==highScoreItem){
loginPanel.setVisible(false);
highScore = new HighScorePanel();
highScore.addComponentListener(this);
this.setContentPane(highScore);
revalidate();
}
}
@Override
public void componentResized(ComponentEvent e) {
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
if(!register.onlyRegister){
LevelStartPanel levelStartPanel = new LevelStartPanel();
this.setContentPane(levelStartPanel);
Timer timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
levelStartPanel.setVisible(false);
((Timer) e.getSource()).stop();
gamePanel = new GamePanel();
setContentPane(gamePanel);
revalidate();
gamePanel.requestFocusInWindow();
}
});
// Start the timer
timer.start();
}
if(register.onlyRegister){
loginPanel.setVisible(true);
this.setContentPane(loginPanel);
revalidate();
}
}
}