25 import java.awt.event.*;
26 import javax.swing.JOptionPane;
27 import javax.swing.Timer;
28 import java.util.HashMap;
40 private Button
reset, addPlayer, setSpeed, startstop;
49 public SimulatorGUI(
int gridX,
int gridY,
int skillz,
int bots_per_player,
int obstacles_) {
59 addWindowListener(
new WindowAdapter() {
60 public void windowClosing(WindowEvent we) {
65 GridBagLayout gbl_panel =
new GridBagLayout();
66 gbl_panel.columnWidths =
new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ;
67 gbl_panel.rowHeights =
new int[]{0, 0};
68 gbl_panel.columnWeights =
new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
69 gbl_panel.rowWeights =
new double[]{1.0, Double.MIN_VALUE};
73 playerList.setPreferredSize(
new Dimension(3, 8));;
74 GridBagConstraints gbc_list =
new GridBagConstraints();
75 gbc_list.fill = GridBagConstraints.BOTH;
76 gbc_list.insets =
new Insets(5,5,5,5);
77 gbc_list.gridwidth = 2;
78 gbc_list.gridheight = 5;
84 public void paint(Graphics g)
87 Rectangle r = getBounds();
88 int cell_length = r.width/
length;
89 int cell_height = r.height/
width;
92 for(
int i=0; i<=
length; i++)
93 g.drawLine(0,cell_height*i,cell_length*
length,cell_height*i);
94 for(
int i=0; i<=
width; i++)
95 g.drawLine(cell_length*i,0,cell_length*i,cell_height*
width);
102 HashMap<String,Color> colorMap =
new HashMap<String,Color>();
103 Color current_color = Color.BLACK;
107 if(current_color.equals(Color.BLACK))
108 current_color = Color.RED;
109 else if(current_color.equals(Color.RED))
110 current_color = Color.PINK;
111 else if(current_color.equals(Color.PINK))
112 current_color = Color.ORANGE;
113 else if(current_color.equals(Color.ORANGE))
114 current_color = Color.YELLOW;
115 else if(current_color.equals(Color.YELLOW))
116 current_color = Color.GREEN;
117 else if(current_color.equals(Color.GREEN))
118 current_color = Color.MAGENTA;
119 else if(current_color.equals(Color.MAGENTA))
120 current_color = Color.CYAN;
121 else if(current_color.equals(Color.CYAN))
122 current_color = Color.BLUE;
123 else if(current_color.equals(Color.BLUE))
124 current_color = Color.LIGHT_GRAY;
125 else if(current_color.equals(Color.LIGHT_GRAY))
126 current_color = Color.GRAY;
127 else if(current_color.equals(Color.GRAY))
128 current_color = Color.DARK_GRAY;
129 else if(current_color.equals(Color.DARK_GRAY))
130 current_color = Color.PINK;
133 colorMap.put(x,current_color);
138 for(
int i=0; i<
length; i++)
139 for(
int j=0; j<
width; j++)
140 switch(world[i][j].contents)
144 g.setColor(Color.BLACK);
145 g.drawLine(i*cell_length,j*cell_height,(i+1)*cell_length,(j+1)*cell_height);
146 g.drawLine(i*cell_length,(j+1)*cell_height,(i+1)*cell_length,j*cell_height);
150 g.setColor(colorMap.get(current_sim.getOccupantPlayer(world[i][j])));
151 g.fillRect(i*cell_length,j*cell_height,cell_length,cell_height);
154 g.setColor(Color.BLACK);
155 g.fillRect(i*cell_length,j*cell_height,cell_length,cell_height);
158 g.setColor(Color.BLACK);
159 switch(world[i][j].fort_orientation)
162 g.drawLine((int)((i+0.5)*cell_length),(j+1)*cell_height,(int)((i+0.5)*cell_length),j*cell_height);
163 g.drawLine((int)((i+0.5)*cell_length),j*cell_height,i*cell_length,(j+1)*cell_height);
164 g.drawLine((int)((i+0.5)*cell_length),j*cell_height,(i+1)*cell_length,(j+1)*cell_height);
167 g.drawLine((int)((i+0.5)*cell_length),(j+1)*cell_height,(int)((i+0.5)*cell_length),j*cell_height);
168 g.drawLine((int)((i+0.5)*cell_length),(j+1)*cell_height,i*cell_length,j*cell_height);
169 g.drawLine((int)((i+0.5)*cell_length),(j+1)*cell_height,(i+1)*cell_length,j*cell_height);
172 g.drawLine(i*cell_length,(int)((j+0.5)*cell_height),(i+1)*cell_length,(int)((j+0.5)*cell_height));
173 g.drawLine((i+1)*cell_length,(
int)((j+0.5)*cell_height),i*cell_length,j*cell_height);
174 g.drawLine((i+1)*cell_length,(
int)((j+0.5)*cell_height),i*cell_length,(j+1)*cell_height);
177 g.drawLine(i*cell_length,(int)((j+0.5)*cell_height),(i+1)*cell_length,(int)((j+0.5)*cell_height));
178 g.drawLine(i*cell_length,(int)((j+0.5)*cell_height),(i+1)*cell_length,j*cell_height);
179 g.drawLine(i*cell_length,(int)((j+0.5)*cell_height),(i+1)*cell_length,(j+1)*cell_height);
184 g.setColor(Color.BLACK);
185 g.drawOval(i*cell_length,j*cell_height,cell_length,cell_height);
191 canvas.setBounds(0, 0, 50, 50);
192 canvas.setBackground(Color.WHITE);
193 GridBagConstraints gbc_canvas =
new GridBagConstraints();
194 gbc_canvas.fill = GridBagConstraints.BOTH;
195 gbc_canvas.gridwidth = 9;
196 gbc_canvas.gridheight = 7;
197 gbc_canvas.gridx = 3;
198 gbc_canvas.gridy = 0;
201 speed =
new TextField();
202 GridBagConstraints gbc_speed =
new GridBagConstraints();
203 gbc_speed.insets =
new Insets(0,0,0,5);
204 gbc_speed.fill = GridBagConstraints.BOTH;
205 gbc_speed.gridwidth = 1;
206 gbc_speed.gridheight = 1;
209 add(
speed,gbc_speed);
212 addPlayerField.setBounds(0,0,50,50);
213 addPlayerField.setPreferredSize(
new Dimension(20,20));
214 GridBagConstraints gbc_field =
new GridBagConstraints();
215 gbc_field.fill = GridBagConstraints.BOTH;
216 gbc_field.insets =
new Insets(0,5,0,5);
217 gbc_field.gridwidth = 2;
218 gbc_field.gridheight = 1;
223 reset =
new Button(
"Reset");
224 GridBagConstraints gbc_reset =
new GridBagConstraints();
225 gbc_reset.fill = GridBagConstraints.BOTH;
228 add(
reset,gbc_reset);
231 reset.addActionListener(
new ActionListener() {
232 public void actionPerformed(ActionEvent e)
236 playerList.removeAll();
241 addPlayer =
new Button(
"Add Player");
242 GridBagConstraints gbc_player =
new GridBagConstraints();
243 gbc_player.fill = GridBagConstraints.BOTH;
244 gbc_player.gridx = 1;
245 gbc_player.gridy = 6;
246 add(addPlayer,gbc_player);
249 addPlayer.addActionListener(
new ActionListener() {
250 public void actionPerformed(ActionEvent e)
253 playerList.add(addPlayerField.getText());
257 setSpeed =
new Button(
"Speed");
258 setSpeed.setBounds(0,0,10,10);
259 GridBagConstraints gbc_speedlabel =
new GridBagConstraints();
260 gbc_speedlabel.fill = GridBagConstraints.BOTH;
261 gbc_speedlabel.gridwidth = 1;
262 gbc_speedlabel.gridheight = 1;
263 gbc_speedlabel.gridx = 2;
264 gbc_speedlabel.gridy = 6;
265 add(setSpeed,gbc_speedlabel);
268 setSpeed.addActionListener(
new ActionListener() {
269 public void actionPerformed(ActionEvent e)
274 newSpeed = Integer.parseInt(speed.getText());
276 catch(NumberFormatException ne)
278 JOptionPane.showMessageDialog(null,
"Invalid speed (not an integer)");
284 JOptionPane.showMessageDialog(null,
"Invalid speed (negative number)");
288 boolean wasRunning = ticker.isRunning();
291 ticker.setInitialDelay(newSpeed);
292 ticker.setDelay(newSpeed);
298 startstop =
new Button(
"Play");
299 GridBagConstraints gbc_startstop =
new GridBagConstraints();
300 gbc_startstop.fill = GridBagConstraints.BOTH;
301 gbc_startstop.gridx = 2;
302 gbc_startstop.gridy = 0;
303 add(startstop,gbc_startstop);
306 startstop.addActionListener(
new ActionListener() {
307 public void actionPerformed(ActionEvent e)
316 catch(
RoboSim.RoboSimExecutionException m)
318 JOptionPane.showMessageDialog(null,m.getMessage());
321 startstop.setLabel(
"Pause");
326 startstop.setLabel(
"Play");
332 ticker =
new Timer(999999,
new ActionListener() {
333 public void actionPerformed(ActionEvent e)
338 ret = current_sim.executeSingleTimeStep();
340 catch(
RoboSim.RoboSimExecutionException m)
342 JOptionPane.showMessageDialog(null,m.getMessage());
349 JOptionPane.showMessageDialog(null,
"The winner is: "+ret);
356 public static void main(String[] args)
361 x=Integer.parseInt(args[0]);
362 y=Integer.parseInt(args[1]);
364 bots_per_player=Integer.parseInt(args[3]);
369 gui.setVisible(
true);
SimulatorGUI(int gridX, int gridY, int skillz, int bots_per_player, int obstacles_)
int initial_robots_per_combatant
static void main(String[] args)