Java Graphics2D绘制到BufferedImage中 [英] Java Graphics2D Drawing into BufferedImage

查看:751
本文介绍了Java Graphics2D绘制到BufferedImage中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于摆弄Java的Graphics2D和图形,尽管它可以正常工作,但是我不确定如何从该图形创建BufferedImage,看来我需要这样做才能将其保存在某个地方.

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

   }

}

除了我得到一个空白的png作为我的output.png之外,这个方法还行,我不确定为什么,尽管我相当确定我的代码非常错误

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

工作版本

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对象,该对象在创建时为空白.

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

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进行绘画:

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


}

这篇关于Java Graphics2D绘制到BufferedImage中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆