JavaFX我想完全保存Chart-Image

use*_*865 9 charts javafx

这是我的代码:

public class SceneToImageAndWrite extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Primary Stage");

        final CategoryAxis xAxis = new CategoryAxis();
        xAxis.setLabel("Animals");
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Number");

        final LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
        lineChart.setTitle("Line Chart");

        XYChart.Series<String, Number> series1 = new Series<String, Number>();
        series1.setName("Series");

        /** xAxis & yAxis Data */
        LinkedHashMap<String, Number> map = new LinkedHashMap<>();      
        map.put("dog", 12);
        map.put("cat", 3);
        map.put("bear", 8);
        map.put("tiger", 20);

        for (Entry<String, Number> entry : map.entrySet()) {
            series1.getData().add(new XYChart.Data<String, Number>(entry.getKey(), entry.getValue()));
        }

        lineChart.getData().add(series1);

        Scene scene = new Scene(lineChart);
        stage.setScene(scene);
        stage.show();

        WritableImage snapShot = scene.snapshot(null);
        ImageIO.write(SwingFXUtils.fromFXImage(snapShot, null), "png", new File("test.png"));
    }
}
Run Code Online (Sandbox Code Playgroud)

帧很棒,但保存图像没有它应该具有的所有图表数据.我想完全保存Chart-Image,出了什么问题?⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

jew*_*sea 20

关闭图表上的动画.

快照 javadoc:

在拍摄正在动画的场景的快照时,无论是应用程序显式还是隐式(例如图表动画),快照将根据拍摄快照时场景图的状态进行渲染,并且不会反映任何后续动画更改.

  • @ user2023865你应该接受这个答案. (5认同)