如何在javafx中整个场景快照?

时间:2015-09-22 04:13:53

标签: java png javafx-8 scene

有这个完全正常工作的代码,但我不知道JAVAFX中整个场景的快照如何作为PNG格式?

我尝试导出饼图作为它工作的快照,但我无法想象如何添加另一个图表作为快照。 这是我的代码

package javaapplication5;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class FlowChart extends Application {

  @Override
  public void start(Stage primaryStage) {


            ObservableList<PieChart.Data> pieChartData = 
                    FXCollections.observableArrayList(
                        new PieChart.Data("WMC", 100),
                        new PieChart.Data("DIT", 200),
                        new PieChart.Data("NOC", 50),
                        new PieChart.Data("CBO", 75),
                        new PieChart.Data("RFC", 110),
                        new PieChart.Data("LCOM", 300),
                        new PieChart.Data("Ca", 111),
                        new PieChart.Data("NPM", 30)

                    );

    final PieChart pieChart = new PieChart(pieChartData);
    Double[] data = {0.1, 0.4, 0.5, 0.7, 0.9, 1.0};
    LineChart<Number, Number> lc = createLineChart(data);  
    pieChart.setTitle("RESULT");

    FlowPane root = new FlowPane();
    root.getChildren().addAll(lc, pieChart);
    Scene scene = new Scene(root, 600, 800);
    primaryStage.setTitle("Result Analysis");
    primaryStage.setScene(scene);
    primaryStage.show();

WritableImage image = pieChart.snapshot(new SnapshotParameters(), null);
File file = new File("C:\\Users\\acer\\Desktop\\Charts.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
}catch (IOException e) {
    // TODO: handle exception here
}

  }

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

  private LineChart<Number, Number> createLineChart(Double[] axisValues) {
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Time");
    //creating the chart
    final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);

    lineChart.setTitle("Axis' values");
    //defining a series
    XYChart.Series<Number, Number> series = new LineChart.Series<>();
    series.setName("X Axis");
    //populating the series with data
    for (int i = 0; i < axisValues.length; i++) {
      XYChart.Data<Number, Number> data = new LineChart.Data<>(i, axisValues[i]);
      series.getData().add(data);
    }
    lineChart.getData().add(series);
    return lineChart;
  }
}

1 个答案:

答案 0 :(得分:4)

snapshot()可以在节点上使用,而不是在场景上使用。为了捕捉场景的快照&#34;,您可以拍摄场景的根节点的快照。

在您当前的问题中,只需从FlowPane(场景的根)创建一个快照,它应该可以正常工作。

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