Rendering Swing-components into bufferedImage?

Is it possible to render Swing-components to bufferedImage so that size and location of all components are defined by layoutmanager(s) in that image by using active rendering, overriding the RepaintManager with a null version and setting setIgnoreRepaint(true) for all Swing components?

EDIT:

Maybe I should first think myself before posting anything, but I think this will do it:

// Paint some component:
Dimension size = myComponent.getSize();
BufferedImage myImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) myImage.createGraphics();
myComponent.paint(g2);

// Or paint all components (Swing):
frame.getLayeredPane().paintComponents(g2);

frame.getLayeredPane().paintComponents(g2) seems to work with bufferstrategy and active rendering.

Another question:
Is there any way to read Bufferstrategy´s backbuffer into bufferedimage without calling BufferStrategy.show()?

[quote]Is there any way to read Bufferstrategy´s backbuffer into bufferedimage without calling BufferStrategy.show()?
[/quote]
Yes, but its a hideous hack - and requires the permission “readDisplayPixels” to be set (so it won’t work in unsigned Applets for example)

The way to do it, is create your own class that implements the Composite interface. (as Compositing operations require access to the destination Raster)

You can then do something like :-


PixelReadbackComposite prc = new PixelReadbackComposite();
Graphics2D g2d = (Graphics2D)bufferStrategy.getDrawGraphics();
g2d.setComposite(prc);

g2d.fillRect(0,0,getWidth(),getHeight()); // causes the Composite to have its getCompositeContext(...) called,
// which in turn has its compose(Raster,Raster, WritableRaster) called. (from which you can get the bufferStrategy's contents)

Image bufferStrategyContents = prc.getDestination();