-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMain.java
More file actions
60 lines (51 loc) · 1.67 KB
/
GameMain.java
File metadata and controls
60 lines (51 loc) · 1.67 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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class GameMain extends JFrame implements ActionListener {
// Creates JPanels
private PanelClass gamePanel;
private MenuClass menuPanel;
// Starts timer for action listener
Timer time = new Timer(5,this);
/** JFrame constructor */
public GameMain() {
// Sets up JFrame
super("Dodge the Ball");
setSize(800, 750);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// Creates menu JPanel
menuPanel = new MenuClass();
getContentPane().add(menuPanel);
time.start();
}
/** Checks if the play button is hit in the Menu class */
public void actionPerformed(ActionEvent e) {
if(menuPanel.playHit) {
menuPanel.setVisible(false);
gamePanel = new PanelClass();
gamePanel.setFocusable(true);
getContentPane().add(gamePanel);
gamePanel.requestFocusInWindow();
time.stop();
}
}
/** Method called by main, necessary for main method to run */
private void createAndDisplayGUI() throws InterruptedException {}
/** Referenced from http://stackoverflow.com/questions/2861933/best-way-to-implement-game-loop-without-freezing-ui-thread
* Main method, calls a nonstatic method createAndDisplayGUI */
public static void main(String[]args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new GameMain().createAndDisplayGUI();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
} );
}
}