Question

I want to create an arrow (---->) on my map, connecting 2 given coordinates.

By now I used the draw line using polygon implementation suggested here and here

Is there a way to acheieve it?

Thanks, Ozrad

Was it helpful?

Solution

Generally yes, you could use the concepts demonstrated in Java make a directed line and make it move and mouse motion listener only in one direction for some ideas.

These both use Path2D to generate the shape and calculate the angle between two points to orientate the shape.

Your requirement is actually a little easier, as you will already have a start and end point, so it should be possible to either calculate the length of the line or angle of the arrow head, depending on which is simpler for you (I'd be calculating the length of the line between the points and rotating it, but that's just me)

OTHER TIPS

The complete solution, based on @MadProgrammer help, is this:

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;
    }
}

Thanks again for the help

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top