0

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

Community
  • 1
  • 1
Oz Radiano
  • 779
  • 2
  • 11
  • 30
  • 1
    Generally yes, you could use the concepts demonstrated [here](http://stackoverflow.com/questions/15607427/java-make-a-directed-line-and-make-it-move/15607737#15607737) and [here](http://stackoverflow.com/questions/20129281/mouse-motion-listener-only-in-one-direction/20131064#20131064) for some ideas. These both use `Path2D` to generate the shape and calculate the angle between 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 – MadProgrammer Mar 04 '14 at 00:45
  • Thanks for your reply. I need the arrow to be in JMapViewer and your links show it in Graphics2D I think? – Oz Radiano Mar 04 '14 at 01:11
  • 1
    And so are yours. Your first link creates `Path2D` and paints it to `Graphics2D` context... – MadProgrammer Mar 04 '14 at 01:12
  • OK I see what you mean, Thanks! You can write it as an answer and i'll mark it if you like – Oz Radiano Mar 04 '14 at 01:15

2 Answers2

1

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)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

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

Oz Radiano
  • 779
  • 2
  • 11
  • 30