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)?
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 HatchStyle
s 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