22
33import java .awt .Color ;
44
5-
65/**
7- * Simple agent view without content but with some useful transformation features.
8- * The transformation features allow to scale and translate 2D world coordinates
9- * into view coordinates. When creating subclasses, it should normally be
10- * sufficient to override the method {@link #paint(java.awt.Graphics)}.
6+ * Simple agent view without content but with some useful transformation
7+ * features. The transformation features allow to scale and translate 2D world
8+ * coordinates into view coordinates. When creating subclasses, it should
9+ * normally be sufficient to override the method
10+ * {@link #paint(java.awt.Graphics)}.
11+ *
1112 * @author R. Lunde
1213 */
1314public class AgentView extends AgentAppFrame .AbstractAgentView {
14- /** Maintains a reference to the model which provides the data to be displayed. */
15+ /**
16+ * Maintains a reference to the model which provides the data to be
17+ * displayed.
18+ */
1519 protected AgentAppModel model ;
16-
20+
1721 private int borderTop = 10 ;
1822 private int borderLeft = 10 ;
1923 private int borderBottom = 10 ;
2024 private int borderRight = 10 ;
21-
25+
2226 private double offsetX ;
2327 private double offsetY ;
2428 private double scale ;
25-
29+
2630 /**
27- * Specifies the number of pixels left blank on each side of the
28- * agent view panel.
31+ * Specifies the number of pixels left blank on each side of the agent view
32+ * panel.
2933 */
3034 public void setBorder (int top , int left , int bottom , int right ) {
3135 borderTop = top ;
3236 borderLeft = left ;
3337 borderBottom = bottom ;
3438 borderRight = right ;
3539 }
40+
3641 /**
3742 * Specifies a bounding box in world coordinates. The resulting
38- * transformation is able to display everything within this
39- * bounding box without scrolling.
43+ * transformation is able to display everything within this bounding box
44+ * without scrolling.
4045 */
41- public void adjustTransformation (double minXW , double minYW , double maxXW , double maxYW ) {
42- // adjust coordinates relative to the left upper corner of the graph area
43- double scaleX = 1f ;
44- double scaleY = 1f ;
45- if (maxXW > minXW )
46- scaleX = (getWidth ()-borderLeft -borderRight ) / (maxXW -minXW );
47- if (maxYW > minYW )
48- scaleY = (getHeight ()-borderTop -borderBottom ) / (maxYW -minYW );
49- offsetX = -minXW ;
50- offsetY = -minYW ;
51- scale = Math .min (scaleX , scaleY );
46+ public void adjustTransformation (double minXW , double minYW , double maxXW ,
47+ double maxYW ) {
48+ // adjust coordinates relative to the left upper corner of the graph
49+ // area
50+ double scaleX = 1f ;
51+ double scaleY = 1f ;
52+ if (maxXW > minXW )
53+ scaleX = (getWidth () - borderLeft - borderRight ) / (maxXW - minXW );
54+ if (maxYW > minYW )
55+ scaleY = (getHeight () - borderTop - borderBottom ) / (maxYW - minYW );
56+ offsetX = -minXW ;
57+ offsetY = -minYW ;
58+ scale = Math .min (scaleX , scaleY );
5259 }
53-
60+
5461 /** Returns the x_view of a given point in world coordinates. */
55- protected int x (double [] xyW ) { return x (xyW [0 ]); }
62+ protected int x (double [] xyW ) {
63+ return x (xyW [0 ]);
64+ }
65+
5666 /** Returns the y_view of a given point in world coordinates. */
57- protected int y (double [] xyW ) { return y (xyW [1 ]); }
67+ protected int y (double [] xyW ) {
68+ return y (xyW [1 ]);
69+ }
70+
5871 /** Returns the x_view of a given x-value in world coordinates. */
59- protected int x (double xW ) { return (int ) Math .round (scale * (xW + offsetX ) + borderLeft ); }
72+ protected int x (double xW ) {
73+ return (int ) Math .round (scale * (xW + offsetX ) + borderLeft );
74+ }
75+
6076 /** Returns the y_view of a given y-value in world coordinates. */
61- protected int y (double yW ) { return (int ) Math .round (scale * (yW + offsetY ) + borderTop ); }
77+ protected int y (double yW ) {
78+ return (int ) Math .round (scale * (yW + offsetY ) + borderTop );
79+ }
80+
6281 /** Transforms a given world length into view length. */
63- protected int scale (int length ) { return (int ) Math .round (scale * length ); }
64-
82+ protected int scale (int length ) {
83+ return (int ) Math .round (scale * length );
84+ }
85+
6586 /** Stores the model and initiates painting. */
87+ @ Override
6688 public void updateView (AgentAppModel model ) {
6789 this .model = model ;
6890 if (model != null )
6991 repaint ();
7092 }
71-
93+
7294 /**
7395 * Shows a graphical representation of the agent in its environment.
7496 * Override this dummy implementation to get a useful view of the agent!
7597 */
98+ @ Override
7699 public void paint (java .awt .Graphics g ) {
77- java .awt .Graphics2D g2 = (java .awt .Graphics2D ) g ;
78- g2 .setColor (Color .white );
79- g2 .fillRect (0 , 0 , getWidth (), getHeight ());
100+ java .awt .Graphics2D g2 = (java .awt .Graphics2D ) g ;
101+ g2 .setBackground (Color .white );
102+ g2 .clearRect (0 , 0 , getWidth (), getHeight ());
80103 }
81104}
0 commit comments