我需要通过读取数据库中的一些条目来创建图表。我只想创建图表的图像并创建一个新的png。我更喜欢使用Java本机库执行此操作,到目前为止,我已经能够完成以下工作。import javafx.application.Application;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.embed.swing.SwingFXUtils;import javafx.scene.Scene;import javafx.scene.image.WritableImage;import javafx.stage.Stage;import javafx.scene.chart.*;import javafx.scene.Group;import java.io.File;import java.io.IOException;import java.nio.file.Path;import java.nio.file.Paths;import javax.imageio.ImageIO;public class Test extends Application { @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Imported Fruits"); stage.setWidth(500); stage.setHeight(500); ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( new PieChart.Data("Grapefruit", 13), new PieChart.Data("Oranges", 25), new PieChart.Data("Plums", 10), new PieChart.Data("Pears", 22), new PieChart.Data("Apples", 30)); final PieChart chart = new PieChart(pieChartData); chart.setTitle("Imported Fruits"); ((Group) scene.getRoot()).getChildren().add(chart); stage.setScene(scene); WritableImage img = new WritableImage(800, 800); scene.snapshot(img); writeImage(Paths.get("/Users/images"), "test.png", img); } public static void writeImage(Path path, String fileName, WritableImage image) { File file = new File(Paths.get(path.toString(), fileName).toString()); try { ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); }}在上面的示例中,我需要启动一个应用程序并创建图表。我只需要创建图表的图像并将其写入文件,是否有一种无需启动应用即可完成此操作的方法?还是有更好的方法来做到这一点?
添加回答
举报
0/150
提交
取消