gpt4 book ai didi

java - 尝试将 BufferedImage 绘制到文件中,但不起作用

转载 作者:行者123 更新时间:2023-11-29 07:04:32 24 4
gpt4 key购买 nike

我正在尝试将 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);

}

}

最佳答案

很难猜测您的问题,因为您的 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);
}

}

关于java - 尝试将 BufferedImage 绘制到文件中,但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21320550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com