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