Rufr
Rufr

Reputation: 43

Graphics2D into BufferedImage

I have found many ways to turn a image into a Graphics2d, but I am not sure how to turn my Graphics2d into a BufferedImage. How would I do this.

For example:

Graphics2D SomeGraphics;
SomeGraphics.drawImage(image, x,y,size,size);
SomeGraphics.fillRect(x,y,size,size);

Now how would I convert that Graphics into a Buffered Image?

Upvotes: 1

Views: 4039

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

Now how would I convert that Graphics into a Buffered Image?

Essentially, you can't. You need to start with a BufferedImage and paint to it's Graphics2D context, for example...

BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(25, 25, 50, 50);
g2d.dispose();

The great thing is, the AWT and Swing API's work around the use of Graphics (which Graphics2D extends from), making it really easy to paint components to a BufferedImage

Upvotes: 4

Related Questions