我正在使用 Java FX,我想将节点转换为图像。我找到了这个资源,但它没有解决我的问题,因为我想将节点转换为图像,而不是整个场景。
How to output the content of a Scene graph in JavaFx 2.0 to an Image
最佳答案
您可以使用新的 FX 2.2 快照功能:
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();
}
如果您出于某种原因必须使用较旧的 FX,只需使用
Node#getBoundsInParent
将场景坐标更改为您的节点坐标即可。调用您链接的代码示例。
关于javafx - 如何在javafx 2.1中将节点转换为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12330720/