1

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: enter image description here

Omid
  • 5,823
  • 4
  • 41
  • 50
  • Depending on what you need, clever uses of [`LinearGradient`s](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/paint/LinearGradient.html) might work. AFAIK (I know nothing about C#) there is no direct equivalent. Note for `ImagePattern` you can also create the underlying `Image` as a `WritableImage` or even a [`snapshot`](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/Node.html#snapshot-javafx.scene.SnapshotParameters-javafx.scene.image.WritableImage-) of, say, a `Pane` containing `Shape` instances. – James_D Sep 02 '16 at 18:24
  • 2
    JPG is not be the best image format to do this, since it's lossy. If you're using that approach, you're better of using `.png`s. – fabian Sep 02 '16 at 18:53

1 Answers1

4

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

enter image description here

James_D
  • 201,275
  • 16
  • 291
  • 322
  • thanks! It's much better than using screenshots and I think there is no better solution, I decompiled .Net code and found out it uses GDI+ for painting HatchSytles so even porting the code is not an option. – Omid Sep 06 '16 at 15:44