我認爲創建一個包含向上線和箭頭的組合很容易,然後只需圍繞中心旋轉該組。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Pane root = createPane();
root.setTranslateX(200);
root.setTranslateY(200);
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
private Pane createPane() {
BorderPane root = new BorderPane();
ArrowFactory arrowfactory = new ArrowFactory();
double lineLength = 100;
double centerX = 0;
double centerY = 0;
root.getChildren().addAll(
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 0),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 30),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 45),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 60),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 90),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 135),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 180),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 225),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 270),
arrowfactory.createLineAndArrow(centerX, centerY, lineLength, 315)
);
return root;
}
public static void main(String[] args) {
launch(args);
}
}
ArrayFactory.java
:
public class ArrowFactory {
private final int arrowWidth = 10;
private final int arrowHeight = 10;
public Group createLineAndArrow(double centerX, double centerY, double lineLength, double rotation) {
Group group = new Group();
Line line = new Line(centerX, centerY, centerX, centerY - lineLength);
Polygon upwardArrow = createUpwardArrow(centerX, centerY - lineLength/2);
group.getChildren().addAll(line, upwardArrow);
group.getTransforms().add(new Rotate(rotation, centerX, centerY));
return group;
}
private Polygon createUpwardArrow(double centerX, double centerY) {
Polygon arrow = new Polygon(
createUpwardArrowPoints(centerX, centerY + arrowHeight/2));
arrow.setFill(Color.TRANSPARENT);
arrow.setStroke(Color.BLACK);
return arrow;
}
private double[] createUpwardArrowPoints(double centerX, double centerY) {
return new double[] {
centerX - arrowWidth/2, centerY, // left
centerX + arrowWidth/2, centerY, // right
centerX, centerY - arrowHeight, // top
};
}
}
使用圓心爲輸入你可以使用:
private Group createLineAndArrow(double x1, double y1, double x2, double y2) {
double distance = Math.hypot(x1-x2, y1-y2);
double angle = Math.toDegrees(Math.atan2(x2 - x1, y2 - y1));
return arrowfactory.createLineAndArrow(x1, y1, distance, angle);
}
我很困惑,最後的翻譯是什麼?你確定箭頭沒有指向從頭到尾嗎?至於這種轉變,你有沒有看過layoutBounds?旋轉的關鍵是layoutBounds的中心,所以如果這不是三角形的中心,那可能就是這個轉變的原因。 – Lidae
@Lidae箭頭被旋轉,然後移動到正確位置 - 線條的中間。因此翻譯。 – alex
但他們已經在翻譯之前,在正確的地方,除非箭頭指向內,但是然後箭頭指向錯誤的方式... – Lidae