Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert node to image in javafx 2.1?

I am using Java FX and I would like to convert a node to an image. I found this resource, but it does not solve my problem as I want to convert a node to an image, not a whole scene.

How to output the content of a Scene graph in JavaFx 2.0 to an Image

like image 920
Amit Gujjar Avatar asked Sep 08 '12 12:09

Amit Gujjar


1 Answers

You can use new FX 2.2 snapshot feature:

public class TrySnapshot extends Application {

    @Override
    public void start(Stage primaryStage) {
        final VBox vbox = new VBox(2);
        final Button btn = new Button();
        vbox.getChildren().add(btn);
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                // here we make image from vbox and add it to scene, can be repeated :)
                WritableImage snapshot = vbox.snapshot(new SnapshotParameters(), null);

                vbox.getChildren().add(new ImageView(snapshot));
                System.out.println(vbox.getChildren().size());
            }
        });

        Scene scene = new Scene(new Group(vbox), 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

If you must use older FX for some reason just change scene coordinates to your node coordinates using Node#getBoundsInParent calls in the code sample you linked.

like image 112
Sergey Grinev Avatar answered Oct 16 '22 18:10

Sergey Grinev