Java Graphics2D Drawing into BufferedImage

2019-08-14 16:11发布

问题:

I'm busy fiddling around with Java's Graphics2D and drawings and I although it works I am not sure how to create a BufferedImage from this graphic which it seems I need to do in order so save it somewhere.

I have something very basic because I'm trying to understand how this works

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);

   }

}

This works ok except I am getting a blank png as my output.png and I'm not sure why although I'm fairly certain my code is horribly wrong

WORKING VERSION

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:

You are overwriting the Graphics2D Object with the one you get from image.createGraphics(), which is blank as you just created it.

Simplify the draw method to :

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);

}

Then call it from another method, to perform the painting on your actual Image's 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);
    }


}