-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogCanvas.java
executable file
·162 lines (130 loc) · 6.24 KB
/
DialogCanvas.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
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
import javax.swing.*; // for all the Swing stuff (JWhatevers)
import java.awt.*; // for Graphics, Graphics2D
import java.awt.geom.*; // for Rectangle2D
import java.util.ArrayList; // for ArrayList
public class DialogCanvas extends JPanel
{
// provide the (x,y) locations for the corners of the rectangle
// that comprise the graph
private final int GRAPH_UPPER_LEFT_X = 75;
private final int GRAPH_UPPER_LEFT_Y = 25;
private final int GRAPH_LOWER_RIGHT_X = 475;
private final int GRAPH_LOWER_RIGHT_Y = 325;
private double horizontalStep; // one step along horizontal, scaled to max
private double verticalStep; // one step along vertical, scaled to max
private TimeSeriesDialog dialog; // the containing JFrame
public DialogCanvas(TimeSeriesDialog container)
{
dialog = container;
// determine what one step in the horizontal and vertical directions
// means by taking the total distance and dividing it by the maximum
// associated with that direction
horizontalStep = (GRAPH_LOWER_RIGHT_X - GRAPH_UPPER_LEFT_X)
/ (double) dialog.maxTime;
verticalStep = (GRAPH_LOWER_RIGHT_Y - GRAPH_UPPER_LEFT_Y)
/ (double) dialog.maxAgents;
}
//======================================================================
//* public void paintComponent(Graphics g)
//* What happens whenever the time series window is (re)drawn.
//======================================================================
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();
graphics.clearRect(0, 0, dialog.WINDOW_WIDTH, dialog.WINDOW_HEIGHT);
drawTimeSeries(graphics);
drawAxes(graphics);
revalidate();
// get rid of the graphics copy
graphics.dispose();
} // end paintComponent()
//======================================================================
//* private void drawTimeSeries(Graphics2D graphics)
//* This method draws time-series curves by plotting three 2x2
//* rectangles at each integer time point: a green one to indicate
//* the number of macrophages; a red one to indicate
//* the number of bacteria.
//======================================================================
private void drawTimeSeries(Graphics2D graphics)
{
// first draw the macrophages
graphics.setPaint(Color.green);
for (int i = 0; i < dialog.macrophagesCounts.size(); i++)
{
graphics.fillRect(
GRAPH_UPPER_LEFT_X + (int)((i + 1) * horizontalStep),
GRAPH_LOWER_RIGHT_Y -
(int)(dialog.macrophagesCounts.get(i).intValue() * verticalStep),
2, 2 ); // a 2x2 rectangle
}
// then draw the bacteria
graphics.setPaint(Color.red);
for (int i = 0; i < dialog.bacteriaCounts.size(); i++)
{
graphics.fillRect(
GRAPH_UPPER_LEFT_X + (int)((i + 1) * horizontalStep),
GRAPH_LOWER_RIGHT_Y -
(int)(dialog.bacteriaCounts.get(i).intValue() * verticalStep),
2, 2 ); // a 2x2 rectangle
}
} // end drawTimeSeries()
//======================================================================
//* private void drawAxes(Graphics2D graphics)
//* This method draws the vertical and horizontal axes, tick marks,
//* and labels for the time series graphic.
//======================================================================
private void drawAxes(Graphics2D graphics)
{
graphics.setPaint(Color.black);
// the left axis
graphics.drawLine( GRAPH_UPPER_LEFT_X, GRAPH_UPPER_LEFT_Y,
GRAPH_UPPER_LEFT_X, GRAPH_LOWER_RIGHT_Y );
// the right axis
graphics.drawLine( GRAPH_UPPER_LEFT_X, GRAPH_LOWER_RIGHT_Y,
GRAPH_LOWER_RIGHT_X, GRAPH_LOWER_RIGHT_Y );
// the static labels etc.
graphics.drawString( "Agents",
10,
GRAPH_UPPER_LEFT_Y + (GRAPH_LOWER_RIGHT_Y - GRAPH_UPPER_LEFT_Y) / 2 );
graphics.drawString( "0",
GRAPH_UPPER_LEFT_X - 10,
GRAPH_LOWER_RIGHT_Y + 10 );
graphics.drawString( "Time",
GRAPH_UPPER_LEFT_X + (GRAPH_LOWER_RIGHT_X - GRAPH_UPPER_LEFT_X) / 2,
GRAPH_LOWER_RIGHT_Y + 30 );
// put label for max number of agents near upper left
FontMetrics font = graphics.getFontMetrics();
String label = String.valueOf( dialog.maxAgents );
Rectangle2D rect = font.getStringBounds(label, graphics);
graphics.drawString( label,
GRAPH_UPPER_LEFT_X - 10 - (int)(rect.getWidth()),
GRAPH_UPPER_LEFT_Y + ((int)(rect.getHeight()) / 2) );
// put label for max time near upper left
label = String.valueOf( dialog.maxTime );
rect = font.getStringBounds(label, graphics);
graphics.drawString( label,
GRAPH_LOWER_RIGHT_X + 10,
GRAPH_LOWER_RIGHT_Y + ((int)(rect.getHeight()) / 2) );
// put tick marks on the left axis -- ten of 'em each just for estimating
int tickSeparation = (GRAPH_LOWER_RIGHT_Y - GRAPH_UPPER_LEFT_Y) / 10;
for (int i = 0; i < 10; i++)
{
graphics.drawLine( GRAPH_UPPER_LEFT_X - 5,
GRAPH_UPPER_LEFT_Y + (i * tickSeparation),
GRAPH_UPPER_LEFT_X,
GRAPH_UPPER_LEFT_Y + (i * tickSeparation) );
}
// put tick marks on the left axis -- ten of 'em each just for estimating
tickSeparation = (GRAPH_LOWER_RIGHT_X - GRAPH_UPPER_LEFT_X) / 10;
for (int i = 0; i < 10; i++)
{
graphics.drawLine( GRAPH_LOWER_RIGHT_X - (i * tickSeparation),
GRAPH_LOWER_RIGHT_Y,
GRAPH_LOWER_RIGHT_X - (i * tickSeparation),
GRAPH_LOWER_RIGHT_Y + 5 );
}
} // end drawAxes()
}