答案 0 :(得分:1)
一般情况下,您可以使用Java make a directed line and make it move和mouse 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;
}
}
再次感谢您的帮助