226801d9d9717390373875ae5684e4ab14315e4d
[applet-bots.git] / src / appletbots / gatherers / GatherersApplet.java
1 /*
2  * Copyright (c) 2002 Erik Rasmussen - All Rights Reserved
3  */
4 package appletbots.gatherers;
5
6 import appletbots.World;
7 import appletbots.Applet;
8 import appletbots.geometry.Point;
9 import appletbots.geometry.Vector;
10
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;
17
18 /**
19  * An applet to run a Gatherer simulation
20  *
21  * @author Erik Rasmussen
22  */
23 public class GatherersApplet extends Applet
24 {
25         /**
26          * The maximum number of red agents allowed
27          */
28         public static final int MAX_RED_NUM_AGENTS = 50;
29         /**
30          * The minimum number of red agents allowed
31          */
32         public static final int MIN_RED_NUM_AGENTS = 1;
33         /**
34          * The number of red agents
35          */
36         protected int numRedAgents = 5;
37         /**
38          * The "Number of Red Agents" slider
39          */
40         protected JSlider numRedAgentsSlider;
41         /**
42          * The "Number of Red Agents" label
43          */
44         protected JLabel numRedAgentsLabel;
45         /**
46          * The maximum number of blue agents allowed
47          */
48         public static final int MAX_BLUE_NUM_AGENTS = 50;
49         /**
50          * The minimum number of blue agents allowed
51          */
52         public static final int MIN_BLUE_NUM_AGENTS = 1;
53         /**
54          * The number of blue agents
55          */
56         protected int numBlueAgents = 5;
57         /**
58          * The "Number of Blue Agents" slider
59          */
60         protected JSlider numBlueAgentsSlider;
61         /**
62          * The "Number of Blue Agents" label
63          */
64         protected JLabel numBlueAgentsLabel;
65         /**
66          * The maximum agent sight value allowed
67          */
68         public static final int MAX_AGENT_SIGHT = 100;
69         /**
70          * The minimum agent sight value allowed
71          */
72         public static final int MIN_AGENT_SIGHT = 5;
73         /**
74          * The agent sight value
75          */
76         protected int agentSight = 60;
77         /**
78          * The "Agent Sight" slider
79          */
80         protected JSlider agentSightSlider;
81         /**
82          * The "Agent Sight" label
83          */
84         protected JLabel agentSightLabel;
85         /**
86          * The maximum number of food allowed
87          */
88         public static final int MAX_NUM_FOOD = 25;
89         /**
90          * The minimum number of food allowed
91          */
92         public static final int MIN_NUM_FOOD = 1;
93         /**
94          * The number of food
95          */
96         protected int numFood = 10;
97         /**
98          * The "Number of Food" slider
99          */
100         protected JSlider numFoodSlider;
101         /**
102          * The "Number of Food" label
103          */
104         protected JLabel numFoodLabel;
105         /**
106          * The "Stationary Food" checkbox
107          */
108         protected JCheckBox stationaryFoodCheckBox;
109         /**
110          * Whether or not the food is stationary
111          */
112         protected boolean stationaryFood = true;
113
114         /**
115          * Initializes the world with the appropriate amount of food and
116          * Gatherers with the appropriate "agent sight" settings
117          *
118          * @return A world with Gatherers
119          */
120         protected World initializeWorld()
121         {
122                 final GatherersWorld world = new GatherersWorld(300, 300, 100);
123                 try
124                 {
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++)
128                         {
129                                 world.addObject(new Gatherer(5, true, 5, agentSight, 5, 3));
130                         }
131                         for (int i = 0; i < numRedAgents; i++)
132                         {
133                                 world.addObject(new Gatherer(5, false, 5, agentSight, 5, 3));
134                         }
135                 }
136                 catch (Exception e)
137                 {
138                         e.printStackTrace();
139                 }
140                 return world;
141         }
142
143         /**
144          * Returns a settings panel with "Number of Agents", "Number of Food",
145          * and "Agent Sight" sliders
146          *
147          * @return A settings panel with "Number of Agents", "Number of Food",
148          *         and "Agent Sight" sliders
149          */
150         protected JPanel getSettingsPanel()
151         {
152                 final JPanel settingsPanel = new JPanel();
153                 settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));
154
155                 numRedAgentsLabel = new JLabel("Number of Red Agents: " + numRedAgents);
156                 numRedAgentsLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
157                 settingsPanel.add(numRedAgentsLabel);
158
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)));
162
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()
167                 {
168                         public void stateChanged(final ChangeEvent event)
169                         {
170                                 numRedAgentsChanged(event);
171                         }
172                 });
173                 settingsPanel.add(numRedAgentsSlider);
174
175                 numBlueAgentsLabel = new JLabel("Number of Blue Agents: " + numBlueAgents);
176                 numBlueAgentsLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
177                 settingsPanel.add(numBlueAgentsLabel);
178
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)));
182
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()
187                 {
188                         public void stateChanged(final ChangeEvent event)
189                         {
190                                 numBlueAgentsChanged(event);
191                         }
192                 });
193                 settingsPanel.add(numBlueAgentsSlider);
194
195                 agentSightLabel = new JLabel("Agent Sight: " + agentSight);
196                 agentSightLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
197                 settingsPanel.add(agentSightLabel);
198
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)));
202
203                 agentSightSlider = new JSlider(MIN_AGENT_SIGHT, MAX_AGENT_SIGHT, agentSight);
204                 agentSightSlider.setLabelTable(agentSightLabels);
205                 agentSightSlider.setPaintLabels(true);
206                 agentSightSlider.addChangeListener(new ChangeListener()
207                 {
208                         public void stateChanged(final ChangeEvent event)
209                         {
210                                 agentSightChanged(event);
211                         }
212                 });
213                 settingsPanel.add(agentSightSlider);
214
215                 numFoodLabel = new JLabel("Number of Food: " + numFood);
216                 numFoodLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
217                 settingsPanel.add(numFoodLabel);
218
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)));
222
223                 numFoodSlider = new JSlider(MIN_NUM_FOOD, MAX_NUM_FOOD, numFood);
224                 numFoodSlider.setLabelTable(numFoodLabels);
225                 numFoodSlider.setPaintLabels(true);
226                 numFoodSlider.addChangeListener(new ChangeListener()
227                 {
228                         public void stateChanged(final ChangeEvent event)
229                         {
230                                 numFoodChanged(event);
231                         }
232                 });
233                 settingsPanel.add(numFoodSlider);
234
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()
239                 {
240                         public void actionPerformed(final ActionEvent event)
241                         {
242                                 GatherersApplet.this.stationaryFoodClicked(event);
243                         }
244                 });
245                 settingsPanel.add(stationaryFoodCheckBox);
246
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;
254         }
255
256         /**
257          * The method invoked when the "Number of Red Agents" slider changes
258          *
259          * @param event The change event
260          */
261         public void numRedAgentsChanged(final ChangeEvent event)
262         {
263                 numRedAgents = numRedAgentsSlider.getValue();
264                 numRedAgentsLabel.setText("Number of Red Agents: " + numRedAgents);
265         }
266
267         /**
268          * The method invoked when the "Number of Blue Agents" slider changes
269          *
270          * @param event The change event
271          */
272         public void numBlueAgentsChanged(final ChangeEvent event)
273         {
274                 numBlueAgents = numBlueAgentsSlider.getValue();
275                 numBlueAgentsLabel.setText("Number of Blue Agents: " + numBlueAgents);
276         }
277
278         /**
279          * The method invoked when the "Agent Sight" slider changes
280          *
281          * @param event The change event
282          */
283         public void agentSightChanged(final ChangeEvent event)
284         {
285                 agentSight = agentSightSlider.getValue();
286                 agentSightLabel.setText("Agent Sight: " + agentSight);
287         }
288
289         /**
290          * The method invoked when the "Number of Food" slider changes
291          *
292          * @param event The change event
293          */
294         public void numFoodChanged(final ChangeEvent event)
295         {
296                 numFood = numFoodSlider.getValue();
297                 numFoodLabel.setText("Number of Food: " + numFood);
298         }
299
300         /**
301          * The method invoked when the user selects or deselects the "Stationary
302          * Food" check box
303          *
304          * @param event The action event
305          */
306         public void stationaryFoodClicked(final ActionEvent event)
307         {
308                 stationaryFood = stationaryFoodCheckBox.isSelected();
309         }
310 }