JavaFx 创建 HatchStyles(类似于 C# .Net)

JavaFx create HatchStyles (something like of C# .Net)

是否有任何类似于 .Net HatchStyles for filling shapes when using scene shape objects 或在 JavaFx Canvas 上绘制的内容(在 jdk、第三方库、示例代码等中)?

我目前能想到的唯一解决方案是使用 ImagePattern 通过这些 .net 阴影样式的图像屏幕截图创建的!

GraphicsContext gc = canvas.getGraphicsContext2D();
ImagePattern pattern = new ImagePattern(new Image("dotnet-pattern.png");
gc.setFill(pattern);

.Net 填充样式的图形表示:

一种方法是对您的建议稍作修改:使用 ImagePattern,但 snapshot 一个适合底层图像的节点。

在实际不知道 HatchStyle 长什么样的情况下,以下变体可能会起作用:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;

public class HatchStyleCanvas extends Application {

    @Override
    public void start(Stage primaryStage) {
        Image hatch = createHatch();
        ImagePattern pattern = new ImagePattern(hatch, 0, 0, 20, 20, false);        
        Canvas canvas = new Canvas(600, 600);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setFill(pattern);
        gc.fillRect(0, 0, 600, 600);
        primaryStage.setScene(new Scene(new StackPane(canvas)));
        primaryStage.show();
    }

    private Image createHatch() {
        Pane pane = new Pane();
        pane.setPrefSize(20, 20);
        Line fw = new Line(-5, -5, 25, 25);
        Line bw = new Line(-5, 25, 25, -5);
        fw.setStroke(Color.ALICEBLUE);
        bw.setStroke(Color.ALICEBLUE);
        fw.setStrokeWidth(5);
        bw.setStrokeWidth(5);
        pane.getChildren().addAll(fw, bw);
        new Scene(pane);
        return pane.snapshot(null, null);
    }

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

这导致