2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
4 package appletbots.swarm;
6 import appletbots.World;
7 import appletbots.Applet;
10 import javax.swing.event.ChangeEvent;
11 import javax.swing.event.ChangeListener;
12 import java.awt.event.ActionEvent;
13 import java.text.NumberFormat;
14 import java.util.Hashtable;
17 * An applet to run a SwarmAgent simulation
19 * @author Erik Rasmussen
21 public class SwarmApplet extends Applet
24 * The maximum number of agents allowed
26 public static final int MAX_NUM_AGENTS = 100;
28 * The minimum number of agents allowed
30 public static final int MIN_NUM_AGENTS = 2;
32 * The number of agents
34 protected int numAgents = 50;
36 * The "Number of Agents" slider
38 protected JSlider numAgentsSlider;
40 * The "Number of Agents" label
42 protected JLabel numAgentsLabel;
44 * The maximum agent sight value allowed
46 public static final int MAX_AGENT_SIGHT = 100;
48 * The minimum agent sight value allowed
50 public static final int MIN_AGENT_SIGHT = 5;
52 * The agent sight value
54 protected int agentSight = 60;
56 * The "Agent Sight" slider
58 protected JSlider agentSightSlider;
60 * The "Agent Sight" label
62 protected JLabel agentSightLabel;
64 * The maximum number of neighbors to follow allowed
66 public static final int MAX_NUM_NEIGHBORS_TO_FOLLOW = 10;
68 * The minimum number of neighbors to follow allowed
70 public static final int MIN_NUM_NEIGHBORS_TO_FOLLOW = 1;
72 * The number of neighbors to follow
74 protected int numNeighborsToFollow = 3;
76 * The "Number of Neighbors to Follow" slider
78 protected JSlider numNeighborsToFollowSlider;
80 * The "Number of Neighbors to Follow" label
82 protected JLabel numNeighborsToFollowLabel;
84 * The maximum randomization factor allowed
86 public static final int MAX_RANDOMIZATION_FACTOR = 100;
88 * The minimum randomization factor allowed
90 public static final int MIN_RANDOMIZATION_FACTOR = 0;
92 * The randomization factor
94 protected int randomizationFactor = 10;
96 * The "Randomization Factor" slider
98 protected JSlider randomizationFactorSlider;
100 * The "Randomization Factor" label
102 protected JLabel randomizationFactorLabel;
104 * The "Choose Neighbors by Proximity" checkbox
106 protected JCheckBox chooseNeighborsByProximityCheckBox;
108 * How to choose neighbors to follow
110 protected boolean chooseNeighborsByProximity = true;
112 private static final NumberFormat PERCENT_FORMAT = NumberFormat.getPercentInstance();
115 * Initializes the world with the appropriate number of SwarmAgents
116 * with the appropriate "agent sight" settings
118 * @return A world with SwarmAgents
120 protected World initializeWorld()
122 final World world = new World(300, 300);
125 for (int i = 0; i < numAgents; i++)
126 world.addObject(new SwarmAgent(5, agentSight, 5, 3, numNeighborsToFollow, chooseNeighborsByProximity, randomizationFactor / 100.0));
136 * Returns a settings panel with "Number of Agents" and "Agent Sight"
139 * @return A settings panel with "Number of Agents" and "Agent Sight"
142 protected JPanel getSettingsPanel()
144 final JPanel settingsPanel = new JPanel();
145 settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));
147 numAgentsLabel = new JLabel("Number of Agents: " + numAgents);
148 numAgentsLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
149 settingsPanel.add(numAgentsLabel);
151 final Hashtable numAgentsLabels = new Hashtable();
152 numAgentsLabels.put(new Integer(MIN_NUM_AGENTS), new JLabel(Integer.toString(MIN_NUM_AGENTS)));
153 numAgentsLabels.put(new Integer(MAX_NUM_AGENTS), new JLabel(Integer.toString(MAX_NUM_AGENTS)));
155 numAgentsSlider = new JSlider(MIN_NUM_AGENTS, MAX_NUM_AGENTS, numAgents);
156 numAgentsSlider.setLabelTable(numAgentsLabels);
157 numAgentsSlider.setPaintLabels(true);
158 numAgentsSlider.addChangeListener(new ChangeListener()
160 public void stateChanged(final ChangeEvent event)
162 numAgentsChanged(event);
165 settingsPanel.add(numAgentsSlider);
167 agentSightLabel = new JLabel("Agent Sight: " + agentSight);
168 agentSightLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
169 settingsPanel.add(agentSightLabel);
171 final Hashtable agentSightLabels = new Hashtable();
172 agentSightLabels.put(new Integer(MIN_AGENT_SIGHT), new JLabel(Integer.toString(MIN_AGENT_SIGHT)));
173 agentSightLabels.put(new Integer(MAX_AGENT_SIGHT), new JLabel(Integer.toString(MAX_AGENT_SIGHT)));
175 agentSightSlider = new JSlider(MIN_AGENT_SIGHT, MAX_AGENT_SIGHT, agentSight);
176 agentSightSlider.setLabelTable(agentSightLabels);
177 agentSightSlider.setPaintLabels(true);
178 agentSightSlider.addChangeListener(new ChangeListener()
180 public void stateChanged(final ChangeEvent event)
182 agentSightChanged(event);
185 settingsPanel.add(agentSightSlider);
187 numNeighborsToFollowLabel = new JLabel("Number of Neighbors to Follow: " + numNeighborsToFollow);
188 numNeighborsToFollowLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
189 settingsPanel.add(numNeighborsToFollowLabel);
191 final Hashtable numNeighborsToFollowLabels = new Hashtable();
192 numNeighborsToFollowLabels.put(new Integer(MIN_NUM_NEIGHBORS_TO_FOLLOW), new JLabel(Integer.toString(MIN_NUM_NEIGHBORS_TO_FOLLOW)));
193 numNeighborsToFollowLabels.put(new Integer(MAX_NUM_NEIGHBORS_TO_FOLLOW), new JLabel(Integer.toString(MAX_NUM_NEIGHBORS_TO_FOLLOW)));
195 numNeighborsToFollowSlider = new JSlider(MIN_NUM_NEIGHBORS_TO_FOLLOW, MAX_NUM_NEIGHBORS_TO_FOLLOW, numNeighborsToFollow);
196 numNeighborsToFollowSlider.setLabelTable(numNeighborsToFollowLabels);
197 numNeighborsToFollowSlider.setPaintLabels(true);
198 numNeighborsToFollowSlider.addChangeListener(new ChangeListener()
200 public void stateChanged(final ChangeEvent event)
202 numNeighborsToFollowChanged(event);
205 settingsPanel.add(numNeighborsToFollowSlider);
207 randomizationFactorLabel = new JLabel("Randomization Factor: " + PERCENT_FORMAT.format(randomizationFactor / 100.0));
208 randomizationFactorLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
209 settingsPanel.add(randomizationFactorLabel);
211 final Hashtable randomizationFactorLabels = new Hashtable();
212 randomizationFactorLabels.put(new Integer(MIN_RANDOMIZATION_FACTOR), new JLabel(PERCENT_FORMAT.format(MIN_RANDOMIZATION_FACTOR / 100.0)));
213 randomizationFactorLabels.put(new Integer(MAX_RANDOMIZATION_FACTOR), new JLabel(PERCENT_FORMAT.format(MAX_RANDOMIZATION_FACTOR / 100.0)));
215 randomizationFactorSlider = new JSlider(MIN_RANDOMIZATION_FACTOR, MAX_RANDOMIZATION_FACTOR, randomizationFactor);
216 randomizationFactorSlider.setLabelTable(randomizationFactorLabels);
217 randomizationFactorSlider.setPaintLabels(true);
218 randomizationFactorSlider.addChangeListener(new ChangeListener()
220 public void stateChanged(final ChangeEvent event)
222 randomizationFactorChanged(event);
225 settingsPanel.add(randomizationFactorSlider);
227 chooseNeighborsByProximityCheckBox = new JCheckBox("Choose Neighbors by Proximity");
228 chooseNeighborsByProximityCheckBox.setSelected(chooseNeighborsByProximity);
229 chooseNeighborsByProximityCheckBox.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
230 chooseNeighborsByProximityCheckBox.addActionListener(new java.awt.event.ActionListener()
232 public void actionPerformed(final ActionEvent event)
234 chooseNeighborsByProximityClicked(event);
237 settingsPanel.add(chooseNeighborsByProximityCheckBox);
239 final JLabel changes = new JLabel("Changes will take");
240 changes.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
241 final JLabel takeEffect = new JLabel("effect at next reset");
242 takeEffect.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
243 settingsPanel.add(changes);
244 settingsPanel.add(takeEffect);
245 return settingsPanel;
249 * The method invoked when the "Number of Agents" slider changes
251 * @param event The change event
253 public void numAgentsChanged(final ChangeEvent event)
255 numAgents = numAgentsSlider.getValue();
256 numAgentsLabel.setText("Number of Agents: " + numAgents);
260 * The method invoked when the "Agent Sight" slider changes
262 * @param event The change event
264 public void agentSightChanged(final ChangeEvent event)
266 agentSight = agentSightSlider.getValue();
267 agentSightLabel.setText("Agent Sight: " + agentSight);
271 * The method invoked when the "Number of Neighbors to Follow" slider changes
273 * @param event The change event
275 public void numNeighborsToFollowChanged(final ChangeEvent event)
277 numNeighborsToFollow = numNeighborsToFollowSlider.getValue();
278 numNeighborsToFollowLabel.setText("Number of Neighbors to Follow: " + numNeighborsToFollow);
282 * The method invoked when the "Randomization Factor" slider changes
284 * @param event The change event
286 public void randomizationFactorChanged(final ChangeEvent event)
288 randomizationFactor = randomizationFactorSlider.getValue();
289 randomizationFactorLabel.setText("Randomization Factor: " + PERCENT_FORMAT.format(randomizationFactor / 100.0));
293 * The method invoked when the user selects or deselects the "Choose Neighbors
294 * by Proximity" check box
296 * @param event The action event
298 public void chooseNeighborsByProximityClicked(final ActionEvent event)
300 chooseNeighborsByProximity = chooseNeighborsByProximityCheckBox.isSelected();