initial commit
[applet-bots.git] / src / appletbots / gatherers / GatherersApplet.java
blob226801d9d9717390373875ae5684e4ab14315e4d
1 /*
2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserved
3 */
4 package appletbots.gatherers;
6 import appletbots.World;
7 import appletbots.Applet;
8 import appletbots.geometry.Point;
9 import appletbots.geometry.Vector;
11 import javax.swing.*;
12 import javax.swing.event.ChangeEvent;
13 import javax.swing.event.ChangeListener;
14 import java.awt.*;
15 import java.awt.event.ActionEvent;
16 import java.util.Hashtable;
18 /**
19 * An applet to run a Gatherer simulation
21 * @author Erik Rasmussen
23 public class GatherersApplet extends Applet
25 /**
26 * The maximum number of red agents allowed
28 public static final int MAX_RED_NUM_AGENTS = 50;
29 /**
30 * The minimum number of red agents allowed
32 public static final int MIN_RED_NUM_AGENTS = 1;
33 /**
34 * The number of red agents
36 protected int numRedAgents = 5;
37 /**
38 * The "Number of Red Agents" slider
40 protected JSlider numRedAgentsSlider;
41 /**
42 * The "Number of Red Agents" label
44 protected JLabel numRedAgentsLabel;
45 /**
46 * The maximum number of blue agents allowed
48 public static final int MAX_BLUE_NUM_AGENTS = 50;
49 /**
50 * The minimum number of blue agents allowed
52 public static final int MIN_BLUE_NUM_AGENTS = 1;
53 /**
54 * The number of blue agents
56 protected int numBlueAgents = 5;
57 /**
58 * The "Number of Blue Agents" slider
60 protected JSlider numBlueAgentsSlider;
61 /**
62 * The "Number of Blue Agents" label
64 protected JLabel numBlueAgentsLabel;
65 /**
66 * The maximum agent sight value allowed
68 public static final int MAX_AGENT_SIGHT = 100;
69 /**
70 * The minimum agent sight value allowed
72 public static final int MIN_AGENT_SIGHT = 5;
73 /**
74 * The agent sight value
76 protected int agentSight = 60;
77 /**
78 * The "Agent Sight" slider
80 protected JSlider agentSightSlider;
81 /**
82 * The "Agent Sight" label
84 protected JLabel agentSightLabel;
85 /**
86 * The maximum number of food allowed
88 public static final int MAX_NUM_FOOD = 25;
89 /**
90 * The minimum number of food allowed
92 public static final int MIN_NUM_FOOD = 1;
93 /**
94 * The number of food
96 protected int numFood = 10;
97 /**
98 * The "Number of Food" slider
100 protected JSlider numFoodSlider;
102 * The "Number of Food" label
104 protected JLabel numFoodLabel;
106 * The "Stationary Food" checkbox
108 protected JCheckBox stationaryFoodCheckBox;
110 * Whether or not the food is stationary
112 protected boolean stationaryFood = true;
115 * Initializes the world with the appropriate amount of food and
116 * Gatherers with the appropriate "agent sight" settings
118 * @return A world with Gatherers
120 protected World initializeWorld()
122 final GatherersWorld world = new GatherersWorld(300, 300, 100);
125 for (int i = 0; i < numFood; i++)
126 world.addObject(new Food(2, (stationaryFood ? 0 : 1), 10, Color.red), new Point(world.getWorldWidth() / 2 - (numFood * 5 / 2) + i * 5, world.getWorldHeight() / 2), new Vector(0, 0));
127 for (int i = 0; i < numBlueAgents; i++)
129 world.addObject(new Gatherer(5, true, 5, agentSight, 5, 3));
131 for (int i = 0; i < numRedAgents; i++)
133 world.addObject(new Gatherer(5, false, 5, agentSight, 5, 3));
136 catch (Exception e)
138 e.printStackTrace();
140 return world;
144 * Returns a settings panel with "Number of Agents", "Number of Food",
145 * and "Agent Sight" sliders
147 * @return A settings panel with "Number of Agents", "Number of Food",
148 * and "Agent Sight" sliders
150 protected JPanel getSettingsPanel()
152 final JPanel settingsPanel = new JPanel();
153 settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));
155 numRedAgentsLabel = new JLabel("Number of Red Agents: " + numRedAgents);
156 numRedAgentsLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
157 settingsPanel.add(numRedAgentsLabel);
159 final Hashtable numRedAgentsLabels = new Hashtable();
160 numRedAgentsLabels.put(new Integer(MIN_RED_NUM_AGENTS), new JLabel(Integer.toString(MIN_RED_NUM_AGENTS)));
161 numRedAgentsLabels.put(new Integer(MAX_RED_NUM_AGENTS), new JLabel(Integer.toString(MAX_RED_NUM_AGENTS)));
163 numRedAgentsSlider = new JSlider(MIN_RED_NUM_AGENTS, MAX_RED_NUM_AGENTS, numRedAgents);
164 numRedAgentsSlider.setLabelTable(numRedAgentsLabels);
165 numRedAgentsSlider.setPaintLabels(true);
166 numRedAgentsSlider.addChangeListener(new ChangeListener()
168 public void stateChanged(final ChangeEvent event)
170 numRedAgentsChanged(event);
173 settingsPanel.add(numRedAgentsSlider);
175 numBlueAgentsLabel = new JLabel("Number of Blue Agents: " + numBlueAgents);
176 numBlueAgentsLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
177 settingsPanel.add(numBlueAgentsLabel);
179 final Hashtable numBlueAgentsLabels = new Hashtable();
180 numBlueAgentsLabels.put(new Integer(MIN_BLUE_NUM_AGENTS), new JLabel(Integer.toString(MIN_BLUE_NUM_AGENTS)));
181 numBlueAgentsLabels.put(new Integer(MAX_BLUE_NUM_AGENTS), new JLabel(Integer.toString(MAX_BLUE_NUM_AGENTS)));
183 numBlueAgentsSlider = new JSlider(MIN_BLUE_NUM_AGENTS, MAX_BLUE_NUM_AGENTS, numBlueAgents);
184 numBlueAgentsSlider.setLabelTable(numBlueAgentsLabels);
185 numBlueAgentsSlider.setPaintLabels(true);
186 numBlueAgentsSlider.addChangeListener(new ChangeListener()
188 public void stateChanged(final ChangeEvent event)
190 numBlueAgentsChanged(event);
193 settingsPanel.add(numBlueAgentsSlider);
195 agentSightLabel = new JLabel("Agent Sight: " + agentSight);
196 agentSightLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
197 settingsPanel.add(agentSightLabel);
199 final Hashtable agentSightLabels = new Hashtable();
200 agentSightLabels.put(new Integer(MIN_AGENT_SIGHT), new JLabel(Integer.toString(MIN_AGENT_SIGHT)));
201 agentSightLabels.put(new Integer(MAX_AGENT_SIGHT), new JLabel(Integer.toString(MAX_AGENT_SIGHT)));
203 agentSightSlider = new JSlider(MIN_AGENT_SIGHT, MAX_AGENT_SIGHT, agentSight);
204 agentSightSlider.setLabelTable(agentSightLabels);
205 agentSightSlider.setPaintLabels(true);
206 agentSightSlider.addChangeListener(new ChangeListener()
208 public void stateChanged(final ChangeEvent event)
210 agentSightChanged(event);
213 settingsPanel.add(agentSightSlider);
215 numFoodLabel = new JLabel("Number of Food: " + numFood);
216 numFoodLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
217 settingsPanel.add(numFoodLabel);
219 final Hashtable numFoodLabels = new Hashtable();
220 numFoodLabels.put(new Integer(MIN_NUM_FOOD), new JLabel(Integer.toString(MIN_NUM_FOOD)));
221 numFoodLabels.put(new Integer(MAX_NUM_FOOD), new JLabel(Integer.toString(MAX_NUM_FOOD)));
223 numFoodSlider = new JSlider(MIN_NUM_FOOD, MAX_NUM_FOOD, numFood);
224 numFoodSlider.setLabelTable(numFoodLabels);
225 numFoodSlider.setPaintLabels(true);
226 numFoodSlider.addChangeListener(new ChangeListener()
228 public void stateChanged(final ChangeEvent event)
230 numFoodChanged(event);
233 settingsPanel.add(numFoodSlider);
235 stationaryFoodCheckBox = new JCheckBox("Stationary Food");
236 stationaryFoodCheckBox.setSelected(stationaryFood);
237 stationaryFoodCheckBox.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
238 stationaryFoodCheckBox.addActionListener(new java.awt.event.ActionListener()
240 public void actionPerformed(final ActionEvent event)
242 GatherersApplet.this.stationaryFoodClicked(event);
245 settingsPanel.add(stationaryFoodCheckBox);
247 final JLabel changes = new JLabel("Changes will take");
248 changes.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
249 final JLabel takeEffect = new JLabel("effect at next reset");
250 takeEffect.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
251 settingsPanel.add(changes);
252 settingsPanel.add(takeEffect);
253 return settingsPanel;
257 * The method invoked when the "Number of Red Agents" slider changes
259 * @param event The change event
261 public void numRedAgentsChanged(final ChangeEvent event)
263 numRedAgents = numRedAgentsSlider.getValue();
264 numRedAgentsLabel.setText("Number of Red Agents: " + numRedAgents);
268 * The method invoked when the "Number of Blue Agents" slider changes
270 * @param event The change event
272 public void numBlueAgentsChanged(final ChangeEvent event)
274 numBlueAgents = numBlueAgentsSlider.getValue();
275 numBlueAgentsLabel.setText("Number of Blue Agents: " + numBlueAgents);
279 * The method invoked when the "Agent Sight" slider changes
281 * @param event The change event
283 public void agentSightChanged(final ChangeEvent event)
285 agentSight = agentSightSlider.getValue();
286 agentSightLabel.setText("Agent Sight: " + agentSight);
290 * The method invoked when the "Number of Food" slider changes
292 * @param event The change event
294 public void numFoodChanged(final ChangeEvent event)
296 numFood = numFoodSlider.getValue();
297 numFoodLabel.setText("Number of Food: " + numFood);
301 * The method invoked when the user selects or deselects the "Stationary
302 * Food" check box
304 * @param event The action event
306 public void stationaryFoodClicked(final ActionEvent event)
308 stationaryFood = stationaryFoodCheckBox.isSelected();