Capturing the raster of Swing components without actually displaying them?

My goal is to be able to create an Image/BufferedImage that represents the image of Swing components. Now one solution would be to display the component, take a printscrn of it, and cut the resulting image down so that it only contains the area that the component takes up. However, this requires the component to be displayed to the screen.

Looking for a solution, I found createImage() in java.awt.Component. I tried that, and got a null image. Going back to the javadocs, I found that the component had to be displayable, which was defined as being visible or packed. I tried inserting the component into a JFrame, packed the frame, and then called createImage, only to get a blank Image. I've also tried displaying the JFrame, calling createImage, and displaying the image, but I've had no luck.

To display the Images, I've been encapsulating them in ImageIcons and displaying those.

Any ideas pertaining to a good solution?

[957 byte] By [RATiXa] at [2007-10-2 16:12:53]
# 1
Just make a new BufferedImage directly
tjacobs01a at 2007-7-13 16:59:41 > top of Java-index,Desktop,Core GUI APIs...
# 2
How would I go about doing that? I'm looking through the API, but I can't find anything relevant.
RATiXa at 2007-7-13 16:59:41 > top of Java-index,Desktop,Core GUI APIs...
# 3
Paint your component into a BufferedImage.
hiwaa at 2007-7-13 16:59:41 > top of Java-index,Desktop,Core GUI APIs...
# 4

> How would I go about doing that? I'm looking through

> the API, but I can't find anything relevant.

If you direcytly want to create Image object .. then check.. this... and see my last posting of Brio.java

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=711032

Note: use setVisible(true);

before creating buffer = createImage(...) else U will get null Image..

U can also make a BufferedImage like..

BufferedImage buffer = new BufferedImage(200, 200, BufferedImage.TYPE_3BYTE_BGR);

Graphics g = buffer.getGraphics();

g.setColor(new Color(255, 0, 255));

g.drawImage(img, x, y, null);

g.fillRect(200,200);

g.dispose();

gervinia at 2007-7-13 16:59:41 > top of Java-index,Desktop,Core GUI APIs...