[Date Prev][Date Next][Index][Thread] [SGT Mail Archive] [SGT Main Page]

How can I convert a JPane into an BufferedImage?



*************************************************
Message from the sgt mail list.
*************************************************

Hi

I would like to convert a JPane into an BufferedImage and have written 
the following method there the comp parameter is the actul JPane.
As a result I only get a greyed image.
Can someone please tell me, how I can produce an image from a JPane 
component ?

***************************************************************************
protected BufferedImage convert(JComponent comp) {
		comp.setDoubleBuffered(false);
		Dimension size = comp.getPreferredSize();
		comp.setSize(size);

		BufferedImage image = new BufferedImage(size.width, size.height, 
BufferedImage.TYPE_4BYTE_ABGR);
		//  (pixelDepth == 8) ? BufferedImage.TYPE_BYTE_INDEXED :
		//   BufferedImage.TYPE_4BYTE_ABGR );
		final Graphics g = image.getGraphics();

		// Workaround for JDK 1.2.2 bug (Linux)
		g.setClip(0, 0, size.width, size.height);

		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					comp.paint(g);
				}
			});
		} catch (Exception x) {
			x.printStackTrace();
		} finally {
			g.dispose();
		}
		return image;
	}
***********************************************************************

-- 
Klaus Hartlage
Math java project:  http://www.hartmath.org
Riding the Java wave: http://www.jsurfer.org


*************************************************
To remove yourself from this mailing list,
send mail to <Majordomo@epic.noaa.gov> with
"unsubscribe sgt" in the message body.