-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameDisplay.java
More file actions
253 lines (209 loc) · 7.66 KB
/
Copy pathgameDisplay.java
File metadata and controls
253 lines (209 loc) · 7.66 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
public class gameDisplay extends JPanel implements MouseListener,
MouseMotionListener, ActionListener, ChangeListener {
//Variables
private int cellSize;
private Image image;
private JFrame frame;
private int numRows;
private int numCols;
private int[] mouseLoc;
private JSlider slider;
private int numPlayers;
private int mode;
private JButton[] buttons;
private JLabel clock;
public gameDisplay(String title, int numRows, int numCols, String topic) {
// Set Instance Variables
this.numRows = numRows;
this.numCols = numCols;
// Determine Cell Size
cellSize = Math.max(3, 600 / Math.max(numRows, numCols));
image = new BufferedImage(numCols * cellSize, numRows * cellSize, BufferedImage.TYPE_INT_RGB);
// Create Frame
frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));
// Display topic
JPanel topicPanel = new JPanel();
JLabel topicLabel = new JLabel("Topic: " + topic);
topicPanel.add(topicLabel);
frame.add(topicPanel, BorderLayout.PAGE_START);
topicLabel.setFont(new Font("Verdana", Font.BOLD, 16));
// Add Clock TODO
JPanel clockPanel = new JPanel();
clock = new JLabel("Time: " + 0.0);
clock.setFont(new Font("Verdana", Font.BOLD, 16));
clockPanel.add(clock);
frame.add(clockPanel, BorderLayout.PAGE_START);
// Instructions
JPanel p = new JPanel();
JPanel p2 = new JPanel();
p.add(new JLabel("Instructions: Players will take turns drawing on the same computer to create a picture"));
p2.add(new JLabel("of the topic provided at the top. Select a difficulty and number of players to start."));
frame.add(p, BorderLayout.CENTER);
frame.add(p2, BorderLayout.CENTER);
// Layout Panel
JPanel topPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.LINE_AXIS));
frame.getContentPane().add(topPanel);
// Set size and mouse trackers
setPreferredSize(new Dimension(numCols * cellSize, numRows * cellSize));
addMouseListener(this);
addMouseMotionListener(this);
topPanel.add(this);
// Create bottom panel for colors, time, # of players
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
topPanel.add(bottomPanel, BorderLayout.CENTER);
// Adding Slider for Amount of Players
slider = new JSlider(JSlider.HORIZONTAL, 2, 5, 3);
slider.addChangeListener(this);
slider.setMajorTickSpacing(1);
slider.setPaintTicks(true);
Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
for (int i = 2; i < 6; i++) {
String x = "" + i;
labelTable.put(new Integer(i), new JLabel(x));
}
slider.setLabelTable(labelTable);
slider.setPaintLabels(true);
frame.getContentPane().add(slider);
JPanel l = new JPanel();
l.add(new JLabel("Select Number of Players"));
frame.add(l, BorderLayout.CENTER);
// Create Buttons
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.PAGE_AXIS));
buttonPanel.add(new JLabel("Mode"));
topPanel.add(buttonPanel);
buttons = new JButton[3];
buttons[0] = new JButton("Easy");
buttons[0].setActionCommand("45");
buttons[0].addActionListener(this);
buttonPanel.add(buttons[0]);
buttons[1] = new JButton("Medium");
buttons[1].setActionCommand("30");
buttons[1].addActionListener(this);
buttonPanel.add(buttons[1]);
buttons[2] = new JButton("Hard");
buttons[2].setActionCommand("20");
buttons[2].addActionListener(this);
buttonPanel.add(buttons[2]);
// Finish Creating the New Frame
frame.pack();
frame.setVisible(true);
}
private int[] toLocation(MouseEvent e)
{
int row = e.getY() / cellSize;
int col = e.getX() / cellSize;
if (row < 0 || row >= numRows || col < 0 || col >= numCols)
return null;
int[] loc = new int[2];
loc[0] = row;
loc[1] = col;
return loc;
}
public void setColor(int row, int col, Color color)
{
Graphics g = image.getGraphics();
g.setColor(color);
g.fillRect(col * cellSize, row * cellSize, cellSize, cellSize);
}
public void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, null);
}
@Override
public void actionPerformed(ActionEvent e) {
mode = Integer.parseInt(e.getActionCommand());
for (JButton button : buttons)
button.setSelected(false);
((JButton)e.getSource()).setSelected(true);
}
public int getMode() {
return mode;
}
@Override
public void mouseClicked(MouseEvent e) {
mouseLoc = toLocation(e);
}
@Override
public void mousePressed(MouseEvent e) {
mouseLoc = toLocation(e);
}
@Override
public void mouseReleased(MouseEvent e) {
mouseLoc = null;
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mouseDragged(MouseEvent e) {
mouseLoc = toLocation(e);
}
@Override
public void mouseMoved(MouseEvent e) {}
public void pause(int milliseconds)
{
try {
Thread.sleep(milliseconds);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
public int[] getMouseLocation() {
return mouseLoc;
}
@Override
public void stateChanged(ChangeEvent e) {
numPlayers = slider.getValue();
}
//returns number of times to step between repainting and processing mouse input
public int getNumPlayers() {
return numPlayers;
}
public void updateClock(double timeElapsed) {
clock.setText("Time: " + timeElapsed);
}
public void displayMessage(String message) throws InterruptedException {
// Display message
JPanel panelBlue = new JPanel();
panelBlue.setBackground(new Color(195, 235, 237));
panelBlue.setLayout(new BorderLayout()); // Layout for the panel to center the label
// Create a label with the player change message
JLabel label = new JLabel(message, SwingConstants.CENTER);
label.setFont(new Font("Verdana", Font.BOLD, 20));
// Add the label to the panel
panelBlue.add(label, BorderLayout.CENTER);
// Add the panel at the top of the frame (using BorderLayout.NORTH)
frame.add(panelBlue, BorderLayout.NORTH);
// Revalidate and repaint the frame
frame.revalidate();
frame.repaint();
// Show the frame
frame.setVisible(true);
// Show the panel for 5 seconds
Thread.sleep(5000);
frame.remove(panelBlue);
// Revalidate and repaint to update the frame's UI
frame.revalidate();
frame.repaint();
}
public void changePlayers(int newPlayer) throws InterruptedException {
newPlayer++;
displayMessage("Change to Player " + newPlayer);
}
public void gameOver() throws InterruptedException {
displayMessage("GAME OVER");
}
}