JavaFX使用SVGPath作为掩码

时间:2016-02-26 17:44:28

标签: 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.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中的结果。有谁知道我能做什么?

1 个答案:

答案 0 :(得分:2)

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

您可以转换SVGPath,使其与矩形相交并剪切它。

UPDATE points_table AS p
JOIN numbers_table AS n ON p.user = n.user
SET p.points = p.points + 10
WHERE n.number > 5

输出如下(注意用于负面翻译的矩形的minX和minY值):

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);
    }
}

clip