001// License: GPL. For details, see Readme.txt file. 002package org.openstreetmap.gui.jmapviewer; 003 004import java.awt.AlphaComposite; 005import java.awt.Color; 006import java.awt.Composite; 007import java.awt.Graphics; 008import java.awt.Graphics2D; 009import java.awt.Point; 010 011import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 012 013/** 014 * A simple implementation of the {@link MapMarker} interface. Each map marker 015 * is painted as a circle with a black border line and filled with a specified 016 * color. 017 * 018 * @author Jan Peter Stotz 019 * 020 */ 021public class MapMarkerCircle extends MapObjectImpl implements MapMarker { 022 023 Coordinate coord; 024 double radius; 025 STYLE markerStyle; 026 027 public MapMarkerCircle(Coordinate coord, double radius) { 028 this(null, null, coord, radius); 029 } 030 public MapMarkerCircle(String name, Coordinate coord, double radius) { 031 this(null, name, coord, radius); 032 } 033 public MapMarkerCircle(Layer layer, Coordinate coord, double radius) { 034 this(layer, null, coord, radius); 035 } 036 public MapMarkerCircle(double lat, double lon, double radius) { 037 this(null, null, new Coordinate(lat,lon), radius); 038 } 039 public MapMarkerCircle(Layer layer, double lat, double lon, double radius) { 040 this(layer, null, new Coordinate(lat,lon), radius); 041 } 042 public MapMarkerCircle(Layer layer, String name, Coordinate coord, double radius) { 043 this(layer, name, coord, radius, STYLE.VARIABLE, getDefaultStyle()); 044 } 045 public MapMarkerCircle(Layer layer, String name, Coordinate coord, double radius, STYLE markerStyle, Style style) { 046 super(layer, name, style); 047 this.markerStyle = markerStyle; 048 this.coord = coord; 049 this.radius = radius; 050 } 051 052 public Coordinate getCoordinate(){ 053 return coord; 054 } 055 public double getLat() { 056 return coord.getLat(); 057 } 058 059 public double getLon() { 060 return coord.getLon(); 061 } 062 063 public double getRadius() { 064 return radius; 065 } 066 067 public STYLE getMarkerStyle() { 068 return markerStyle; 069 } 070 071 public void paint(Graphics g, Point position, int radio) { 072 int size_h = radio; 073 int size = size_h * 2; 074 075 if (g instanceof Graphics2D && getBackColor()!=null) { 076 Graphics2D g2 = (Graphics2D) g; 077 Composite oldComposite = g2.getComposite(); 078 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); 079 g2.setPaint(getBackColor()); 080 g.fillOval(position.x - size_h, position.y - size_h, size, size); 081 g2.setComposite(oldComposite); 082 } 083 g.setColor(getColor()); 084 g.drawOval(position.x - size_h, position.y - size_h, size, size); 085 086 if(getLayer()==null||getLayer().isVisibleTexts()) paintText(g, position); 087 } 088 089 public static Style getDefaultStyle(){ 090 return new Style(Color.ORANGE, new Color(200,200,200,200), null, getDefaultFont()); 091 } 092 @Override 093 public String toString() { 094 return "MapMarker at " + getLat() + " " + getLon(); 095 } 096 @Override 097 public void setLat(double lat) { 098 if(coord==null) coord = new Coordinate(lat,0); 099 else coord.setLat(lat); 100 } 101 @Override 102 public void setLon(double lon) { 103 if(coord==null) coord = new Coordinate(0,lon); 104 else coord.setLon(lon); 105 } 106}