繁体   English   中英

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

[英]how snapshot the entire scene in javafx?

有这个完整的代码,但是我不知道如何将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;
  }
}

snapshot()可以在节点上使用,而不能在场景上使用。 为了捕获“场景快照”,您可以拍摄场景根节点的快照。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM