JMapViewer绘制一个看起来像箭头的多边形

时间:2014-03-04 00:02:46

标签: java google-maps jmapviewer

我想在地图上创建一个箭头(---->),连接两个给定的坐标。

到目前为止,我使用了建议herehere

的多边形实现的绘制线

有没有办法让它变形?

谢谢, Ozrad

2 个答案:

答案 0 :(得分:1)

一般情况下,您可以使用Java make a directed line and make it movemouse motion listener only in one direction中展示的概念来表达某些想法。

这两者都使用Path2D生成形状并计算两点之间的角度以定向形状。

您的要求实际上要容易一些,因为您已经有了一个起点和终点,因此应该可以计算线的长度或箭头的角度,具体取决于哪个更简单(我会计算点之间的直线长度并旋转它,但那只是我)

答案 1 :(得分:0)

基于@MadProgrammer帮助的完整解决方案是:

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Path2D;
import java.util.List;
import java.awt.Graphics;

import org.openstreetmap.gui.jmapviewer.MapPolygonImpl;
import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;

public class MyMapMarkerArrow extends MapPolygonImpl {

    public MyMapMarkerArrow(List<? extends ICoordinate> points) {
        super(null, null, points);
    }

    @Override
    public void paint(Graphics g, List<Point> points) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(getColor());
        g2d.setStroke(getStroke());
        Path2D path = buildPath(points);
        g2d.draw(path);
        g2d.dispose();
    }

    private Path2D buildPath(List<Point> points) {
        Path2D path = new Path2D.Double();
        if (points != null && points.size() > 0) {
            Point firstPoint = points.get(0);
            path.moveTo(firstPoint.getX(), firstPoint.getY());
            for (Point p : points) {
                path.lineTo(p.getX(), p.getY());
            }

            int pointsSize = points.size() - 1;

            if (points.get(0).getY() > points.get(1).getY()) {
                path.lineTo(points.get(pointsSize).getX(),
                        points.get(pointsSize).getY() + 20);

                path.moveTo(points.get(pointsSize).getX(),
                        points.get(pointsSize).getY());

                path.lineTo(points.get(pointsSize).getX() - 20,
                        points.get(pointsSize).getY());
            } else {
                path.lineTo(points.get(pointsSize).getX(),
                        points.get(pointsSize).getY() - 20);

                path.moveTo(points.get(pointsSize).getX(),
                        points.get(pointsSize).getY());

                path.lineTo(points.get(pointsSize).getX() + 20,
                        points.get(pointsSize).getY());
            }
        }
        return path;
    }
}

再次感谢您的帮助