Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SVGPath作为掩码的JavaFX_Java_Svg_Javafx_Clip - Fatal编程技术网

使用SVGPath作为掩码的JavaFX

使用SVGPath作为掩码的JavaFX,java,svg,javafx,clip,Java,Svg,Javafx,Clip,我在尝试使用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

我在尝试使用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)));
结果是。但是,有一次我尝试通过将最后一行更改为

rectangle.setClip(path);
primaryStage.setScene(new Scene(new VBox(rectangle)));`

结果是。有人知道我可以做什么吗?

VBox是一个布局管理器,它将更改添加组件的布局。如果将对象放置在一个组(没有布局)而不是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]