I'm writing a JavaFX Application which needs to draw directed graphs. I want to implement this by shapes because I want the vertices can be dragged. I use a circle to represent a vertex, but I meet a problem when I try to find a shape to represent the directed edge.
A directed edge should looks like an arrow, my idea is to combine a line with a triangle to represent it. So I extend the javafx.scene.shape.Line class:
class Arrow extends Line {
Polygon triangle;
Arrow(double startX, double startY, double endX, double endY) {
super(startX, startY, endX, endY);
double dx = endX - startX;
double dy = endY - startY;
double angle = Math.atan2(dy, dx);
triangle = new Polygon(endX, endY, endX - 8, endY + 4, endX - 8, endY - 4);
triangle.setRotate(Math.toDegrees(angle));
triangle.rotateProperty().bind()
triangle.rotateProperty().bind(Bindings.createDoubleBinding(() -> {
double x = this.getEndX() - this.getStartX();
double y = this.getEndY() - this.getStartY();
double a = Math.atan2(y, x);
return Math.toDegrees(a);
}));
}
}
As the vertices can be dragged, of course the edge should move at the same time, meaning that the triangle inside the arrow should move and rotate according to the line's properties. But I haven't found a way to bind the Polygon's position property with the Line's endXProperty and endYProperty.
So I want to ask how to achieve this? Or is there any better way to implement a movable arrow(directed edge) by shapes?