1

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 を使用してイメージを保存できる new が開始されます。画像は保存を拒否し、代わりに IllegalArguementException を発生させます。問題はこの Save メソッドにあるはずであり、ImageIO.write(bla bla bla)メソッドで発生すると想定しています。この画像が正しく保存されるようにするにはどうすればよいですか? また、何が間違っているのでしょうか? JFileChooser API を少し読んだことがありますが、これが唯一の重要な部分だと思いましたが、戻って何かを追加する必要があるかどうかを教えてください。ありがとう。

補足: JFileChooser は、ユーザーがメイン プログラムの JMenuBar の [名前を付けて保存] ボタンを押した場合にのみ表示されます (表示されていません)。メイン プログラムは、次のコード部分を使用して使用できる "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)
4

1 に答える 1

5

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: image == null!

The exception states that it is being thrown because your image is null, so the problem is not in the code above, but in the code that calls the code above: You're passing in a null image.

To prove it one way or the other, test it:

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

Also and again, these guys:

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

should not be static. Again, the JFileChooser likely loses the Nimbus L&F because of this, because it is initiated when the class loads, before you've set the L&F, and not when an object of your IOClass is instantiated.


Edit
As an aside, shouldn't this:

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

be this?:

ImageIO.write(image, "png", fileChooser.getSelectedFile());
于 2013-06-10T00:02:14.840 に答える