[英]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.