gpt4 book ai didi

JavaFX 使用 SVGPath 作为掩码

转载 作者:行者123 更新时间:2023-11-30 03:02:18 31 4
gpt4 key购买 nike

我在尝试使用 SVGPath 剪辑另一个节点时遇到问题,例如以下代码:

Rectangle rectangle = new Rectangle(430, 80);
rectangle.setFill(Paint.valueOf("FF0000"));
SVGPath path = new SVGPath();
path.setContent("m 131.07143,433.07649 c 359.64286,0 360,0.35714 360,0.35714 l 4.28571,1.07143 5.71429,2.5 4.64286,2.85714 4.64285,5 4.64286,7.14286 1.42857,4.28572 1.42857,7.5 -1.07142,10 -1.42858,4.64285 -3.21428,5 -4.64286,5.35715 -5.35714,3.57142 -6.42857,2.85715 -2.85715,1.07143 -3.92857,0.71428 -360.35714,0 -3.21429,-1.07143 -3.92857,-1.42857 -3.57143,-1.78571 -5.71428,-3.30358 -3.21429,-3.48214 -2.05357,-2.67857 -2.67857,-4.28571 -0.98214,-2.76786 -1.69643,-4.01786 -0.44643,-4.375 0.0893,-4.46428 0.35715,-4.375 0.17857,-2.41072 1.60714,-3.57143 1.42857,-3.57143 1.60715,-2.41071 2.32142,-3.03571 2.94643,-3.39286 3.75,-2.67857 3.48215,-1.96429 5.26785,-2.32143 3.57143,-0.44643 z");
primaryStage.setScene(new Scene(new VBox(rectangle, path)));

结果为the SVG being drawn underneath the rectangle correctly 。但是,一旦我尝试通过将最后一行更改为

来剪辑矩形
rectangle.setClip(path);
primaryStage.setScene(new Scene(new VBox(rectangle)));`

结果为the rectangle being completely hidden 。有谁知道我能做什么?

最佳答案

VBox是一个布局管理器,它会改变添加组件的布局。如果将对象放置在 Group(不进行布局)而不是 VBox 中,您将看到该矩形形状和 SVGPath 不相交(SVGPath 位于右侧约 100 像素、下方约 400 像素处)矩形,根据您对路径的定义)。

您可以平移 SVGPath,使其与矩形相交并剪裁它。

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

public class SVGMasker extends Application {
@Override
public void start(Stage stage) throws Exception {
Rectangle rectangle = new Rectangle(430, 80, Color.RED);
SVGPath path = new SVGPath();
path.setContent("m 131.07143,433.07649 c 359.64286,0 360,0.35714 360,0.35714 l 4.28571,1.07143 5.71429,2.5 4.64286,2.85714 4.64285,5 4.64286,7.14286 1.42857,4.28572 1.42857,7.5 -1.07142,10 -1.42858,4.64285 -3.21428,5 -4.64286,5.35715 -5.35714,3.57142 -6.42857,2.85715 -2.85715,1.07143 -3.92857,0.71428 -360.35714,0 -3.21429,-1.07143 -3.92857,-1.42857 -3.57143,-1.78571 -5.71428,-3.30358 -3.21429,-3.48214 -2.05357,-2.67857 -2.67857,-4.28571 -0.98214,-2.76786 -1.69643,-4.01786 -0.44643,-4.375 0.0893,-4.46428 0.35715,-4.375 0.17857,-2.41072 1.60714,-3.57143 1.42857,-3.57143 1.60715,-2.41071 2.32142,-3.03571 2.94643,-3.39286 3.75,-2.67857 3.48215,-1.96429 5.26785,-2.32143 3.57143,-0.44643 z");
path.setTranslateX(-path.getBoundsInLocal().getMinX());
path.setTranslateY(-path.getBoundsInLocal().getMinY());
rectangle.setClip(new Group(path));
System.out.println(path.getBoundsInLocal());
stage.setScene(new Scene(new Group(rectangle)));
stage.show();
}

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

输出如下(注意用于负平移的矩形的 minX 和 minY 值):

BoundingBox [minX:101.07147979736328, minY:433.07647705078125, minZ:0.0, width:416.78570556640625, height:63.9285888671875, depth:0.0, maxX:517.8571853637695, maxY:497.00506591796875, maxZ:0.0]

clip

关于JavaFX 使用 SVGPath 作为掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35658364/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com