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    public MapPolygonImpl(List<? extends ICoordinate> points) {
028        this(null, null, points);
029    }
030    public MapPolygonImpl(String name, List<? extends ICoordinate> points) {
031        this(null, name, points);
032    }
033    public MapPolygonImpl(String name, ICoordinate ... points) {
034        this(null, name, points);
035    }
036    public MapPolygonImpl(Layer layer, List<? extends ICoordinate> points) {
037        this(layer, null, points);
038    }
039    public MapPolygonImpl(Layer layer, String name, List<? extends ICoordinate> points) {
040        this(layer, name, points, getDefaultStyle());
041    }
042    public MapPolygonImpl(Layer layer, String name, ICoordinate ... points) {
043        this(layer, name, Arrays.asList(points), getDefaultStyle());
044    }
045    public MapPolygonImpl(Layer layer, String name, List<? extends ICoordinate> points, Style style) {
046        super(layer, name, style);
047        this.points = points;
048    }
049
050    @Override
051    public List<? extends ICoordinate> getPoints() {
052        return this.points;
053    }
054
055    @Override
056    public void paint(Graphics g, List<Point> points) {
057        Polygon polygon = new Polygon();
058        for (Point p : points) {
059            polygon.addPoint(p.x, p.y);
060        }
061        paint(g, polygon);
062    }
063
064    @Override
065    public void paint(Graphics g, Polygon polygon) {
066        // Prepare graphics
067        Color oldColor = g.getColor();
068        g.setColor(getColor());
069
070        Stroke oldStroke = null;
071        if (g instanceof Graphics2D) {
072            Graphics2D g2 = (Graphics2D) g;
073            oldStroke = g2.getStroke();
074            g2.setStroke(getStroke());
075        }
076        // Draw
077        g.drawPolygon(polygon);
078        if (g instanceof Graphics2D && getBackColor()!=null) {
079            Graphics2D g2 = (Graphics2D) g;
080            Composite oldComposite = g2.getComposite();
081            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
082            g2.setPaint(getBackColor());
083            g2.fillPolygon(polygon);
084            g2.setComposite(oldComposite);
085        }
086        // Restore graphics
087        g.setColor(oldColor);
088        if (g instanceof Graphics2D) {
089            ((Graphics2D) g).setStroke(oldStroke);
090        }
091        Rectangle rec = polygon.getBounds();
092        Point corner = rec.getLocation();
093        Point p= new Point(corner.x+(rec.width/2), corner.y+(rec.height/2));
094        if(getLayer()==null||getLayer().isVisibleTexts()) paintText(g, p);
095    }
096
097    public static Style getDefaultStyle(){
098        return new Style(Color.BLUE, new Color(100,100,100,50), new BasicStroke(2), getDefaultFont());
099    }
100
101    @Override
102    public String toString() {
103        return "MapPolygon [points=" + points + "]";
104    }
105}