SimulatorGUI.java
Go to the documentation of this file.
1 
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.JOptionPane;
27 import javax.swing.Timer;
28 import java.util.HashMap;
29 public class SimulatorGUI extends Frame
30 {
31  //Program State
33  private Timer ticker;
34 
35  //GUI Components
36  private List playerList;
37  private Canvas canvas;
38  private TextField speed;
39  private TextField addPlayerField;
40  private Button reset, addPlayer, setSpeed, startstop;
41 
42  //Parameters for RoboSim
44  private int skill_points;
45  private int length;
46  private int width;
47  private int obstacles;
48 
49  public SimulatorGUI(int gridX, int gridY, int skillz, int bots_per_player, int obstacles_) {
50  //Store RoboSim parameters
51  length=gridX;
52  width=gridY;
53  skill_points=skillz;
54  initial_robots_per_combatant = bots_per_player;
55  obstacles=obstacles_;
56 
57  //Set up window
58  setSize(500,500);
59  addWindowListener(new WindowAdapter() {
60  public void windowClosing(WindowEvent we) {
61  dispose();
62  }
63  });
64 
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};
70  setLayout(gbl_panel);
71 
72  playerList = new List();
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;
79  gbc_list.gridx = 0;
80  gbc_list.gridy = 0;
81  add(playerList, gbc_list);
82 
83  canvas = new Canvas() {
84  public void paint(Graphics g)
85  {
86  //Get bounds of rid
87  Rectangle r = getBounds();
88  int cell_length = r.width/length;
89  int cell_height = r.height/width;
90 
91  //Draw grid
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);
96 
97  //If there's no current sim, we need to exit now
98  if(current_sim==null)
99  return;
100 
101  //Generate color mapping
102  HashMap<String,Color> colorMap = new HashMap<String,Color>();
103  Color current_color = Color.BLACK;
104  for(String x : playerList.getItems())
105  {
106  //Update color
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;
131 
132  //Add mapping
133  colorMap.put(x,current_color);
134  }
135 
136  //Iterate over RoboSim grid and "paint" squares that need to be painted
137  Robot.GridCell[][] world = current_sim.getWorldGrid();
138  for(int i=0; i<length; i++)
139  for(int j=0; j<width; j++)
140  switch(world[i][j].contents)
141  {
142  case BLOCKED:
143  //Draw a black 'X' in the cell
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);
147  break;
148  case SELF:
149  //Fill rectangle with color representing player
150  g.setColor(colorMap.get(current_sim.getOccupantPlayer(world[i][j])));
151  g.fillRect(i*cell_length,j*cell_height,cell_length,cell_height);
152  break;
153  case WALL:
154  g.setColor(Color.BLACK);
155  g.fillRect(i*cell_length,j*cell_height,cell_length,cell_height);
156  break;
157  case FORT:
158  g.setColor(Color.BLACK);
159  switch(world[i][j].fort_orientation)
160  {
161  case UP:
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);
165  break;
166  case DOWN:
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);
170  break;
171  case LEFT:
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);
175  break;
176  case RIGHT:
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);
180  break;
181  }
182  break;
183  case CAPSULE:
184  g.setColor(Color.BLACK);
185  g.drawOval(i*cell_length,j*cell_height,cell_length,cell_height);
186  break;
187  }
188  }
189  };
190 
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;
199  add(canvas, gbc_canvas);
200 
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;
207  gbc_speed.gridx = 2;
208  gbc_speed.gridy = 5;
209  add(speed,gbc_speed);
210 
211  addPlayerField = new TextField();
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;
219  gbc_field.gridx = 0;
220  gbc_field.gridy = 5;
221  add(addPlayerField,gbc_field);
222 
223  reset = new Button("Reset");
224  GridBagConstraints gbc_reset = new GridBagConstraints();
225  gbc_reset.fill = GridBagConstraints.BOTH;
226  gbc_reset.gridx = 0;
227  gbc_reset.gridy = 6;
228  add(reset,gbc_reset);
229 
230  //Reset button destroys everything
231  reset.addActionListener(new ActionListener() {
232  public void actionPerformed(ActionEvent e)
233  {
234  current_sim = null;
235  ticker.stop();
236  playerList.removeAll();
237  canvas.repaint();
238  }
239  });
240 
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);
247 
248  //Add Player button adds a player to the list
249  addPlayer.addActionListener(new ActionListener() {
250  public void actionPerformed(ActionEvent e)
251  {
252  if(!addPlayerField.getText().equals(""))
253  playerList.add(addPlayerField.getText());
254  }
255  });
256 
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);
266 
267  //Speed sets speed to new value
268  setSpeed.addActionListener(new ActionListener() {
269  public void actionPerformed(ActionEvent e)
270  {
271  int newSpeed = 0;
272  try
273  {
274  newSpeed = Integer.parseInt(speed.getText());
275  }
276  catch(NumberFormatException ne)
277  {
278  JOptionPane.showMessageDialog(null,"Invalid speed (not an integer)");
279  return;
280  }
281 
282  if(newSpeed <= 0)
283  {
284  JOptionPane.showMessageDialog(null,"Invalid speed (negative number)");
285  return;
286  }
287 
288  boolean wasRunning = ticker.isRunning();
289  if(wasRunning)
290  ticker.stop();
291  ticker.setInitialDelay(newSpeed);
292  ticker.setDelay(newSpeed);
293  if(wasRunning)
294  ticker.restart();
295  }
296  });
297 
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);
304 
305  //Play button starts world by starting timer
306  startstop.addActionListener(new ActionListener() {
307  public void actionPerformed(ActionEvent e)
308  {
309  if(!ticker.isRunning())
310  {
311  if(current_sim==null)
312  try
313  {
315  }
316  catch(RoboSim.RoboSimExecutionException m)
317  {
318  JOptionPane.showMessageDialog(null,m.getMessage());
319  }
320  ticker.start();
321  startstop.setLabel("Pause");
322  }
323  else
324  {
325  ticker.stop();
326  startstop.setLabel("Play");
327  }
328  }
329  });
330 
331  //Set up timer
332  ticker = new Timer(999999, new ActionListener() {
333  public void actionPerformed(ActionEvent e)
334  {
335  String ret = null;
336  try
337  {
338  ret = current_sim.executeSingleTimeStep();
339  }
340  catch(RoboSim.RoboSimExecutionException m)
341  {
342  JOptionPane.showMessageDialog(null,m.getMessage());
343  ret=null;
344  ticker.stop();
345  }
346  if(ret!=null)
347  {
348  ticker.stop();
349  JOptionPane.showMessageDialog(null,"The winner is: "+ret);
350  }
351  canvas.repaint();
352  }
353  });
354  }
355 
356  public static void main(String[] args)
357  {
358  int x=20, y=20, skill_points=20, bots_per_player=5, obstacles=30;
359  if(args.length==5)
360  {
361  x=Integer.parseInt(args[0]);
362  y=Integer.parseInt(args[1]);
363  skill_points=Integer.parseInt(args[2]);
364  bots_per_player=Integer.parseInt(args[3]);
365  obstacles=Integer.parseInt(args[4]);
366  }
367 
368  SimulatorGUI gui = new SimulatorGUI(x,y,skill_points,bots_per_player,obstacles);
369  gui.setVisible(true);
370  }
371 }
SimulatorGUI(int gridX, int gridY, int skillz, int bots_per_player, int obstacles_)
RoboSim current_sim
int initial_robots_per_combatant
TextField addPlayerField
static void main(String[] args)
TextField speed