Paint Clipped Graphics to BufferedImage

E

Eric Dalquist

I have a JPanel and I'm exporting it's painted contents to a
BufferedImage and then writing that to the file system. I would like a
way to be able to export only a portion of the JPanel to the image.
Currenlty I get a Rectangle that contains the entire area that I want
to export. Then I do:

BufferedImage outputImg = new BufferedImage(circuitArea.width,
circuitArea.height, BufferedImage.TYPE_INT_RGB);
Graphics g = outputImg.getGraphics();
g.setClip(circuitArea);
this.circuit.paint(g);

Thinking that by setting the clip area I can save just the clipped
area. Well when I create the image with the width & height of the
clipped area it's origin is at 0,0 and the clipped area gets painted
at the x & y of the rectangle so only some or none of the panel
actually gets painted to the image.

Is there a way I can paint just a selected rectangle from a JPanel to
an image without having to create an image large enough to paint the
entire area and then cropping?
 
B

Babu Kalakrishnan

Eric said:
I have a JPanel and I'm exporting it's painted contents to a
BufferedImage and then writing that to the file system. I would like a
way to be able to export only a portion of the JPanel to the image.
Currenlty I get a Rectangle that contains the entire area that I want
to export. Then I do:

BufferedImage outputImg = new BufferedImage(circuitArea.width,
circuitArea.height, BufferedImage.TYPE_INT_RGB);
Graphics g = outputImg.getGraphics();
g.setClip(circuitArea);
this.circuit.paint(g);

Thinking that by setting the clip area I can save just the clipped
area. Well when I create the image with the width & height of the
clipped area it's origin is at 0,0 and the clipped area gets painted
at the x & y of the rectangle so only some or none of the panel
actually gets painted to the image.

Is there a way I can paint just a selected rectangle from a JPanel to
an image without having to create an image large enough to paint the
entire area and then cropping?


Create an Image with the required smaller size. Apply a Translation
AffineTransform on the Graphics context obtained from it before passing it to
the paint method.

BK
 
E

Eric Dalquist

Thank you, I was looking for something like that, I guess I didn't
realize right away that Image.getGraphics() actually returned a
Graphics2D object. Thanks for the help!

-Eric Dalquist
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top