【问题标题】:JavaFX using a SVGPath as a maskJavaFX 使用 SVGPath 作为掩码
【发布时间】:2016-02-26 17:44:28
【问题描述】:

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

结果为@​​987654321@。但是,一旦我尝试通过将最后一行更改为

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

结果为@​​987654322@。有谁知道我能做什么?

【问题讨论】:

标签: java svg javafx clip


【解决方案1】:

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]

【讨论】:

    猜你喜欢
    • 2018-06-24
    • 1970-01-01
    • 2017-03-23
    • 2018-03-19
    • 2013-11-05
    • 2016-12-21
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多