/
ZombieFrame.java
145 lines (127 loc) · 4.22 KB
/
ZombieFrame.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
/**
* ZombieHouse - a zombie survival game written in Java for CS 351.
*
* Team members:
* Ramon A. Lovato
* Danny Gomez
* James Green
* Marcos Lemus
* Mario LoPrinzi
*/
import java.awt.*;
import javax.swing.*;
/**
* ZombieFrame - ZombieHouse's GUI class.
*
* @author Ramon A. Lovato
* @group Danny Gomez
* @group James Green
* @group Marcos Lemus
* @group Mario LoPrinzi
*/
public class ZombieFrame extends JFrame {
/**
* Because Eclipse made me!
*/
private static final long serialVersionUID = 1L;
private Container pane;
private ZombieKeyBinds keys;
private GameGraphics graphics;
/**
* ZombieFrame's constructor.
*
* @param contents Optional JPanel(s) to add to content pane. Arguments > 0
* are ignored.
*/
public ZombieFrame(JPanel... contents) {
super("ZombieHouse");
// Request keyboard focus for the frame.
setFocusable(true);
requestFocusInWindow();
requestFocus();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setResizable(false);
setBackground(Color.BLACK);
// The content pane. An optional JPanel may be passed into the
// constructor. It creates an empty pane with a black background if
// one isn't provided.
pane = getContentPane();
pane.setBackground(Color.BLACK);
pane.setFocusable(false);
pane.setVisible(true);
if (contents.length > 0) {
pane.add(contents[0]);
}
keys = new ZombieKeyBinds((JComponent) pane);
// Get the graphics device information.
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphics = environment.getDefaultScreenDevice();
pack();
// Go full screen, if supported.
if (graphics.isFullScreenSupported()) {
try {
graphics.setFullScreenWindow(this);
// Having gone full screen, retrieve the display size.
// size = Toolkit.getDefaultToolkit().getScreenSize();
// This double-switching of setVisible is to fix a bug with
// full-screen-exclusive mode on OS X. Versions 10.8 and later
// don't send keyboard events properly without it.
if (System.getProperty("os.name").contains("OS X")) {
setVisible(false);
}
} catch (HeadlessException ex) {
System.err.println("Error: primary display not set or found. "
+ "Your experience of life may be suboptimal.");
ex.printStackTrace();
}
} else {
// If full-screen-exclusive mode isn't supported, switch to
// maximized window mode.
System.err.println("Full-screen-exclusive mode not supported.");
setExtendedState(Frame.MAXIMIZED_BOTH);
}
setVisible(true);
}
/**
* Start the graphics module.
*
* @param house House object to draw first.
* @param tileset Which tileset to use to draw the house.
*/
public void startGraphics(House house, int tileset) {
graphics = new GameGraphics(getWidth(), getHeight());
graphics.initHouse(house, tileset);
graphics.setOpaque(true);
replace(graphics);
}
/**
* Replace the current contents of the frame's content pane.
*
* @param panel JPanel of the new contents of the content pane.
*/
public void replace(JPanel panel) {
// Removes everything from the content pane.
for (int i = 0; i < pane.getComponentCount(); i++) {
pane.remove(i);
}
pane.add(panel);
}
/**
* Getter for graphics module.
*
* @return GameGraphics graphics module.
*/
public GameGraphics getGameGraphics() {
return graphics;
}
/**
* Getter for keys.
*
* @return keys ZombieKeyBinds object containing the key binds.
*/
public ZombieKeyBinds getKeyBinds() {
return keys;
}
}