-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameWindow.java
More file actions
135 lines (94 loc) · 3.08 KB
/
GameWindow.java
File metadata and controls
135 lines (94 loc) · 3.08 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
//Justin Baldeosingh
//816021226
//COMP 3609 - Assignment 2
import javax.swing.*; // need this for GUI objects
import java.awt.*; // need this for Layout Managers
import java.awt.event.*; // need this to respond to GUI events
public class GameWindow extends JFrame implements ActionListener, MouseListener {
//Declares interface buttons.
private JButton startB;
private JButton endB;
private JButton restartB;
private JButton exitB;
private Container c;
//Declares the different panels for the interface.
private JPanel mainPanel;
private GamePanel gamePanel;
private Scoreboard scoreboard;
@SuppressWarnings({"unchecked"})
public GameWindow() {
setTitle ("Missile Defense Command");
setSize (612, 575);
// Creates the main scoreboard.
scoreboard = new Scoreboard();
// create buttons
startB = new JButton ("Start Game");
endB = new JButton ("End Game");
restartB = new JButton ("Restart Game");
exitB = new JButton ("Exit");
// add listener to each button (same as the current object)
startB.addActionListener(this);
endB.addActionListener(this);
restartB.addActionListener(this);
exitB.addActionListener(this);
// create mainPanel
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
// create the gamePanel for game entities
gamePanel = new GamePanel(scoreboard);
gamePanel.setPreferredSize(new Dimension(612, 408));
// create buttonPanel
GridLayout gridLayout;
JPanel buttonPanel = new JPanel();
gridLayout = new GridLayout(2, 2);
buttonPanel.setLayout(gridLayout);
// add buttons to buttonPanel
buttonPanel.add (startB);
buttonPanel.add (endB);
buttonPanel.add (restartB);
buttonPanel.add (exitB);
// add sub-panels with GUI objects to mainPanel
mainPanel.add(scoreboard);
mainPanel.add(gamePanel);
mainPanel.add(buttonPanel);
mainPanel.setBackground(Color.BLACK);
// set up mainPanel to respond to the mouse
gamePanel.addMouseListener(this);
// add mainPanel to window surface
c = getContentPane();
c.add(mainPanel);
// set properties of window
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// implement single method in ActionListener interface
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals(startB.getText()))
gamePanel.startGame();
if (command.equals(endB.getText()))
gamePanel.endGame();
if (command.equals(restartB.getText()))
gamePanel.restartGame();
if (command.equals(exitB.getText()))
System.exit(0);
mainPanel.requestFocus();
}
// implement methods in MouseListener interface
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
//If the game has started, any click event on the panel should be used as game user input for firing the cannon.
if(gamePanel.gameStarted)
gamePanel.targetSky(x, y);
}
public void mouseReleased(MouseEvent e) {
}
}