2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
4 package appletbots
.spreadout
;
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 SpreadOutAgent simulation
19 * @author Erik Rasmussen
21 public class SpreadOutApplet
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 acceleration factor allowed
66 public static final int MAX_ACCELERATION_FACTOR
= 100;
68 * The minimum acceleration factor allowed
70 public static final int MIN_ACCELERATION_FACTOR
= 0;
72 * The acceleration factor
74 protected int accelerationFactor
= 10;
76 * The "Acceleration Factor" slider
78 protected JSlider accelerationFactorSlider
;
80 * The "Acceleration Factor" label
82 protected JLabel accelerationFactorLabel
;
84 * The "Stop When No Visible Neighbors" checkbox
86 protected JCheckBox stopWhenNoNeighborsCheckBox
;
88 * Whether or not to stop when there are no visible neighbors
90 protected boolean stopWhenNoNeighbors
= true;
92 private static final NumberFormat PERCENT_FORMAT
= NumberFormat
.getPercentInstance();
95 * Initializes the world with the appropriate number of SpreadOutAgents
96 * with the appropriate "agent sight" settings
98 * @return A world with SpreadOutAgents
100 protected World
initializeWorld()
102 final World world
= new World(300, 300);
105 for (int i
= 0; i
< numAgents
; i
++)
106 world
.addObject(new SpreadOutAgent(5, agentSight
, 5, 3, accelerationFactor
, stopWhenNoNeighbors
));
116 * Returns a settings panel with "Number of Agents" and "Agent Sight"
119 * @return A settings panel with "Number of Agents" and "Agent Sight"
122 protected JPanel
getSettingsPanel()
124 final JPanel settingsPanel
= new JPanel();
125 settingsPanel
.setLayout(new BoxLayout(settingsPanel
, BoxLayout
.Y_AXIS
));
127 numAgentsLabel
= new JLabel("Number of Agents: " + numAgents
);
128 numAgentsLabel
.setAlignmentX(java
.awt
.Component
.CENTER_ALIGNMENT
);
129 settingsPanel
.add(numAgentsLabel
);
131 final Hashtable numAgentsLabels
= new Hashtable();
132 numAgentsLabels
.put(new Integer(MIN_NUM_AGENTS
), new JLabel(Integer
.toString(MIN_NUM_AGENTS
)));
133 numAgentsLabels
.put(new Integer(MAX_NUM_AGENTS
), new JLabel(Integer
.toString(MAX_NUM_AGENTS
)));
135 numAgentsSlider
= new JSlider(MIN_NUM_AGENTS
, MAX_NUM_AGENTS
, numAgents
);
136 numAgentsSlider
.setLabelTable(numAgentsLabels
);
137 numAgentsSlider
.setPaintLabels(true);
138 numAgentsSlider
.addChangeListener(new ChangeListener()
140 public void stateChanged(final ChangeEvent event
)
142 numAgentsChanged(event
);
145 settingsPanel
.add(numAgentsSlider
);
147 agentSightLabel
= new JLabel("Agent Sight: " + agentSight
);
148 agentSightLabel
.setAlignmentX(java
.awt
.Component
.CENTER_ALIGNMENT
);
149 settingsPanel
.add(agentSightLabel
);
151 final Hashtable agentSightLabels
= new Hashtable();
152 agentSightLabels
.put(new Integer(MIN_AGENT_SIGHT
), new JLabel(Integer
.toString(MIN_AGENT_SIGHT
)));
153 agentSightLabels
.put(new Integer(MAX_AGENT_SIGHT
), new JLabel(Integer
.toString(MAX_AGENT_SIGHT
)));
155 agentSightSlider
= new JSlider(MIN_AGENT_SIGHT
, MAX_AGENT_SIGHT
, agentSight
);
156 agentSightSlider
.setLabelTable(agentSightLabels
);
157 agentSightSlider
.setPaintLabels(true);
158 agentSightSlider
.addChangeListener(new ChangeListener()
160 public void stateChanged(final ChangeEvent event
)
162 agentSightChanged(event
);
165 settingsPanel
.add(agentSightSlider
);
167 accelerationFactorLabel
= new JLabel("Acceleration Factor: " + PERCENT_FORMAT
.format(accelerationFactor
/ 100.0));
168 accelerationFactorLabel
.setAlignmentX(java
.awt
.Component
.CENTER_ALIGNMENT
);
169 settingsPanel
.add(accelerationFactorLabel
);
171 final Hashtable accelerationFactorLabels
= new Hashtable();
172 accelerationFactorLabels
.put(new Integer(MIN_ACCELERATION_FACTOR
), new JLabel(PERCENT_FORMAT
.format(MIN_ACCELERATION_FACTOR
/ 100.0)));
173 accelerationFactorLabels
.put(new Integer(MAX_ACCELERATION_FACTOR
), new JLabel(PERCENT_FORMAT
.format(MAX_ACCELERATION_FACTOR
/ 100.0)));
175 accelerationFactorSlider
= new JSlider(MIN_ACCELERATION_FACTOR
, MAX_ACCELERATION_FACTOR
, accelerationFactor
);
176 accelerationFactorSlider
.setLabelTable(accelerationFactorLabels
);
177 accelerationFactorSlider
.setPaintLabels(true);
178 accelerationFactorSlider
.addChangeListener(new ChangeListener()
180 public void stateChanged(final ChangeEvent event
)
182 accelerationFactorChanged(event
);
185 settingsPanel
.add(accelerationFactorSlider
);
187 stopWhenNoNeighborsCheckBox
= new JCheckBox("Stop When No Visible Neighbors");
188 stopWhenNoNeighborsCheckBox
.setSelected(stopWhenNoNeighbors
);
189 stopWhenNoNeighborsCheckBox
.setAlignmentX(java
.awt
.Component
.CENTER_ALIGNMENT
);
190 stopWhenNoNeighborsCheckBox
.addActionListener(new java
.awt
.event
.ActionListener()
192 public void actionPerformed(final ActionEvent event
)
194 stopWhenNoNeighborsClicked(event
);
197 settingsPanel
.add(stopWhenNoNeighborsCheckBox
);
199 final JLabel changes
= new JLabel("Changes will take");
200 changes
.setAlignmentX(java
.awt
.Component
.CENTER_ALIGNMENT
);
201 final JLabel takeEffect
= new JLabel("effect at next reset");
202 takeEffect
.setAlignmentX(java
.awt
.Component
.CENTER_ALIGNMENT
);
203 settingsPanel
.add(changes
);
204 settingsPanel
.add(takeEffect
);
205 return settingsPanel
;
209 * The method invoked when the "Number of Agents" slider changes
211 * @param event The change event
213 public void numAgentsChanged(final ChangeEvent event
)
215 numAgents
= numAgentsSlider
.getValue();
216 numAgentsLabel
.setText("Number of Agents: " + numAgents
);
220 * The method invoked when the "Agent Sight" slider changes
222 * @param event The change event
224 public void agentSightChanged(final ChangeEvent event
)
226 agentSight
= agentSightSlider
.getValue();
227 agentSightLabel
.setText("Agent Sight: " + agentSight
);
231 * The method invoked when the "Acceleration Factor" slider changes
233 * @param event The change event
235 public void accelerationFactorChanged(final ChangeEvent event
)
237 accelerationFactor
= accelerationFactorSlider
.getValue();
238 accelerationFactorLabel
.setText("Acceleration Factor: " + PERCENT_FORMAT
.format(accelerationFactor
/ 100.0));
242 * The method invoked when the user selects or deselects the "Choose Neighbors
243 * by Proximity" check box
245 * @param event The action event
247 public void stopWhenNoNeighborsClicked(final ActionEvent event
)
249 stopWhenNoNeighbors
= stopWhenNoNeighborsCheckBox
.isSelected();