Convert a Graphics2D to an Image or BufferedImage

前端 未结 5 2006
野的像风
野的像风 2020-12-03 13:58

I have a little problem here.

I have an applet, where user can \"draw\" inside it. To do that, I use the java.awt.Graphics2D. But, how can I do to save the user draw

5条回答
  •  既然无缘
    2020-12-03 14:22

    If you'd like to draw JComponent's image onto BufferedImage (JApplet extends JComponent):

    JComponent whatToDraw = ...;
    BufferedImage img = new BufferedImage(whatToDraw.getWidth(), 
            whatToDraw.getHeight(), BufferedImage.TYPE_INT_RGB);
    whatToDraw.printAll(img.getGraphics());
    

    And to write its data to JPEG file:

    ImageIO.write(img, "jpg", new File("something.jpg"));
    

提交回复
热议问题