-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentGUI.java
More file actions
executable file
·57 lines (51 loc) · 2.57 KB
/
AgentGUI.java
File metadata and controls
executable file
·57 lines (51 loc) · 2.57 KB
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
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
/**************************************************************************
* 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);
}
/**************************************************************************
* 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
{
canvas.updateGrid();
long msecs = (long)(delayInSecs * 1000);
Thread.sleep(msecs);
}
}