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