Using texture for triangle mesh without having to read/write an image file

后端 未结 2 531
臣服心动
臣服心动 2021-01-21 10:38

This is a followup on a previous question (see Coloring individual triangles in a triangle mesh on javafx) which I believe is another topic on its own.

Is there a way (w

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-21 11:13

    You might create the texture to be used for fill etc. in your code by using any graphic object and convert that into an image in memory, not touching disk.

    The example below will create a texture using a green spline.

    Pane testImage2(Pane pane) {
        Pane inner = new Pane();
        inner.prefWidthProperty().bind(pane.widthProperty());
        inner.prefHeightProperty().bind(pane.heightProperty());
        pane.getChildren().add(inner);
    
        SVGPath texture = new SVGPath();
        texture.setStroke(Color.GREEN);
        texture.setStrokeWidth(2.5);
        texture.setFill(Color.TRANSPARENT);
        texture.setContent("M 10 10 C 40 10 10 70 70 20");
    
        SnapshotParameters params = new SnapshotParameters();
        params.setViewport(new Rectangle2D(-5, -5, 70, 50));
        Image image = texture.snapshot(params, null);
    
        Paint paint = new ImagePattern(image, 5,5, 20, 20, false);
        inner.setBackground(new Background(new BackgroundFill(paint, new CornerRadii(0), new Insets(inset))));
        return pane;
    }
    

    enter image description here

提交回复
热议问题