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

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

问题描述

这是我的代码。



    import java.io.File;
    import java.util.LinkedHashMap;
    import java.util.Map.Entry;

    import javafx.application.Application;
    import javafx.embed.swing.SwingFXUtils;
    import javafx.scene.Scene;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.LineChart;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.XYChart;
    import javafx.scene.chart.XYChart.Series;
    import javafx.scene.image.WritableImage;
    import javafx.stage.Stage;

    import javax.imageio.ImageIO;

    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 lineChart = new LineChart(xAxis, yAxis);
            lineChart.setTitle("Line Chart");

            XYChart.Series series1 = new Series();
            series1.setName("Series");

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

            for(Entry entry : map.entrySet()) {
                series1.getData().add(new XYChart.Data(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"));
        }
    }




...

Saved-image没有一个图表数据。我想完全保存Chart-Image。



Frame is great but...
Saved-image hasn't a little chart-data. I want to save Chart-Image completely.

推荐答案

关闭动画

快照 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天全站免登陆