将AnchorPane打印为PNG

时间:2017-01-30 10:05:17

标签: java javafx png

我将打印为png我的anchorpane:我在控制器中加载一个图像并写一些文本,比我想要保存整个面板,但我不知道我必须使用哪些工具。

URL fxmlpath = this.getClass().getResource("/fxml/ListaNotaSpesa.fxml");

FXMLLoader loader = new FXMLLoader();
AnchorPane pane= loader.load(fxmlpath);


Scene scene = new Scene(pane);

primaryStage.setTitle("Inserisci Note Spese");
primaryStage.setScene(scene);
primaryStage.show();

1 个答案:

答案 0 :(得分:0)

您可以使用Node.snapshot

  

在下一帧拍摄此节点的快照并调用   图像准备好后指定的回调方法。 CSS和布局   之前,将对节点及其任何子节点进行处理   渲染它。整个目标图像被清除为填充   由SnapshotParameters指定的Paint。然后呈现此节点   到图像。

示例:

WritableImage image = anchorPane.snapshot(new SnapshotParameters(), null);

File file = new File("D:\\anchor.png");

ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);