-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgentCanvas.java
More file actions
executable file
·332 lines (283 loc) · 14.5 KB
/
AgentCanvas.java
File metadata and controls
executable file
·332 lines (283 loc) · 14.5 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import javax.swing.*; // for all the Swing stuff (JWhatevers)
import java.util.*; // for ArrayList
import java.awt.*; // for Graphics, Graphics2D
import java.awt.event.*; // for MouseListener
import java.awt.geom.*; // for Rectangle2D
/**
* This class implements the agent canvas for our agent-based simulation,
* specifically drawing the grid and (eventually) drawing the agents
* that will run rampant thereupon. A canvas must appear in the main GUI
* window (JFrame) -- see AgentGUI.
*/
class AgentCanvas extends JPanel implements MouseListener
{
private int viewportX; // where in the viewport to start drawing image/grid
private int viewportY; // (we want the image centered)
private int gridWidth; // width of grid in cells
private int gridHeight; // height of grid in cells
private int cellGUISize; // maximum drawn size of a cell
private SimulationManager simulation; // a reference to the simulation object
/**************************************************************************
//* Constructor for the agent canvas.
*
* @param theSimulation a SimulationManager reference to the simulation
* @param gridRows the number of rows to be drawn in the environment
* @param gridCols the number of cols to be drawn in the environment
* @param cellGUISize the size of a cell, and maximum size of an agent in a cell
**************************************************************************/
public AgentCanvas(SimulationManager theSimulation,
int gridRows, int gridCols, int cellGUISize)
{
this.simulation = theSimulation;
addMouseListener(this); // to handle mouse clicks
this.cellGUISize = cellGUISize;
this.gridHeight = gridRows;
this.gridWidth = gridCols;
updateGrid();
}
/**************************************************************************
* Accessor method returning the grid width (number of columns).
* @return an integer representing the grid width (number of columns)
**************************************************************************/
public int getGridWidth() { return gridWidth; }
/**************************************************************************
* Accessor method returning the grid height (number of rows).
* @return an integer representing the grid height (number of rows)
**************************************************************************/
public int getGridHeight() { return gridHeight; }
/**************************************************************************
* Method to redraw the canvas, simply by call repaint
**************************************************************************/
public void updateGrid()
{
// call repaint to redisplay the new background, then agents & grid
repaint(); // calls the defined paintComponent method
}
/**************************************************************************
* Method implementing everything that should happen when the agent canvas
* is (re)drawn. This method makes use of an ArrayList<AgentInterface>
* via the getListOfAgents() method that any agent class must implement.
*
* @param g a Graphics component corresponding to this JPanel
**************************************************************************/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// safest to create a copy of the graphics component -- one must
// ensure that no changes are made to the original
Graphics2D graphics = (Graphics2D) g.create();
JViewport viewport = null;
JScrollPane scrollPane = null;
Insets borders = null;
int viewportWidth = 0;
int viewportHeight = 0;
int imageWidth = gridWidth * cellGUISize;
int imageHeight = gridHeight * cellGUISize;
// make sure that we're grabbing onto the viewport of the scroll pane
Component ancestor = getParent();
if (ancestor == null || !(ancestor instanceof JViewport))
{
//Exception e = new Exception(
// "AgentCanvas instance must be within JScrollPane instance");
//e.printStackTrace();
//return;
viewportWidth = imageWidth;
viewportHeight = imageHeight;
borders = new Insets(0,0,0,0);
}
else
{
// presumably we have the viewport of scroll pane containing 'this'
viewport = (JViewport) ancestor;
viewportWidth = viewport.getWidth();
viewportHeight = viewport.getHeight();
scrollPane = (JScrollPane) viewport.getParent();
borders = scrollPane.getInsets();
}
// Note that drawImage automatically scales the image to fit that
// rectangle.
int renderWidth = gridWidth * cellGUISize;
int renderHeight = gridHeight * cellGUISize;
// determine the starting (x,y) in the viewport where the image
// will be drawn
viewportX = (int) Math.max((viewportWidth - renderWidth) / 2, 0);
viewportY = (int) Math.max((viewportHeight - renderHeight) / 2, 0);
// in case there was a previous image, clear things out
graphics.clearRect(0, 0, viewportWidth, viewportHeight);
// now draw the agents
ArrayList<AgentInterface> agentsList = simulation.getListOfAgents();
for (int i = 0; i < agentsList.size(); i++)
{
AgentInterface a = agentsList.get(i);
// make sure not to draw any agent outside the image boundaries;
// remember that graphics x corresponds to column and graphics y
// corresponds to row
if ((a.getRow() >= 0) && (a.getCol() >= 0) &&
((a.getRow() * cellGUISize) + cellGUISize <= renderHeight) &&
((a.getCol() * cellGUISize) + cellGUISize <= renderWidth))
{
int guiX = viewportX + (a.getCol() * cellGUISize);
int guiY = viewportY + (a.getRow() * cellGUISize);
if (a.getType() == AgentInterface.AgentType.MACROPHAGE)
{
graphics.setPaint(new Color(0,150,0)); // dark green
graphics.fillRect(guiX, guiY, cellGUISize, cellGUISize);
}
else
{
// draw the bacteria in red, and slightly smaller
graphics.setPaint(Color.red);
int size = (int)(cellGUISize*0.6);
int offset = (int)(cellGUISize*0.2); // 0.2 + 0.6 + 0.2 = 1.0
graphics.fillRect(guiX + offset, guiY + offset, size + 1, size + 1);
}
// draw the agent's ID on top of colored agent box
String agentID = "" + a.getID();
FontMetrics font = graphics.getFontMetrics();
Rectangle2D rect = font.getStringBounds(agentID, graphics);
int textWidth = (int)(rect.getWidth());
int textHeight = (int)(rect.getHeight());
int startX = (cellGUISize - textWidth) / 2;
int startY = (cellGUISize - textHeight) / 2;
graphics.setPaint(Color.white);
// the x,y provided to draw string corresponds to lower LHS;
// hence, add textHeight to the y component
graphics.drawString(agentID, guiX + startX,
guiY + startY + textHeight - 1);
}
}
// draw the grid last so that it will overlay the agent squares
drawGrid(graphics, viewportX, viewportY, renderWidth, renderHeight);
// show the number of infected/uninfected agents
drawAgentInfo(graphics, viewportX, viewportY,
renderWidth, renderHeight, borders);
revalidate();
// get rid of the graphics copy
graphics.dispose();
}
/**************************************************************************
* Method to draw a grid on top of (any) background and agents.
* @param graphics a Graphics2D version of this JPanel
* @param x horizontal position to start drawing from
* @param y vertical position to start drawing from
* @param width width of grid to be drawn
* @param height heightof grid to be drawn
**************************************************************************/
private void drawGrid(Graphics2D graphics, int x, int y,
int width, int height)
{
graphics.setPaint(Color.black);
// the columns
for (int row = 0; row < width / cellGUISize; row++)
graphics.drawLine(x + (row * cellGUISize), y,
x + (row * cellGUISize), y + height - 1);
// the rows
for (int col = 0; col < height / cellGUISize; col++)
graphics.drawLine(x, y + (col * cellGUISize),
x + width - 1, y + (col * cellGUISize));
// the border
graphics.drawLine(x, y, x, y + height);
graphics.drawLine(x, y, x + width, y);
graphics.drawLine(x + width, y, x + width, y + height);
graphics.drawLine(x, y + height, x + width, y + height);
}
//======================================================================
//* private void drawAgentInfo(Graphics2D graphics, int x, int y,
//======================================================================
/**************************************************************************
* Method to draw all the agents in the simulation.
*
* @param graphics a Graphics2D version of this JPanel
* @param x horizontal position to start drawing from
* @param y vertical position to start drawing from
* @param width width of agent to be drawn
* @param height height of agent to be drawn
* @param borders reference to the borders of the container
**************************************************************************/
private void drawAgentInfo(Graphics2D graphics, int x, int y,
int width, int height, Insets borders)
{
final int verticalSpaceBeforeText = 20;
// a string to appear at the bottom listing current counts
String agentInfo = "Macrophages: " + simulation.getNumMacrophages()
+ " " + "Bacteria: " + simulation.getNumBacteria();
// Find the size of string in the font being used by the current
// Graphics2D context.
FontMetrics font = graphics.getFontMetrics();
Rectangle2D rect = font.getStringBounds(agentInfo, graphics);
int textWidth = (int)(rect.getWidth());
int textHeight = (int)(rect.getHeight());
int startStringAt = (width - textWidth) / 2;
// center text horizontally (max sure left side at least draws w/in
// the viewport window -- i.e., x at least 0)
graphics.drawString(agentInfo, Math.max(x + startStringAt, 0),
y + height + verticalSpaceBeforeText);
// Make sure the image plus text (which may be a new one loaded in) is
// visible in the scroll pane. If this isn't somewhere, scrollbars
// won't work in the main screen's encompassing JScrollPane.
setPreferredSize(
new Dimension(
Math.max(width + borders.left + borders.right, textWidth),
height + borders.top + borders.bottom
+ verticalSpaceBeforeText + textHeight));
}
/**************************************************************************
* Method that takes physical (x,y) and converts it to logical (x,y) in the
* context of the agent/grid size. Recall that x and y are reversed with
* respect to GUI and 2D arrays -- i.e., in the GUI, (0,0) appears in the
* bottom left.
*
* @param x the physical x value from a mouse click
* @param y the physical y value from a mouse click
* @return a Point corresponding to the converted (array-logical) coordinate pair
**************************************************************************/
private Point convertToLogical(int x, int y)
{
// convert to next lowest multiple of cellGUISize using int division
int newX = (x - viewportX) / cellGUISize;
int newY = (y - viewportY) / cellGUISize;
return new Point(newX, newY);
}
/**************************************************************************
* Method (currently not used) that can allow for individual inspection of
* an agent appearing at a logical (in array terms) point on the screen.
*
* @param p a Point corresponding to the array-logical conversion of a GUI (x,y) mouse click
**************************************************************************/
private void inspectAgentAt(Point p)
{
/*
// remember canvas x,y are reveresed from row,col
Agent agent = simulation.getAgentAt((int) p.getY(), (int) p.getX());
if (agent != null)
{
// use hash map to see if the window is already created, and if so,
// just focus; otherwise, create a new inspector window
double time = simulation.getTime();
String id = agent.getRow() + "," + agent.getCol() + " " + time;
AgentInspector window = AgentInspector.windowList.get(id);
if (window == null)
{
window = new AgentInspector(agent, time, id);
AgentInspector.windowList.put(id, window);
}
window.setVisible(true);
}
*/
}
//************************************************************
//* The following methods are for implementing MouseListener.
//* We're really only interested in mouseClicked().
//************************************************************
public void mouseClicked(MouseEvent e)
{
// grab the physical (x,y) of the mouse click, convert to
// "array logical", and then inspect the agent there...
Point p = convertToLogical(e.getX(), e.getY());
inspectAgentAt(p);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}