簡體   English   中英

使用JFileChooser保存BufferedImage會引起一些問題

[英]Saving a BufferedImage using a JFileChooser raises a few problems

我有一個相當簡單的繪制程序,它使用BufferedImage作為畫布。 我有一個單獨的類( IOClass ),用於處理保存圖像並打開另一個圖像。 我在通過saveImage()方法保存BufferedImage時遇到了一些麻煩。 這是整個課程:

 package ui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

// class for Saving & Opening images (for the bufferedImage called 'background'
public class IOClass {
    static BufferedImage image;

    public IOClass(BufferedImage image) {
        this.image = image;
    }

    public static final JFileChooser fileChooser = new JFileChooser();
    public void saveImage() {
        int saveValue = fileChooser.showSaveDialog(null);
        if (saveValue == JFileChooser.APPROVE_OPTION) {
            try {
                ImageIO.write(image, "png", new File(fileChooser
                        .getSelectedFile().getAbsolutePath()
                        + fileChooser.getSelectedFile().getName()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public BufferedImage openImage() {
        int open = fileChooser.showOpenDialog(null);

    }
}

因此,正如您通過閱讀saveImage()方法所看到的,這就是問題所在。 程序打開,繪制圖片,然后使用“另存為”菜單選項轉到JMenuBar,這將激活一個actionListener,該類將打開此類並啟動新的fileChooser ,您可以在其中使用JFileChooser保存圖像。 圖像拒絕保存,而是引發IllegalArguementException。 問題必須在此Save方法中,並且我假設它發生在ImageIO.write(bla bla bla)方法中。 我該怎么做才能確保正確保存該圖像,我到底在做什么錯? 我已經閱讀了一些JFileChooser API,並且我認為這是其中唯一真正必要的部分,但是請告訴我是否應該返回並添加一些內容。 謝謝。

附加:僅當用戶按下主程序(未顯示)的JMenuBar上的“另存為”按鈕時,JFileChooser才會出現。 主程序使用“ Nimbus”主題,可以通過使用代碼段來使用它:

try {
            UIManager
                    .setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

當我較早嘗試時,打開JFileChooser也以Nimbus主題打開,但是現在,它僅以正常,無聊的默認Swing外觀打開。 我該怎么做才能使Nimbus主題恢復原狀(看起來好多了)。

編輯:根據要求,完整的堆棧跟蹤:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: image == null!
    at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
    at javax.imageio.ImageIO.getWriter(Unknown Source)
    at javax.imageio.ImageIO.write(Unknown Source)
    at ui.IOClass.saveImage(IOClass.java:26)
    at ui.ProgramUI$saveAsListener.actionPerformed(ProgramUI.java:406)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

線程“ AWT-EventQueue-0”中的異常java.lang.IllegalArgumentException:image == null!

該異常指出由於圖像為空而被拋出,所以問題不在上面的代碼中,而是在調用上面代碼的代碼中:您正在傳遞一個空圖像。

要以另一種方式證明它,請對其進行測試:

    if (saveValue == JFileChooser.APPROVE_OPTION) {
      System.out.println("is image null? " + (image == null));
      try {
        // ....

這些家伙又一次:

static BufferedImage image;
public static final JFileChooser fileChooser = new JFileChooser();

不應該是靜態的。 同樣,JFileChooser可能會因此而失去Nimbus L&F,因為它是在類加載時啟動的,而不是在設置L&F之前啟動的,而不是在實例化IOClass的對象時啟動的。


編輯
順便說一句,這不應該:

ImageIO.write(image, "png", new File(fileChooser
                    .getSelectedFile().getAbsolutePath()
                    + fileChooser.getSelectedFile().getName()));

是這個嗎?:

ImageIO.write(image, "png", fileChooser.getSelectedFile());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM