001    package aima.gui.framework;
002    
003    import java.awt.Color;
004    
005    import aima.search.map.Point2D;
006    
007    /**
008     * Simple agent view without content but with some useful transformation
009     * features. The transformation features allow to scale and translate 2D world
010     * coordinates into view coordinates. When creating subclasses, it should
011     * normally be sufficient to override the method
012     * {@link #paint(java.awt.Graphics)}.
013     * 
014     * @author R. Lunde
015     */
016    public class AgentView extends AgentAppFrame.AbstractAgentView {
017            /**
018             * Maintains a reference to the model which provides the data to be
019             * displayed.
020             */
021            protected AgentAppModel model;
022    
023            private int borderTop = 10;
024            private int borderLeft = 10;
025            private int borderBottom = 10;
026            private int borderRight = 10;
027    
028            private double offsetX;
029            private double offsetY;
030            private double scale;
031    
032            /**
033             * Specifies the number of pixels left blank on each side of the agent view
034             * panel.
035             */
036            public void setBorder(int top, int left, int bottom, int right) {
037                    borderTop = top;
038                    borderLeft = left;
039                    borderBottom = bottom;
040                    borderRight = right;
041            }
042    
043            /**
044             * Specifies a bounding box in world coordinates. The resulting
045             * transformation is able to display everything within this bounding box
046             * without scrolling.
047             */
048            public void adjustTransformation(double minXW, double minYW, double maxXW,
049                            double maxYW) {
050                    // adjust coordinates relative to the left upper corner of the graph
051                    // area
052                    double scaleX = 1f;
053                    double scaleY = 1f;
054                    if (maxXW > minXW)
055                            scaleX = (getWidth() - borderLeft - borderRight) / (maxXW - minXW);
056                    if (maxYW > minYW)
057                            scaleY = (getHeight() - borderTop - borderBottom) / (maxYW - minYW);
058                    offsetX = -minXW;
059                    offsetY = -minYW;
060                    scale = Math.min(scaleX, scaleY);
061            }
062    
063            /** Returns the x_view of a given point in world coordinates. */
064            protected int x(Point2D xyW) {
065                    return x(xyW.getX());
066            }
067    
068            /** Returns the y_view of a given point in world coordinates. */
069            protected int y(Point2D xyW) {
070                    return y(xyW.getY());
071            }
072    
073            /** Returns the x_view of a given x-value in world coordinates. */
074            protected int x(double xW) {
075                    return (int) Math.round(scale * (xW + offsetX) + borderLeft);
076            }
077    
078            /** Returns the y_view of a given y-value in world coordinates. */
079            protected int y(double yW) {
080                    return (int) Math.round(scale * (yW + offsetY) + borderTop);
081            }
082    
083            /** Transforms a given world length into view length. */
084            protected int scale(int length) {
085                    return (int) Math.round(scale * length);
086            }
087    
088            /** Stores the model and initiates painting. */
089            @Override
090            public void updateView(AgentAppModel model) {
091                    this.model = model;
092                    if (model != null)
093                            repaint();
094            }
095    
096            /**
097             * Shows a graphical representation of the agent in its environment.
098             * Override this dummy implementation to get a useful view of the agent!
099             */
100            @Override
101            public void paint(java.awt.Graphics g) {
102                    java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
103                    g2.setBackground(Color.white);
104                    g2.clearRect(0, 0, getWidth(), getHeight());
105            }
106    }