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 private Coordinate coord; 024 private double radius; 025 private STYLE markerStyle; 026 027 /** 028 * Constructs a new {@code MapMarkerCircle}. 029 * @param coord Coordinates of the map marker 030 * @param radius Radius of the map marker position 031 */ 032 public MapMarkerCircle(Coordinate coord, double radius) { 033 this(null, null, coord, radius); 034 } 035 036 /** 037 * Constructs a new {@code MapMarkerCircle}. 038 * @param name Name of the map marker 039 * @param coord Coordinates of the map marker 040 * @param radius Radius of the map marker position 041 */ 042 public MapMarkerCircle(String name, Coordinate coord, double radius) { 043 this(null, name, coord, radius); 044 } 045 046 /** 047 * Constructs a new {@code MapMarkerCircle}. 048 * @param layer Layer of the map marker 049 * @param coord Coordinates of the map marker 050 * @param radius Radius of the map marker position 051 */ 052 public MapMarkerCircle(Layer layer, Coordinate coord, double radius) { 053 this(layer, null, coord, radius); 054 } 055 056 /** 057 * Constructs a new {@code MapMarkerCircle}. 058 * @param lat Latitude of the map marker 059 * @param lon Longitude of the map marker 060 * @param radius Radius of the map marker position 061 */ 062 public MapMarkerCircle(double lat, double lon, double radius) { 063 this(null, null, new Coordinate(lat, lon), radius); 064 } 065 066 /** 067 * Constructs a new {@code MapMarkerCircle}. 068 * @param layer Layer of the map marker 069 * @param lat Latitude of the map marker 070 * @param lon Longitude of the map marker 071 * @param radius Radius of the map marker position 072 */ 073 public MapMarkerCircle(Layer layer, double lat, double lon, double radius) { 074 this(layer, null, new Coordinate(lat, lon), radius); 075 } 076 077 /** 078 * Constructs a new {@code MapMarkerCircle}. 079 * @param layer Layer of the map marker 080 * @param name Name of the map marker 081 * @param coord Coordinates of the map marker 082 * @param radius Radius of the map marker position 083 */ 084 public MapMarkerCircle(Layer layer, String name, Coordinate coord, double radius) { 085 this(layer, name, coord, radius, STYLE.VARIABLE, getDefaultStyle()); 086 } 087 088 /** 089 * Constructs a new {@code MapMarkerCircle}. 090 * @param layer Layer of the map marker 091 * @param name Name of the map marker 092 * @param coord Coordinates of the map marker 093 * @param radius Radius of the map marker position 094 * @param markerStyle Marker style (fixed or variable) 095 * @param style Graphical style 096 */ 097 public MapMarkerCircle(Layer layer, String name, Coordinate coord, double radius, STYLE markerStyle, Style style) { 098 super(layer, name, style); 099 this.markerStyle = markerStyle; 100 this.coord = coord; 101 this.radius = radius; 102 } 103 104 @Override 105 public Coordinate getCoordinate() { 106 return coord; 107 } 108 109 @Override 110 public double getLat() { 111 return coord.getLat(); 112 } 113 114 @Override 115 public double getLon() { 116 return coord.getLon(); 117 } 118 119 @Override 120 public double getRadius() { 121 return radius; 122 } 123 124 @Override 125 public STYLE getMarkerStyle() { 126 return markerStyle; 127 } 128 129 @Override 130 public void paint(Graphics g, Point position, int radius) { 131 int sizeH = radius; 132 int size = sizeH * 2; 133 134 if (g instanceof Graphics2D && getBackColor() != null) { 135 Graphics2D g2 = (Graphics2D) g; 136 Composite oldComposite = g2.getComposite(); 137 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); 138 g2.setPaint(getBackColor()); 139 g.fillOval(position.x - sizeH, position.y - sizeH, size, size); 140 g2.setComposite(oldComposite); 141 } 142 g.setColor(getColor()); 143 g.drawOval(position.x - sizeH, position.y - sizeH, size, size); 144 145 if (getLayer() == null || getLayer().isVisibleTexts()) paintText(g, position); 146 } 147 148 public static Style getDefaultStyle() { 149 return new Style(Color.ORANGE, new Color(200, 200, 200, 200), null, getDefaultFont()); 150 } 151 152 @Override 153 public String toString() { 154 return "MapMarker at " + getLat() + ' ' + getLon(); 155 } 156 157 @Override 158 public void setLat(double lat) { 159 if (coord == null) coord = new Coordinate(lat, 0); 160 else coord.setLat(lat); 161 } 162 163 @Override 164 public void setLon(double lon) { 165 if (coord == null) coord = new Coordinate(0, lon); 166 else coord.setLon(lon); 167 } 168}