Java Graphics2D 绘图到 BufferedImage

标签 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);
      }

   }

}

最佳答案

您正在用从 image.createGraphics() 获得的对象覆盖 Graphics2D 对象,它在您刚创建时是空白的。

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

}

然后从另一个方法调用它,在您实际的 ImageGraphics2D 上执行绘画:

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


}

关于Java Graphics2D 绘图到 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35507152/

相关文章:

java - 递归查找数组中数字的最小路径

java - fragment 已添加到 Activity 但不显示

java - 如何在java中添加弹出窗口到jtextFiled View

java - 从 JSON Schema 动态创建 Swing GUI(使用 Metawidget)

javascript - 函数给出 NAN 而不是字符串

c - 在不排序的情况下删除数组中的重复元素 - 错误输出

java - JSON 作为表单参数

java - 我的应用程序在启动时崩溃,我不知道为什么。 Gradle 构建运行良好,没有任何错误

java - 关于在 Java 中使用 GridBagLayout

r - 将 lm 输出列表转换为数据帧