我在尝试使用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]