-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlPanel.java
221 lines (181 loc) · 7.61 KB
/
ControlPanel.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* @(#)ControlPanel.java
*
*
* @author
* @version 1.00 2017/2/19
*/
//Swing package GUI components
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
//AWT package GUI details
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
//AWT action events and listener
import java.awt.event.MouseListener;
/**
* Implementation of the panel that is responsible for the button that
* resets the state of the Lights Out game back to the start and the
* tracking of the statistics (number of clicks and lights still left
* on) in the game.
* <p>
* Again, note that this object IS a type of JPanel.
*
*/
public class ControlPanel extends JPanel {
/** Size of control panel width to be 32% the width of the game window */
public static final int WIDTH = (int)(GameWindow.WIDTH * .32);
/** Height of control panel same as LightGrid */
public static final int HEIGHT = LightGrid.SIZE;
/**
* Font for control panel (My implementation was same as the
* GameWindow, but at 40pt size).
*/
public static final Font FONT = new Font("Helvetica", Font.PLAIN, 40);/*** CHOOSE THE FONT YOU WOULD LIKE ***/
/**
* Text color (My implementation was same as the Light's off color)
*/
public static final Color TEXT_COLOR = new Color(1, 221, 218); /*** blue CHOOSE THE FONT COLOR YOU WANT (1, 221, 218***/
/**
* Color for reset button
*/
public static final Color RESET_COLOR = new Color(1, 221, 218);/*** bright purple CHOOSE COLOR OF RESET BUTTON 242, 43, 235***/
/**
* Color for text of reset button
*/
public static final Color RESET_TEXT_COLOR = new Color(0, 0, 0);/*** black CHOOSE COLOR OF TEST ON RESET BUTTON ***/
/**
* First Color for the border of the reset button
*/
public static final Color BORDER1 = new Color(146, 149, 148); /*** grey CHOOSE 1ST OF TWO COLORS FOR BORDER OF RESET BUTTON ***/
/**
* Second Color for the border of the reset button
*/
public static final Color BORDER2 = new Color(148, 147, 145);/*** orange CHOOSE 2ND OF TWO COLORS FOR BORDER OF RESET BUTTON ***/
/**
* Labels for each of the different parts of the control panel.
* Note, the part that actually says "Clicks" and the number that
* is the total number of clicks are separate JLabels. This makes
* things much easier to layout and adjust.
*/
private JLabel clickLabel, clickCounter, lightLabel, lightCounter, statusLabel;
/** Button that when clicked resets the game */
private JButton resetButton;
/** Reference to the MouseListener that came from the top level JFrame */
private MouseListener gameListener;
/**
* Creates a new ControlPanel which is responsible for implementing
* all of the necessary labels for displaying the total number of
* clicks and lights that remain on, as well as holding the reset
* button and the label which displays the status of the game. If the
* game is currently in progress, the bottom status label should read
* some generous message like "Good Luck!", once the player switches
* off all of the lights this label should be responsible for displaying
* a winning message.
*
* @param listener MouseListener from the top level JFrame window
*
*/
public ControlPanel(MouseListener listener)
{
/****************************************************************
* Correctly assign the specified listener to gameListener. *
* Set the size of the ControlPanel, set the opacity to false *
* since we do not want an outline of the control panel to *
* show up, and finally remove the layout so the set bounds *
* applied below will take effect. *
* *
* IMPORTANT IDEA: *
* The MouseListener is assigned to the reset button at *
* the bottom of this constructor. Note, how simple it *
* actually is for different components of a GUI to actually *
* communicate simply by passing the same object references *
* around. *
****************************************************************/
gameListener = listener;
this.setSize(WIDTH, HEIGHT);
this.setOpaque(false);
this.setLayout(null);
/********************************************************
* THE INSTANTIATION OF EACH OF THE COMPONENTS OF THE *
* CONTROL PANEL HAVE BEEN SET FOR YOU. DO NOT ADJUST *
* ANYTHING BELOW THIS COMMENT IN THE CONSTRUCTOR *
********************************************************/
//Create a label for total number of clicks
this.clickLabel = new JLabel("Clicks:");
this.clickLabel.setFont(this.FONT);
this.clickLabel.setForeground(this.TEXT_COLOR);
//Add click label to this panel
this.clickLabel.setBounds((int)(this.WIDTH * .01), (int)(this.HEIGHT * .01), (int)(this.WIDTH * .5), (int)(this.HEIGHT * .2));
this.add(this.clickLabel);
//Create counter label
this.clickCounter = new JLabel("0");
this.clickCounter.setFont(this.FONT);
this.clickCounter.setForeground(this.TEXT_COLOR);
//Add counter label to this panel
this.clickCounter.setBounds((int)(this.WIDTH * .7), (int)(this.HEIGHT * .01), (int)(this.WIDTH * .45), (int)(this.HEIGHT * .2));
this.add(this.clickCounter);
//Create lights label
this.lightLabel = new JLabel("Lights:");
this.lightLabel.setFont(this.FONT);
this.lightLabel.setForeground(this.TEXT_COLOR);
//Add light label to this panel
this.lightLabel.setBounds((int)(this.WIDTH * .01), (int)(this.HEIGHT * .24), (int)(this.WIDTH * .5), (int)(this.HEIGHT * .2));
this.add(this.lightLabel);
//Create lights counter
this.lightCounter = new JLabel("0");
this.lightCounter.setFont(this.FONT);
this.lightCounter.setForeground(this.TEXT_COLOR);
//Add light counter label to this panel
this.lightCounter.setBounds((int)(this.WIDTH * .7), (int)(this.HEIGHT * .24), (int)(this.WIDTH * .45), (int)(this.HEIGHT * .2));
this.add(this.lightCounter);
//Create reset button
this.resetButton = new JButton("Reset");
this.resetButton.setFont(this.FONT);
this.resetButton.setPreferredSize(new Dimension((int)(this.WIDTH * .6), (int)(this.HEIGHT * .1) ));
this.resetButton.setForeground(this.RESET_TEXT_COLOR);
this.resetButton.setBackground(this.RESET_COLOR);
this.resetButton.setBorder( new BevelBorder(BevelBorder.RAISED, this.BORDER1, this.BORDER2) );
this.resetButton.setFocusable(false);
//Add game listener to this button
this.resetButton.addMouseListener(this.gameListener);
//Add reset button
this.resetButton.setBounds((int)(this.WIDTH * .2), (int)(this.HEIGHT * .55), (int)(this.WIDTH * .6), (int)(this.HEIGHT * .1));
this.add(this.resetButton);
//Create status label
this.statusLabel = new JLabel("Good Luck!");
this.statusLabel.setFont(this.FONT);
this.statusLabel.setForeground(this.TEXT_COLOR);
//Add status label
this.statusLabel.setBounds((int)(this.WIDTH * .1), (int)(this.HEIGHT * .7), (int)(this.WIDTH * .9), (int)(this.HEIGHT * .2));
this.add(this.statusLabel);
}
/**
* Reset control panel when reset button clicked
*/
public void reset()
{
/****************************************
* Set the label counters back to zero *
****************************************/
this.clickCounter.setText("0");
this.lightCounter.setText("0");
}
/**
* Return light counter label
*/
public JLabel getLightCounter()
{
return this.lightCounter;
}
/**
* Return click counter label
*/
public JLabel getClickCounter()
{
return this.clickCounter;
}
}