001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.gui.jmapviewer;
003    
004    import java.awt.BasicStroke;
005    import java.awt.Color;
006    import java.awt.Graphics;
007    import java.awt.Graphics2D;
008    import java.awt.Point;
009    import java.awt.Stroke;
010    
011    import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle;
012    
013    /**
014     * @author Vincent
015     *
016     */
017    public class MapRectangleImpl implements MapRectangle {
018    
019        private Coordinate topLeft;
020        private Coordinate bottomRight;
021        private Color color;
022        private Stroke stroke;
023    
024        public MapRectangleImpl(Coordinate topLeft, Coordinate bottomRight) {
025            this(topLeft, bottomRight, Color.BLUE, new BasicStroke(2));
026        }
027    
028        public MapRectangleImpl(Coordinate topLeft, Coordinate bottomRight, Color color, Stroke stroke) {
029            this.topLeft = topLeft;
030            this.bottomRight = bottomRight;
031            this.color = color;
032            this.stroke = stroke;
033        }
034    
035        /* (non-Javadoc)
036         * @see org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle#getTopLeft()
037         */
038        @Override
039        public Coordinate getTopLeft() {
040            return topLeft;
041        }
042    
043        /* (non-Javadoc)
044         * @see org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle#getBottomRight()
045         */
046        @Override
047        public Coordinate getBottomRight() {
048            return bottomRight;
049        }
050    
051        /* (non-Javadoc)
052         * @see org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle#paint(java.awt.Graphics, java.awt.Point, java.awt.Point)
053         */
054        @Override
055        public void paint(Graphics g, Point topLeft, Point bottomRight) {
056            // Prepare graphics
057            Color oldColor = g.getColor();
058            g.setColor(color);
059            Stroke oldStroke = null;
060            if (g instanceof Graphics2D) {
061                Graphics2D g2 = (Graphics2D) g;
062                oldStroke = g2.getStroke();
063                g2.setStroke(stroke);
064            }
065            // Draw
066            g.drawRect(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
067            // Restore graphics
068            g.setColor(oldColor);
069            if (g instanceof Graphics2D) {
070                ((Graphics2D) g).setStroke(oldStroke);
071            }
072        }
073    
074        @Override
075        public String toString() {
076            return "MapRectangle from " + topLeft + " to " + bottomRight;
077        }
078    }