-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arrow.java
46 lines (35 loc) · 1.55 KB
/
Arrow.java
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
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
public enum Arrow {;
public static void draw (final Graphics2D gfx, final Point2D start, final Point2D end, final Stroke lineStroke, final Stroke arrowStroke, final float arrowSize) {
final double startx = start.getX();
final double starty = start.getY();
gfx.setStroke(arrowStroke);
final double deltax = startx - end.getX();
final double result;
if (deltax == 0.0d) {
result = Math.PI / 2;
}
else {
result = Math.atan((starty - end.getY()) / deltax) + (startx < end.getX() ? Math.PI : 0);
}
final double angle = result;
final double arrowAngle = Math.PI / 12.0d;
final double x1 = arrowSize * Math.cos(angle - arrowAngle);
final double y1 = arrowSize * Math.sin(angle - arrowAngle);
final double x2 = arrowSize * Math.cos(angle + arrowAngle);
final double y2 = arrowSize * Math.sin(angle + arrowAngle);
final double cx = (arrowSize / 2.0f) * Math.cos(angle);
final double cy = (arrowSize / 2.0f) * Math.sin(angle);
final GeneralPath polygon = new GeneralPath();
polygon.moveTo(end.getX(), end.getY());
polygon.lineTo(end.getX() + x1, end.getY() + y1);
polygon.lineTo(end.getX() + x2, end.getY() + y2);
polygon.closePath();
gfx.fill(polygon);
gfx.setStroke(lineStroke);
gfx.drawLine((int) startx, (int) starty, (int) (end.getX() + cx), (int) (end.getY() + cy));
}
}