001// License: GPL. For details, see Readme.txt file. 002package org.openstreetmap.gui.jmapviewer; 003 004import java.awt.AlphaComposite; 005import java.awt.BasicStroke; 006import java.awt.Color; 007import java.awt.Composite; 008import java.awt.Graphics; 009import java.awt.Graphics2D; 010import java.awt.Point; 011import java.awt.Polygon; 012import java.awt.Rectangle; 013import java.awt.Stroke; 014import java.util.Arrays; 015import java.util.List; 016 017import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate; 018import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon; 019 020public class MapPolygonImpl extends MapObjectImpl implements MapPolygon { 021 022 private List<? extends ICoordinate> points; 023 024 public MapPolygonImpl(ICoordinate... points) { 025 this(null, null, points); 026 } 027 028 public MapPolygonImpl(List<? extends ICoordinate> points) { 029 this(null, null, points); 030 } 031 032 public MapPolygonImpl(String name, List<? extends ICoordinate> points) { 033 this(null, name, points); 034 } 035 036 public MapPolygonImpl(String name, ICoordinate... points) { 037 this(null, name, points); 038 } 039 040 public MapPolygonImpl(Layer layer, List<? extends ICoordinate> points) { 041 this(layer, null, points); 042 } 043 044 public MapPolygonImpl(Layer layer, String name, List<? extends ICoordinate> points) { 045 this(layer, name, points, getDefaultStyle()); 046 } 047 048 public MapPolygonImpl(Layer layer, String name, ICoordinate... points) { 049 this(layer, name, Arrays.asList(points), getDefaultStyle()); 050 } 051 052 public MapPolygonImpl(Layer layer, String name, List<? extends ICoordinate> points, Style style) { 053 super(layer, name, style); 054 this.points = points; 055 } 056 057 @Override 058 public List<? extends ICoordinate> getPoints() { 059 return this.points; 060 } 061 062 @Override 063 public void paint(Graphics g, List<Point> points) { 064 Polygon polygon = new Polygon(); 065 for (Point p : points) { 066 polygon.addPoint(p.x, p.y); 067 } 068 paint(g, polygon); 069 } 070 071 @Override 072 public void paint(Graphics g, Polygon polygon) { 073 // Prepare graphics 074 Color oldColor = g.getColor(); 075 g.setColor(getColor()); 076 077 Stroke oldStroke = null; 078 if (g instanceof Graphics2D) { 079 Graphics2D g2 = (Graphics2D) g; 080 oldStroke = g2.getStroke(); 081 g2.setStroke(getStroke()); 082 } 083 // Draw 084 g.drawPolygon(polygon); 085 if (g instanceof Graphics2D && getBackColor() != null) { 086 Graphics2D g2 = (Graphics2D) g; 087 Composite oldComposite = g2.getComposite(); 088 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); 089 g2.setPaint(getBackColor()); 090 g2.fillPolygon(polygon); 091 g2.setComposite(oldComposite); 092 } 093 // Restore graphics 094 g.setColor(oldColor); 095 if (g instanceof Graphics2D) { 096 ((Graphics2D) g).setStroke(oldStroke); 097 } 098 Rectangle rec = polygon.getBounds(); 099 Point corner = rec.getLocation(); 100 Point p = new Point(corner.x+(rec.width/2), corner.y+(rec.height/2)); 101 if (getLayer() == null || getLayer().isVisibleTexts()) paintText(g, p); 102 } 103 104 public static Style getDefaultStyle() { 105 return new Style(Color.BLUE, new Color(100, 100, 100, 50), new BasicStroke(2), getDefaultFont()); 106 } 107 108 @Override 109 public String toString() { 110 return "MapPolygon [points=" + points + ']'; 111 } 112}