-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentGUI.java
executable file
·77 lines (66 loc) · 3.33 KB
/
AgentGUI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import javax.swing.*; // for all the JWhatevers
import java.awt.*; // for BorderLayout
import java.util.Random;
/**
* This class implements the GUI window for our agent-based simulation,
* specifically drawing the grid and (eventually) drawing the agents
* that will run rampant thereupon.
*/
class AgentGUI extends WindowManager
{
private SimulationManager simulation; // a reference to the simulation object
private AgentCanvas canvas; // for drawing the agents
private TimeSeriesDialog dialog; // for drawing time series
private int lastUpdateTime; // for updating dialog
/**************************************************************************
* Constructor for the agent GUI window.
*
* @param theSimulation a SimulationManager reference to the simulation object
* @param gridSize number of rows (same as columns) in the environment
* @param guiCellWidth width of each cell drawn in the gui
**************************************************************************/
public AgentGUI(SimulationManager theSimulation, int gridSize, int guiCellWidth)
{
// call the WindowManager constructor, and add a BorderLayout
super("Agent-Based Simulation", 600, 600);
this.setLayout(new BorderLayout());
// hang on to a reference to the SimulationManager object
this.simulation = theSimulation;
// create the AgentCanvas for drawing, and then add to window's center
this.canvas = new AgentCanvas(theSimulation,
gridSize, gridSize, guiCellWidth);
this.add(new JScrollPane(this.canvas), BorderLayout.CENTER);
int maxAgents = gridSize * gridSize;
this.dialog = new TimeSeriesDialog(maxAgents, (int)theSimulation.getMaxTime());
this.lastUpdateTime = 0;
}
/**************************************************************************
* Accessor method returning the number of rows (same as columns) in the
* environment.
* @return an integer representing the number of rows (columns) in the environment
**************************************************************************/
public int getGridSize() { return(canvas.getGridWidth()); }
/**************************************************************************
* Method to redraw the canvas in the GUI window. This method calls the
* updateGrid() method in AgentCanvas which updates the GUI landsacpe by
* querying the simulation object.
*
* @param delayInSecs a double representing the delay between draws
**************************************************************************/
public void update(double delayInSecs) throws InterruptedException
{
// update the main GUI
canvas.updateGrid();
long msecs = (long)(delayInSecs * 1000);
Thread.sleep(msecs);
// and update the time series dialog -- only on integer time steps
double t = simulation.getTime();
System.out.printf("AgentGUI: t=%.2f, lastUpdateTime = %d%n",t,lastUpdateTime);
if ((int)t > lastUpdateTime)
{
this.dialog.updateCounts(simulation.getNumMacrophages(),
simulation.getNumBacteria());
lastUpdateTime++;
}
}
}