我正在尝试将JPanel转换为BufferedImage,然后将该图像保存到文件中。
DrawingPanel是一个扩展JPanel的类。
public class J extends JFrame {
public J(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
DrawingPanel panel = new DrawingPanel();
add(panel);
BufferedImage bi = (BufferedImage) panel.createImage(500,500);
File file = new File("file.png");
setVisible(true);
try {
ImageIO.write(bi, "PNG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我收到错误:IllegalArgumentException: image == null!
此错误发生在以下行中:
ImageIO.write(bi, "PNG", file);
有什么问题?感谢
编辑:DrawingPanel类:
public class DrawingPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
setBackground(Color.BLACK);
}
}
答案 0 :(得分:6)
由于我们无法使用您的DrawingPanel,因此很难猜测您的问题,但这可能是由于您尝试在GUI渲染之前获取您的图像,并且实际上在它已经渲染任何内容之前,这种情况发生了只有在JFrame上调用setVisible(true)
后才能使用。
如,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageFoo extends JFrame {
public ImageFoo() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
// setSize(500, 500);
DrawingPanel panel = new DrawingPanel();
add(panel);
// BufferedImage bi = (BufferedImage) panel.createImage(500, 500);
// File file = new File("file.png");
pack();
setLocationRelativeTo(null);
setVisible(true);
BufferedImage bi = (BufferedImage) panel.createImage(500, 500);
File file = new File("file.png");
try {
ImageIO.write(bi, "PNG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void createAndShowGui() {
JFrame frame = new ImageFoo();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class DrawingPanel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
public DrawingPanel() {
setBackground(Color.black);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Paint paint = new GradientPaint(0, 0, Color.red, 20, 20, Color.blue, true);
g2.setPaint(paint);
g2.fillOval(0, 0, PREF_W, PREF_H);
// setBackground(Color.BLACK); // never do in paintComponent
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}
编辑:我更喜欢这种尝试
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageFoo extends JFrame {
public ImageFoo() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
// setSize(500, 500);
final DrawingPanel panel = new DrawingPanel();
add(panel);
// BufferedImage bi = (BufferedImage) panel.createImage(500, 500);
// File file = new File("file.png");
pack();
setLocationRelativeTo(null);
setVisible(true);
new Thread(new Runnable() {
public void run() {
// try {
// Thread.sleep(300);
// } catch (InterruptedException e1) {
// }
// BufferedImage bi = (BufferedImage) panel.createImage(500, 500);
BufferedImage bi = new BufferedImage(DrawingPanel.PREF_W,
DrawingPanel.PREF_H, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
panel.paintAll(g2);
g2.dispose();
File file = new File("file.png");
try {
ImageIO.write(bi, "PNG", file);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private static void createAndShowGui() {
JFrame frame = new ImageFoo();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class DrawingPanel extends JPanel {
public static final int PREF_W = 500;
public static final int PREF_H = PREF_W;
public DrawingPanel() {
setBackground(Color.black);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Paint paint = new GradientPaint(0, 0, Color.red, 20, 20, Color.blue, true);
g2.setPaint(paint);
g2.fillOval(0, 0, PREF_W, PREF_H);
// setBackground(Color.BLACK); // never do in paintComponent
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
}