Java Graphics2D绘图到BufferedImage

时间:2016-02-19 14:02:13

标签: java swing output bufferedimage graphics2d

我正在忙着摆弄Java的Graphics2D和绘图,虽然它有效但我不知道如何从这个图形创建一个BufferedImage,我觉得我需要这样做才能将它保存到某个地方

我有一些非常基本的东西,因为我试图了解其工作原理

import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class myFrame {

   public static void main(String[] args) {

      JFrame lv_frame = new JFrame();
      lv_frame.setTitle("Drawing");
      lv_frame.setSize(300, 300);
      lv_frame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);

      lv_frame.add(new drawingPanel(5, 5));

      lv_frame.setVisible(true);

   }

}

class drawingPanel extends JPanel {

   public drawingPanel(int x, int y) {
   }

   public void draw(Graphics graphic) {

      Graphics2D graphic2D = (Graphics2D) graphic;
      graphic2D.fillArc(0, 0, 50, 50, 0, 45);
      graphic2D.fillArc(0, 0, 50, 50, 135, 45);

      BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_BGR);

      try {
         graphic2D = image.createGraphics();
         File output = new File("output.png");
         ImageIO.write(image, "png", output);
      } catch(IOException log) {
         System.out.println(log);
      }

   }

   public void paintComponent(Graphics graphic) {

      super.paintComponent(graphic);
      draw(graphic);

   }

}

除了我得到一个空白的png作为我的output.png之外,这个工作正常,我不知道为什么虽然我很确定我的代码是非常错误的

工作版

import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class myFrame {

   public static void main(String[] args) {

      JFrame lv_frame = new JFrame();
      lv_frame.setTitle("Drawing");
      lv_frame.setSize(300, 300);
      lv_frame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);

      lv_frame.add(new drawingPanel());

      lv_frame.setVisible(true);

   }

}

class drawingPanel extends JPanel {

   public void paintComponent(Graphics graphic) {

      super.paintComponent(graphic);
      draw(graphic);
      saveImage();

   }

   public void draw(Graphics graphic) {

      Graphics2D graphic2D = (Graphics2D) graphic;

      Color color = Color.decode("#DDDDDD");
      graphic2D.setPaint(color);

      graphic2D.fillArc(0, 0, 50, 50, 0, 45);
      graphic2D.fillArc(0, 0, 50, 50, 135, 45);

   }

   public void saveImage() {

      BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_BGR);
      Graphics2D graphic2D = image.createGraphics();

      try {
         File output = new File("output.png");
         draw(graphic2D);
         ImageIO.write(image, "png", output);
      } catch(IOException log) {
         System.out.println(log);
      }

   }

}

1 个答案:

答案 0 :(得分:5)

您正在使用Graphics2D获取的image.createGraphics()对象覆盖draw对象,该对象在您刚创建时就是空白。

public void draw(Graphics graphic) { Graphics2D graphic2D = (Graphics2D) graphic; graphic2D.fillArc(0, 0, 50, 50, 0, 45); graphic2D.fillArc(0, 0, 50, 50, 135, 45); } 方法简化为:

Image

然后从其他方法调用它,在您的实际Graphics2D public void saveAsImage(){ BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_BGR); try { Graphics2D graphic = image.createGraphics(); File output = new File("output.png"); draw(graphic); // actual drawing on your image ImageIO.write(image, "png", output); } catch(IOException log) { System.out.println(log); } } 上执行绘画:

CommandName