Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how snapshot the entire scene in javafx?

have this fully working code but i don't know how snapshot the entire scene in JAVAFX as PNG format ?

i try to export pie Chart as snapshot it work but i couldn't figure how to add another chart to be snapshot also . this is my code

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;
  }
}
like image 619
mdsavs Avatar asked Sep 22 '15 04:09

mdsavs


1 Answers

snapshot() can be used on nodes and not on scene. In order to capture a "snapshot of the scene", you can take a snapshot of the root node of the scene.

In your current problem, just create a snapshot from the FlowPane (root of the scene) and it should work.

WritableImage image = root.snapshot(new SnapshotParameters(), null);
like image 129
ItachiUchiha Avatar answered Nov 06 '22 21:11

ItachiUchiha