-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameDisplay.java
More file actions
231 lines (182 loc) · 5.63 KB
/
GameDisplay.java
File metadata and controls
231 lines (182 loc) · 5.63 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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import common.Location;
public class GameDisplay {
public GameDisplay(GameController controller, int numMachines) {
buttonStrings = new ArrayList<String>();
for(int i=0; i< numMachines; i++) {
buttonStrings.add("" + i);
}
correctMachines = new ArrayList<>(numMachines);
for(int i=0;i<numMachines;i++)
correctMachines.add(true);
theController = controller;
}
void drawRect(Location p, int w, int h) {
if (panelG != null)
panelG.drawRect(p.getX() + Width/2, Height/2 - p.getY(), w, h);
}
void drawCircle(Location c, int rad) {
if (panelG != null) {
//System.out.println("Circle at " + c + " radius " + rad);
panelG.drawOval(c.getX() -rad/2 + Width/2,
Height/2 - (c.getY() - rad/2) , rad, rad);
}
}
void drawText(Location p, String text) {
if (panelG != null)
panelG.drawString(text, p.getX() + Width/2, Height/2- p.getY());
}
void drawLine(Location start, Location end) {
if (panelG != null)
panelG.drawLine(start.getX(), start.getY(), end.getX(), end.getY());
}
public void init() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
setUpGUI();
}
});
}
public void clear() {
if (imagePanel != null) {
imagePanel.removeAll();
}
}
public void update() {
if (imagePanel != null) {
//System.out.println("Updating view");
imagePanel.repaint();
}
}
@SuppressWarnings("serial")
private void setUpGUI() {
System.out.println("Set up GUI");
JFrame frame = new JFrame("Game Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(Width + 200, Height + 10));
topPanel.setLayout(new FlowLayout());
Border bborder = BorderFactory.createLineBorder(Color.red);
frame.add(topPanel);
imagePanel = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
panelG = g;
//System.out.println("Repainting");
theController.updateView();
}
};
imagePanel.setPreferredSize(new Dimension(Width, Height));
imagePanel.setBorder(bborder);
topPanel.add(imagePanel);
JPanel sidePanel = new JPanel();
sidePanel.setLayout(new BoxLayout(sidePanel, BoxLayout.PAGE_AXIS));
sidePanel.setBorder(bborder);
topPanel.add(sidePanel);
JLabel l1 = new JLabel("Select Leader");
sidePanel.add(l1);
addLeaderButtons(sidePanel);
JLabel l2 = new JLabel("Select Faulty Machines");
sidePanel.add(l2);
addFaultyCheckBoxes(sidePanel);
JButton startButton = new JButton("Start Phase");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Start Phase");
new Thread() {
public void run() {
System.out.println("Leader: " + leaderId);
for(int i=0;i< correctMachines.size();i++) {
System.out.println(i + ": " + correctMachines.get(i));
}
theController.startPhase(leaderId, correctMachines);
}
}.start();
}
});
sidePanel.add(startButton);
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exit");
System.exit(0);
}
});
sidePanel.add(exitButton);
// Display the window.
frame.pack();
frame.setVisible(true);
}
private void addLeaderButtons(JPanel panel) {
bg = new ButtonGroup();
ActionListener watchAction =
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = e.getActionCommand();
leaderId = Integer.parseInt(id);
}
};
for(String bString: buttonStrings) {
JRadioButton b = new JRadioButton(bString);
b.setActionCommand(bString);
b.addActionListener(watchAction);
bg.add(b);
panel.add(b);
}
}
private void addFaultyCheckBoxes(JPanel panel) {
ItemListener watchAction =
new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
Object box = e.getItem();
int mID = checkboxes.indexOf(box);
if(e.getStateChange() == ItemEvent.DESELECTED) {
correctMachines.set(mID, true);
} else {
correctMachines.set(mID, false);
}
}
};
checkboxes = new ArrayList<>();
for(String bString: buttonStrings) {
JCheckBox b = new JCheckBox(bString, false);
b.setActionCommand(bString);
b.addItemListener(watchAction);
panel.add(b);
checkboxes.add(b);
}
}
private GameController theController;
private int leaderId;
private ArrayList<Boolean> correctMachines;
private JPanel topPanel, imagePanel;
private ButtonGroup bg;
private Graphics panelG;
private final int Height = 600;
private final int Width = 900;
private ArrayList<String> buttonStrings;
private ArrayList<JCheckBox> checkboxes;
}