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)