【问题标题】:JavaFx Equivalent to Graphics context.rotate() for nodesJavaFx 等效于节点的 Graphics context.rotate()
【发布时间】:2019-04-13 11:01:01
【问题描述】:

如果我在画布中绘图,我可以旋转图形上下文的坐标系,而无需旋转已在相应画布中绘制的任何内容,与平移相同。

在一个组中是否有类似的可能?如果我只是旋转并翻译它的孩子,我不会得到正确的效果,因为翻译将自己定位在组的未旋转系统上。

如果没有,是否有类似 3d 画布的东西具有该功能?

【问题讨论】:

标签: java javafx transform


【解决方案1】:

正如 Slaw 所指出的,我正在寻找的是包 javafx.scene.transform 中的类。

这里是一个例子:假设我想要一条从 (200, 200) 开始的直线,在距离 x 轴 60 度的方向上长度为 200。如果没有 transform 包,这也不难做到,但它只是一个简单的例子。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.scene.transform.Affine;
import javafx.stage.Stage;

public class Example extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage stage) throws Exception {
        Group group = new Group();
        Scene scene = new Scene(group, 400, 400);
        stage.setScene(scene);

        // Does not work as intended
        Line line1 = new Line(0,0, 200, 0);
        line1.setTranslateX(200);
        line1.setTranslateY(200);
        line1.setRotate(60);

        // Does work as intended.
        Line line2 = new Line(0, 0, 200, 0);
        Affine affine = new Affine();
        affine.appendTranslation(200, 200);
        affine.appendRotation(60);
        line2.getTransforms().add(affine);

        group.getChildren().addAll(line1, line2);

        stage.show();
    }
}

【讨论】:

    猜你喜欢
    • 2020-01-17
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多