Skip to content

Instantly share code, notes, and snippets.

@kn0412
Last active July 2, 2024 06:40
Show Gist options
  • Save kn0412/2086581e98a32c8dfa1f69772f14bca4 to your computer and use it in GitHub Desktop.
Save kn0412/2086581e98a32c8dfa1f69772f14bca4 to your computer and use it in GitHub Desktop.
Arrow class for JavaFX
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
/**
*
* @author kn
*/
public class Arrow extends Path{
private static final double defaultArrowHeadSize = 5.0;
public Arrow(double startX, double startY, double endX, double endY, double arrowHeadSize){
super();
strokeProperty().bind(fillProperty());
setFill(Color.BLACK);
//Line
getElements().add(new MoveTo(startX, startY));
getElements().add(new LineTo(endX, endY));
//ArrowHead
double angle = Math.atan2((endY - startY), (endX - startX)) - Math.PI / 2.0;
double sin = Math.sin(angle);
double cos = Math.cos(angle);
//point1
double x1 = (- 1.0 / 2.0 * cos + Math.sqrt(3) / 2 * sin) * arrowHeadSize + endX;
double y1 = (- 1.0 / 2.0 * sin - Math.sqrt(3) / 2 * cos) * arrowHeadSize + endY;
//point2
double x2 = (1.0 / 2.0 * cos + Math.sqrt(3) / 2 * sin) * arrowHeadSize + endX;
double y2 = (1.0 / 2.0 * sin - Math.sqrt(3) / 2 * cos) * arrowHeadSize + endY;
getElements().add(new LineTo(x1, y1));
getElements().add(new LineTo(x2, y2));
getElements().add(new LineTo(endX, endY));
}
public Arrow(double startX, double startY, double endX, double endY){
this(startX, startY, endX, endY, defaultArrowHeadSize);
}
}
@salimkaboura8
Copy link

Thank you this is very helpful!

@bobbymtnz
Copy link

Thank you for this class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment