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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With