Search code examples
javaswingbufferedimagegraphics2d

How to draw existing graphics to a BufferedImage - Java


I a have a JPanel where I have drawn some lines. Now I want to pass those graphics as a BufferedImage to another class, but I don't know how. I know the way to get the graphic2D object of a BufferedImage and draw on it, but how to do the reverse?


Solution

  • You need to create a BufferedImage of the panel.

    Check out the Screen Image class. It allows you to create an Image of any Swing component.