-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
205 lines (173 loc) · 7.7 KB
/
GameWindow.java
File metadata and controls
205 lines (173 loc) · 7.7 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
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
205
/**
* @(#)GameWindow.java
*
*
* @author
* @version 1.00 2017/2/19
*/
//Swing package GUI
import javax.swing.JPanel;
//AWT package GUI details
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
//AWT Listener Events
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
* Implementation of the container that organized the components
* of our Lights Out game by extending the javax.swing.JPanel class.
* <p>
* A JPanel is a lightweight container with easy to learn implementation
* details such as layouts and painting. When you get the hang of the
* Graphics class, you can basically use a panel to custom draw any aspect
* of your GUI windows and easily break the program graphics into different
* grid junks. Although, as you become even better, implementing a JComponent
* itself would be better but we will address that later.
* <p>
* Again, the important thing to wrap your head around is because we are
* inheriting from the JPanel class, this game window is now a JPanel itself
* and can do all things a JPanel container can do without us having to know
* how those things are implemented.
*
*/
public class GameWindow extends JPanel {
/** Set width of game window to same as the Frame width */
public static final int WIDTH = LightsOut.WIDTH;
/** Set height of game window to same as the Frame height */
public static final int HEIGHT = LightsOut.HEIGHT;
/** Background color of game window */
public static final Color BACKGROUND = new Color(97, 97, 95); /*** dark greyCHOOSE COLOR FOR BACKGROUND OF WINDOW ***/
/** LightGrid object for game which contains all of the Lights */
private LightGrid grid;
/** ControlPanel object for game which contains stats and reset button */
private ControlPanel controlPanel;
/** GameListener from JFrame to be passed throughout the different components */
private MouseListener gameListener;
/**
* Creates a new GameWindow responsible for set up of the different
* components of the game with the specified MouseListener object that
* all of the components actions are handled through.
*
* @param listener MouseListener that was implemented in the JFrame
* window. We will pass this listener object down throughout
* the different components of the program so the single
* listener object will communicate with all different parts.
*
*/
public GameWindow(MouseListener listener)
{
/********************************************************************
* Set up the listener, correctly set the size, set the background *
* color and also set the opacity of the JPanel to true (meaning *
* its not transparent), nullify the layout of this JPanel, and *
* invoke the method responsible for creating the components of *
* the GameWindow. *
********************************************************************/
this.gameListener = listener;
this.setSize(WIDTH, HEIGHT);
this.setBackground(BACKGROUND);
this.setOpaque(true);
this.setLayout(null);
this.initializeLightGrid();
this.initializeControlPanel();
}
/**
* Responsible for implementing the 2D grid of Light objects. This method
* will only be invoked by the constructor of this class, thus we have set
* the accessibility to private.
* <p>
* Although you may ask why we don't do this in the constructor, it is
* a useful organizational technique to separate any multiple line
* implementations into other methods. Therefore, if we wanted to create
* a more complex version of the Lights Out game we could add more details
* to this method. Or, if there were multiple constructors for the class then
* we would only need to write the implementation once here and each
* constructor invokes this method themselves.
*
*/
private void initializeLightGrid()
{
/************************************************************************
* Instantiate a LightGrid, set the bounds of the light grid with the *
* starting coordinates set to 1% of the width and 14% of the height *
* of this GameWindow, and finally be sure to add the grid to the *
* GameWindow. *
************************************************************************/
this.grid = new LightGrid(this.gameListener);
this.grid.setBounds((int)(WIDTH *.01), (int)(HEIGHT * .14), (WIDTH -(int)(WIDTH *.01)-300) , (HEIGHT - (int)(HEIGHT * .14) -50));
this.add(this.grid);
}
/**
* Responsible for implementing the panel that contains the stats
* for the game as well as the button that resets the game. Again,
* for the same reasons as listed in the above method we have
* separated this task from the constructor.
*
*/
private void initializeControlPanel()
{
/****************************************************************
* Instantiate a ControlPanel, set the bounds of the control *
* panel with the starting coordinates set to 50 more than *
* the width of the LightGrid and the same y-coordinate as *
* the LightGrid so they line up nicely, set the text of *
* the light counter label in the control panel to the *
* current number of lights that are on, and finally don't *
* forget to add the control panel to the GameWindow. *
****************************************************************/
this.controlPanel = new ControlPanel(this.gameListener);
this.controlPanel.setBounds((int)(WIDTH *.01)+ 550, (int) (HEIGHT * .14), WIDTH, HEIGHT);
String numLights = "" + grid.getNumberOfLightsOn();
this.controlPanel.getLightCounter().setText(numLights);
this.add(this.controlPanel);
}
/**
* Used to pass left click event from main window down to
* the light grid. Thus the sole purpose here is to act like
* the middle man, transferring the event from the top level
* class down to one or more of its parts.
*
* @param e MouseEvent that was passed from the JFrame's
* listener class.
*
*/
public void onLeftClick(MouseEvent e)
{
/************************************************************
* Correctly pass the MouseEvent that occurred to the grid *
* for processing, then update the control panel's light *
* counter label to the new number of lights that are on, *
* and update the click counter label since a click on the *
* LightGrid was just determined to have occurred. *
************************************************************/
grid.onLeftClick(e);
String numLights = "" + grid.getNumberOfLightsOn();
this.controlPanel.getLightCounter().setText(numLights);
int clicks = Integer.parseInt(this.controlPanel.getClickCounter().getText());
clicks += 1;
String numClicks = "" + clicks;
this.controlPanel.getClickCounter().setText(numClicks);
}
/**
* Responsible for resetting the different components of the Lights Out
* game when the reset button has been clicked. This method should be
* invoked by the JFrame's event handling class when the reset button
* was clicked.
*
*/
public void reset()
{
/************************************************************
* Reset the grid, reset the control panel, then update the *
* control panel's light counter to be the new number of *
* lights that are on in the grid. *
************************************************************/
this.grid.reset();
this.controlPanel.reset();
String numLights = "" + grid.getNumberOfLightsOn();
this.controlPanel.getLightCounter().setText(numLights);
}
}