JavaFx创建HatchStyles(类似于C#.Net) [英] JavaFx create HatchStyles (something like of C# .Net)

查看:186
本文介绍了JavaFx创建HatchStyles(类似于C#.Net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有类似.Net

解决方案

一种方法是略微修改你的建议:使用 ImagePattern ,但


Is there anything similar to .Net HatchStyles for filling shapes when using scene shape objects or drawing on JavaFx Canvas (in jdk, third party libraries, sample codes, etc)?

The only solution I currently can think of is using an ImagePattern created via Image screenshots of those .net hatchstyles!

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

graphical representation of .Net hatch styles:

解决方案

One approach would be a slight modification of what you suggested: use an ImagePattern, but snapshot an appropriate node for the underlying image.

Without actually knowing what the HatchStyles look like, variations on the following might work:

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

This results in

这篇关于JavaFx创建HatchStyles(类似于C#.Net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆