JavaFX:将变换应用于节点布局边界

4
我已创建了SVGPath和Label,并将它们放置在StackPane上:
package sample;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.FillRule;
import javafx.scene.shape.SVGPath;
import javafx.stage.Stage;

public class Main extends Application {

    private static final String BUBBLE_SVG_PATH = "m 32.339338,-904.55632 c -355.323298,0 -643.374998,210.31657 " +
            "-643.374998,469.78125 0,259.46468 288.0517,469.812505 643.374998,469.812505 123.404292,0 " +
            "238.667342,-25.3559002 336.593752,-69.3438 69.80799,78.7043 181.84985,84.1354 378.90625,5.3126 " +
            "-149.2328,-8.9191 -166.3627,-41.22 -200.6562,-124.031305 80.6876,-78.49713 128.5,-176.04496 " +
            "128.5,-281.75 0,-259.46468 -288.0205,-469.78125 -643.343802,-469.78125 z";

    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("Hello World");
        SVGPath svgPath = new SVGPath();
        svgPath.setContent(BUBBLE_SVG_PATH);
        svgPath.setFill(Color.WHITE);
        svgPath.setStroke(Color.BLACK);
        svgPath.setScaleX(0.5);
        svgPath.setScaleY(0.5);
        Label label = new Label("Hello world");
        System.out.println(svgPath.getBoundsInLocal());
        System.out.println(svgPath.getBoundsInParent());
        System.out.println(svgPath.getLayoutBounds());
        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(svgPath);
        stackPane.getChildren().add(label);
        Group root = new Group();
        root.getChildren().add(stackPane);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

我的问题是SVGPath的layoutBounds不反映应用的转换。因此,stackPane会在svgPath的原始大小下拉伸并拉伸窗口。我知道这种行为符合文档(layoutBounds不应考虑变换),但如何绕过这个问题?

1个回答

0

在计算布局边界方面,Group 具有以下行为(我强调):

Group 将采用其子项的集体边界,并且不可直接调整大小。

应用于 Group 的任何变换、效果或状态都将应用于该组的所有子项。这些变换和效果不会包含在此 Group 的布局边界中,但是 如果在此 Group 的子项上直接设置变换和效果,则这些变换和效果将包含在此 Group 的布局边界中

因此,将 SVG 路径包装在 Group 中将给您所需的效果:

StackPane stackPane = new StackPane();
// stackPane.getChildren().add(svgPath);
stackPane.getChildren().add(new Group(svgPath));
stackPane.getChildren().add(label);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接