JavaFX 我想完全保存 Chart-Image [英] JavaFX I want to save Chart-Image completely

查看:28
本文介绍了JavaFX 我想完全保存 Chart-Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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"));
    }
}

Frame 很棒,但保存的图像没有它应有的所有图表数据.我想完全保存Chart-Image,怎么了?⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

Frame is great but saved-image doesn't have all chart-data that it should have. I want to save Chart-Image completely, what's wrong? ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

推荐答案

关闭 动画 在您的图表上.

Turn off animation on your chart.

来自 快照 javadoc:

From the snapshot javadoc:

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

When taking a snapshot of a scene that is being animated, either explicitly by the application or implicitly (such as chart animation), the snapshot will be rendered based on the state of the scene graph at the moment the snapshot is taken and will not reflect any subsequent animation changes.

这篇关于JavaFX 我想完全保存 Chart-Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆