【问题标题】:Java Graphics2D Drawing into BufferedImageJava Graphics2D 绘图到 BufferedImage
【发布时间】:2016-02-19 14:02:12
【问题描述】:

我正忙于摆弄 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);
      }

   }

}

【问题讨论】:

  • 正如@Hovercraft Full Of Eels 在他的评论中所说,如果您从您的paintComponent 方法调用saveImage,则每次重新绘制JPanel 时,该文件都会被覆盖。这真的是你想要在那里做的吗?
  • 是的,每次绘制图像时都必须保存它,所以我不希望它们被单独调用

标签: java swing output bufferedimage graphics2d


【解决方案1】:

您正在用从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);
    }


}

【讨论】:

  • 根据这个答案,1+,将绘图到屏幕与绘图到文件分开很重要,因为您不想以任何方式减慢屏幕渲染速度,也不想不必要地重复绘图一个不必要的文件。
  • 这很完美,我将更新我的问题以反映您的建议,以防任何人提出改进建议
  • @Hovercraft Full Of Eels 我想你不知道如何保持输出的透明度?
  • @Hovercraft Full Of Eels 没关系,我只需将 TYPE_INT_BGR 更改为 TYPE_4BYTE_ABGR_PRE
  • 我看到您在几周前被告知不要在 paintComponent 内部进行文件 I/O(请参阅第一条评论),但仍在这样做——为什么?
猜你喜欢
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 2013-08-21
  • 2014-10-25
相关资源
最近更新 更多