繁体   English   中英

尝试将BufferedImage绘制到文件,不起作用

[英]Trying to draw BufferedImage to file, doesn't work

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM