本文介绍了如何在不实际渲染的情况下从Javafx图表(线图)获取图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用JavaFx工具来生成图表,该图表将包含在报告生成模块中

I want to employ JavaFx facilities to generate charts which will be included in a report generation module

我已经阅读了一些使用快照LineChart方法的代码段

I've read some code snippets that use the snapshot LineChart's method

// lineChart previously properly. It actually renders in a Window (on
// another JavaFX application). But this test case doesn't display it.

WritableImage wi = lineChart.snapshot(new SnapshotParameters(), new WritableImage(250, 250));
File file = new File("CanvasImage.png");
try {
    ImageIO.write(SwingFXUtils.fromFXImage(wi, null), "png", file);
} catch (Exception s) {
}

但是当我显示图像时,它什么也没显示,在0,0位置只有一点坐标轴.

But when I display the image it shows nothing, only a little coordinate axis at 0,0 position.

推荐答案

如果要获取节点的屏幕快照,首先必须在场景中进行渲染.您无需在任何阶段使用该场景..只需创建一个场景->设置所需的节点->并调用该节点的applyCss方法.然后即可拍摄快照.

If you want to get the screenshot of a node, first you have to render it in a Scene. You dont need to use that Scene in any stage.. just create a scene ->set the node you desired -> and call the applyCss method of the node. Then you can take the snapshot.

以下代码对我有用.我尚未在应用程序中呈现我的"lineChart".

Below code worked for me. I have not rendered my 'lineChart' in the application.

注意:只需确保虚拟场景的大小与图像的大小相同.

Note: Just ensure that the dummy scene size is same as the dimensions of your image.

LineChart<String, Number> lineChart= buildChart();
new Scene(lineChart,800, 600);
lineChart.applyCss();
WritableImage wi = lineChart.snapshot(new SnapshotParameters(), new WritableImage(800, 600));
File file = new File("CanvasImage.png");
try {
    ImageIO.write(SwingFXUtils.fromFXImage(wi, null), "png", file);
} catch (Exception s) {
}

这篇关于如何在不实际渲染的情况下从Javafx图表(线图)获取图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:42