-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathArrow.java
More file actions
47 lines (37 loc) · 1.59 KB
/
Arrow.java
File metadata and controls
47 lines (37 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package fxsimulator;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class Arrow extends Path{
private static final double defaultArrowHeadSize = 7;
private double startX, startY, endX, endY;
public Arrow(double startX, double startY, double endX, double endY, double arrowHeadSize){
super();
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
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);
}
}