我已经成功地在同一个面板上绘制了两个图表(条形图和折线图).

我正在try 实现一个保存按钮,当单击该按钮时,会将结果图像(带有轴)写入到我保存的 Select 的BMP图像中.

代码运行,我收到一个肯定的警告,并创建了一个图像文件.但是,生成的图像文件为空(0字节).

@FXML // fx:id="graph"
    private Pane graph; // Value injected by FXMLLoader

@FXML // fx:id="saveButton"
    private Button saveButton; // Value injected by FXMLLoader

// ...

@FXML
    void clickSave(ActionEvent event) {
        Stage yourStage = (Stage) saveButton.getScene().getWindow();

        FileChooser fileChooser = new FileChooser();
        fileChooser.setInitialDirectory(new File("Path\\With\\Spaces"));
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("BMP Files", "*.bmp"));

        // Show save dialog
        File file = fileChooser.showSaveDialog(yourStage);

        if (file != null) {
            if (!file.exists()) {
                try {
                    Files.createFile(file.toPath());
                } catch (IOException e) {
                    e.printStackTrace(); // Handle the exception
                }
            }

            WritableImage writableImage = graph.snapshot(new SnapshotParameters(), null);
            BufferedImage bufferedImage = SwingFXUtils.fromFXImage(writableImage, null);

            try {
                ImageIO.write(bufferedImage, "BMP", file);

                // Inform the user about the successful save
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("File Saved");
                alert.setHeaderText(null);
                alert.setContentText("The file has been saved successfully.");
                alert.showAndWait();
            } catch (IOException e) {
                e.printStackTrace();

                // Inform the user about the error
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("Error");
                alert.setHeaderText(null);
                alert.setContentText("An error occurred while saving the file.");
                alert.showAndWait();
            }
        }
    }

编辑: 按照@James_D的注释建议,我将代码更改为以下代码,但问题仍然存在.

@FXML
    void clickSave(ActionEvent event) {
        Stage stage = (Stage) saveButton.getScene().getWindow();

        FileChooser fileChooser = new FileChooser();
        fileChooser.setInitialDirectory(new File("Path\\With\\Spaces"));
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("BMP Files", "*.bmp"));

        // Show save dialog
        File file = fileChooser.showSaveDialog(stage);

        if (file != null) {
            WritableImage writableImage = graph.snapshot(new SnapshotParameters(), null);
            BufferedImage bufferedImage = SwingFXUtils.fromFXImage(writableImage, null);

            try {
                ImageIO.write(bufferedImage, "BMP", file);

                if (!file.exists()) {
                    Files.createFile(file.toPath());
                }

                // Inform the user about the successful save
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("File Saved");
                alert.setHeaderText(null);
                alert.setContentText("The file has been saved successfully.");
                alert.showAndWait();
            } catch (IOException e) {
                e.printStackTrace();

                // Inform the user about the error
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("Error");
                alert.setHeaderText(null);
                alert.setContentText("An error occurred while saving the file.");
                alert.showAndWait();
            }
        }
    }

推荐答案

非常感谢@Slaw和this answer.

SwingFXUtils函数返回类型为TYPE_INT_ARGB_PRE的图像.只有类型TYPE_INT_RGB的图像才能写入.bmp文件,否则ImageIO.write(bufferedImage, "BMP", file);返回false.因此,必须进行从TYPE_INT_ARGB_PRETYPE_INT_RGB的适当转换.下面的块显示了更新后的相关代码,其中现在包含了转换.

WritableImage writableImage = graph.snapshot(new SnapshotParameters(), null);
BufferedImage preBufferedImage = SwingFXUtils.fromFXImage(writableImage, null);

BufferedImage bufferedImage = new BufferedImage(
        preBufferedImage.getWidth(),
        preBufferedImage.getHeight(),
        BufferedImage.TYPE_INT_RGB
);
bufferedImage.createGraphics().drawImage(
        preBufferedImage,
        0,
        0,
        java.awt.Color.WHITE,
        null
);

try {
    Boolean __ = ImageIO.write(bufferedImage, "BMP", file);

    if (!file.exists()) {
        Files.createFile(file.toPath());
    }
    ...
}

Java相关问答推荐

使用联接和分页的SpringBoot Spring数据JPA

现场观看Android Studio中的变化

try 创建一个对象,使用它,然后使用一条语句将其存储为列表

不推荐使用的Environment.getExternalStorageDirectory().getAbsolutePath()返回的值不同于新的getExternalFilesDir(空)?

在Java Swing Paint应用程序中捕获快速鼠标移动时遇到困难

Kotlin内联互操作:强制装箱

如何在Java中从XML中获取特定的 node ,然后将其删除?

%This内置函数示例

扩展视图高度,并将其拖动到较低的视图上,而不是将其向下推?

如何在JUNIT测试中覆盖ExecutorService?

Java创建带有扩展通配符的抽象处理器

如何在@CsvSource中传递空格作为值

Java 21中泛型的不兼容更改

如何在SWT菜单项文本中保留@字符

处理4.3问题:javax.xml.ind包不存在(&Q;).您可能在学习GitHub教程时遗漏了库.&Q

如何对存储为字符串的大数字数组进行排序?

在JSON上获取反斜杠

message.acknowledge()没有';在使用Spring Boot在ActiveMQ中读取消息后,t将消息出列

为什么child-pom会创建一个新版本

睡眠在 Spring Boot 中