我已创建了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不应考虑变换),但如何绕过这个问题?