Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » AWT component into SWT image
AWT component into SWT image [message #443938] Mon, 04 October 2004 18:39 Go to next message
Jaakko Lindvall is currently offline Jaakko LindvallFriend
Messages: 9
Registered: July 2009
Junior Member
Hi,

I'm trying to make a snapshot of an AWT component and convert it into a
SWT image. All of this should happen offscreen, so I cannot embed the AWT
component into SWT canvas and then take a screenshot.

Also, I didn't get any reasonable content at all (only black), when I
tried to create a java.awt.image.BufferedImage image and use
awtComponent.paint(image.getGraphics()).

How can I do this? My apologies if the question is slightly off topic.

Regards,
Jaakko Lindvall
Re: AWT component into SWT image [message #443942 is a reply to message #443938] Mon, 04 October 2004 19:42 Go to previous messageGo to next message
Veronika Irvine is currently offline Veronika IrvineFriend
Messages: 1272
Registered: July 2009
Senior Member
See:
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet156.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup

"Jaakko Lindvall" <jaakko.lindvall@pp.inet.fi> wrote in message
news:opsfczghxaqvihzt@news.eclipse.org...
> Hi,
>
> I'm trying to make a snapshot of an AWT component and convert it into a
> SWT image. All of this should happen offscreen, so I cannot embed the AWT
> component into SWT canvas and then take a screenshot.
>
> Also, I didn't get any reasonable content at all (only black), when I
> tried to create a java.awt.image.BufferedImage image and use
> awtComponent.paint(image.getGraphics()).
>
> How can I do this? My apologies if the question is slightly off topic.
>
> Regards,
> Jaakko Lindvall
Re: AWT component into SWT image [message #443946 is a reply to message #443942] Mon, 04 October 2004 23:32 Go to previous messageGo to next message
Jaakko Lindvall is currently offline Jaakko LindvallFriend
Messages: 9
Registered: July 2009
Junior Member
Veronika Irvine wrote:
> See:
> http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet156.java?rev=HEAD& amp;content-type=text/vnd.viewcvs-markup
>

Handy snippet. However, it seems that one _must visibly render_ those
AWT components in order to take the snapshot. That ruins the idea of
taking the snap in AWT and converting the BufferedImage into SWT Image.

I wonder if there is some way for SWT_AWT to mimic rendering the AWT
component but not really show it? Though it seems that Swing is the only
toolkit in which offscreen rendering of widgets is easy -- being native
seems to be a curse in this case.
Re: AWT component into SWT image [message #444053 is a reply to message #443938] Tue, 05 October 2004 21:03 Go to previous messageGo to next message
Jeff Myers is currently offline Jeff MyersFriend
Messages: 396
Registered: July 2009
Senior Member
Jaakko Lindvall wrote:
> Hi,
>
> I'm trying to make a snapshot of an AWT component and convert it into a
> SWT image. All of this should happen offscreen, so I cannot embed the
> AWT component into SWT canvas and then take a screenshot.
>
> Also, I didn't get any reasonable content at all (only black), when I
> tried to create a java.awt.image.BufferedImage image and use
> awtComponent.paint(image.getGraphics()).
>
> How can I do this? My apologies if the question is slightly off topic.
>
> Regards,
> Jaakko Lindvall

Have you checked to see if it's a problem with the contents of the
BufferedImage, or if it's a problem with the conversion you're doing to
the SWT image?

You might want to see how the VE is doing this sort of thing:
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.ve.jfc/ vm_jfcvm/org/eclipse/ve/internal/jfc/vm/ImageDataCollector.j ava?rev=1.1&content-type=text/vnd.viewcvs-markup&cvs root=Tools_Project
in the start() method.

Specifically, you could try doing:
Image collectedImage = awtComponent.createImage(awtComponent.getWidth(),
awtComponent.getHeight());
Graphics g = collectedImage.getGraphics();
awtComponent.printAll(g);
g.dispose();

- Jeff
Re: AWT component into SWT image [message #444446 is a reply to message #443938] Tue, 12 October 2004 23:00 Go to previous messageGo to next message
Charlton Rose is currently offline Charlton RoseFriend
Messages: 7
Registered: July 2009
Junior Member
This is a multi-part message in MIME format.
--------------050008090608050005030402
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

I saw the snippets referred to by the other posters for creating SWT
images from AWT images. They looks interesting, if not complex. I've
implemented similar routines for converting AWT images to SWT. I use one
adaptor for opaque images and another for images with an alpha channel.
You may find these easier to use than the other snippets.

Both the RGB and ARGB adaptors share a common superclass:

import java.awt.image.BufferedImage;
import org.eclipse.swt.graphics.Image;

public abstract class ImageAdaptor extends BufferedImage
{

public ImageAdaptor(int inWidth, int inHeight, int inType)
{
super(inWidth, inHeight, inType);
}

public abstract Image toSwtImage();

}


The idea is that you create an adaptor, which is an AWT image, draw to
it, and then take an SWT snapshot. Here is the RGB implementation:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Display;

public final class RgbImageAdaptor extends ImageAdaptor
{

public RgbImageAdaptor(int inWidth, int inHeight)
{
super(inWidth, inHeight, BufferedImage.TYPE_3BYTE_BGR);
}

public Image toSwtImage()
{
int vnWidth = getWidth();
int vnHeight = getHeight();
int vnDepth = 24;
PaletteData vpPalette = new PaletteData(0xff, 0xff00, 0xff0000);
int vnScanlinePad = vnWidth * 3;
WritableRaster vpRaster = getRaster();
DataBufferByte vpBuffer = (DataBufferByte) vpRaster.getDataBuffer();
byte[] vabData = vpBuffer.getData();
ImageData vpImageData = new ImageData(vnWidth, vnHeight, vnDepth, vpPalette, vnScanlinePad, vabData);
Image vpImage = new Image(Display.getDefault(), vpImageData);
return vpImage;
}

}


The above class is an AWT image. To make use of it, simply instantiate
one and draw on it (using ordinary AWT drawing techniques). You can even
paint Swing components on this image! Then, when you're ready, just call
|toSwtImage()| and you've got an SWT image!

On the other hand, if all you want to do is convert an already existing
AWT image, just use this helper::

public static Image toSwt(java.awt.Image ipImage)
{
RgbImageAdaptor vpAdaptor = new RgbImageAdaptor(ipImage.getHeight(null), ipImage.getWidth(null));
Graphics vpG = vpAdaptor.getGraphics();
vpG.drawImage(ipImage, 0, 0, null);
vpG.dispose();
return vpAdaptor.toSwtImage();
}


I've been using the above code; it works. Now here's the answer to your
question. I suppose you could adapt this code to draw a Swing component
just as easily. /Just make sure you're component has been sized first,
or you may get all black pixels!/

// Warning: Untested code:
public static Image toSwt(Component ipComponent)
{
RgbImageAdaptor vpAdaptor = new RgbImageAdaptor(ipComponent.getWidth(), ipComponent.getHeight());
Graphics vpG = vpAdaptor.getGraphics();
ipComponent.paint(vpG);
vpG.dispose();
return vpAdaptor.toSwtImage();
}


As I mentioned earlier, I also have an image converter for images with
transparency. This also works, but is much slower:

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Display;

public final class ArgbImageAdaptor extends ImageAdaptor
{

public ArgbImageAdaptor(int inWidth, int inHeight)
{
super(inWidth, inHeight, BufferedImage.TYPE_4BYTE_ABGR);
}

public Image toSwtImage()
{
// This implementation is horribly unoptimized.
int vnWidth = getWidth();
int vnHeight = getHeight();
int vnDepth = 32;
PaletteData vpPalette = new PaletteData(0xff, 0xff00, 0xff0000);
int vnScanlinePad = vnWidth * vnDepth / 8;
WritableRaster vpRaster = getRaster();
DataBufferByte vpBuffer = (DataBufferByte) vpRaster.getDataBuffer();
byte[] vabData = vpBuffer.getData();
ImageData vpImageData = new ImageData(vnWidth, vnHeight, vnDepth, vpPalette, vnScanlinePad, vabData);
WritableRaster vpAlphaRaster = getAlphaRaster();
DataBufferByte vpAlphaBuffer = (DataBufferByte) vpAlphaRaster.getDataBuffer();
byte[] vabAlphaData = vpAlphaBuffer.getData();
for (int vnX = 0; vnX &lt; vnWidth; ++ vnX)
for (int vnY = 0; vnY &lt; vnHeight; ++ vnY)
vpImageData.setAlpha(vnX, vnY, 0xff & vabAlphaData[(vnY * vnWidth + vnX) * 4]);
Image vpImage = new Image(Display.getDefault(), vpImageData);
return vpImage;
}

}


I've been using these adaptors for quite some time and have never had
any problems with them. They make it really easy to draw in Swing and
show in SWT. I hope you find them useful. I would also appreciate public
comment on these.



Jaakko Lindvall wrote:

> I'm trying to make a snapshot of an AWT component and convert it into
> a SWT image. All of this should happen offscreen, so I cannot embed
> the AWT component into SWT canvas and then take a screenshot.
>
> Also, I didn't get any reasonable content at all (only black), when I
> tried to create a java.awt.image.BufferedImage image and use
> awtComponent.paint(image.getGraphics()).
>
> How can I do this? My apologies if the question is slightly off topic.


--------------050008090608050005030402
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<p>I saw the snippets referred to by the other posters for creating SWT
images from AWT images. They looks interesting, if not complex. I've
implemented similar
routines for converting AWT images to SWT. I use one adaptor for opaque
images and another for images with an alpha channel.
Re: AWT component into SWT image [message #466203 is a reply to message #444446] Wed, 04 January 2006 19:56 Go to previous message
Florian Huber is currently offline Florian HuberFriend
Messages: 15
Registered: July 2009
Junior Member
Hi,

thanks for the codesnippet. It was very useful. But I got a black
background.
I used your code to convert an awt image (based on a gif) to a swt
image. The result is pretty good, except the background. I guess, the
problem might be within the convertion of the transparency.

If you have any idea how solve this, I'd appreciate this.

Cheers,
Florian

Charlton Rose wrote:
> I saw the snippets referred to by the other posters for creating SWT
> images from AWT images. They looks interesting, if not complex. I've
> implemented similar routines for converting AWT images to SWT. I use one
> adaptor for opaque images and another for images with an alpha channel.
> You may find these easier to use than the other snippets.
>
> Both the RGB and ARGB adaptors share a common superclass:
>
> import java.awt.image.BufferedImage;
> import org.eclipse.swt.graphics.Image;
>
> public abstract class ImageAdaptor extends BufferedImage
> {
>
> public ImageAdaptor(int inWidth, int inHeight, int inType)
> {
> super(inWidth, inHeight, inType);
> }
>
> public abstract Image toSwtImage();
>
> }
>
>
> The idea is that you create an adaptor, which is an AWT image, draw to
> it, and then take an SWT snapshot. Here is the RGB implementation:
>
> import java.awt.Graphics;
> import java.awt.image.BufferedImage;
> import java.awt.image.DataBufferByte;
> import java.awt.image.WritableRaster;
> import org.eclipse.swt.graphics.Image;
> import org.eclipse.swt.graphics.ImageData;
> import org.eclipse.swt.graphics.PaletteData;
> import org.eclipse.swt.widgets.Display;
>
> public final class RgbImageAdaptor extends ImageAdaptor
> {
>
> public RgbImageAdaptor(int inWidth, int inHeight)
> {
> super(inWidth, inHeight, BufferedImage.TYPE_3BYTE_BGR);
> }
>
> public Image toSwtImage()
> {
> int vnWidth = getWidth();
> int vnHeight = getHeight();
> int vnDepth = 24;
> PaletteData vpPalette = new PaletteData(0xff, 0xff00, 0xff0000);
> int vnScanlinePad = vnWidth * 3;
> WritableRaster vpRaster = getRaster();
> DataBufferByte vpBuffer = (DataBufferByte) vpRaster.getDataBuffer();
> byte[] vabData = vpBuffer.getData();
> ImageData vpImageData = new ImageData(vnWidth, vnHeight, vnDepth, vpPalette, vnScanlinePad, vabData);
> Image vpImage = new Image(Display.getDefault(), vpImageData);
> return vpImage;
> }
>
> }
>
>
> The above class is an AWT image. To make use of it, simply instantiate
> one and draw on it (using ordinary AWT drawing techniques). You can even
> paint Swing components on this image! Then, when you're ready, just call
> |toSwtImage()| and you've got an SWT image!
>
> On the other hand, if all you want to do is convert an already existing
> AWT image, just use this helper::
>
> public static Image toSwt(java.awt.Image ipImage)
> {
> RgbImageAdaptor vpAdaptor = new RgbImageAdaptor(ipImage.getHeight(null), ipImage.getWidth(null));
> Graphics vpG = vpAdaptor.getGraphics();
> vpG.drawImage(ipImage, 0, 0, null);
> vpG.dispose();
> return vpAdaptor.toSwtImage();
> }
>
>
> I've been using the above code; it works. Now here's the answer to your
> question. I suppose you could adapt this code to draw a Swing component
> just as easily. /Just make sure you're component has been sized first,
> or you may get all black pixels!/
>
> // Warning: Untested code:
> public static Image toSwt(Component ipComponent)
> {
> RgbImageAdaptor vpAdaptor = new RgbImageAdaptor(ipComponent.getWidth(), ipComponent.getHeight());
> Graphics vpG = vpAdaptor.getGraphics();
> ipComponent.paint(vpG);
> vpG.dispose();
> return vpAdaptor.toSwtImage();
> }
>
>
> As I mentioned earlier, I also have an image converter for images with
> transparency. This also works, but is much slower:
>
> import java.awt.image.BufferedImage;
> import java.awt.image.DataBufferByte;
> import java.awt.image.WritableRaster;
> import org.eclipse.swt.graphics.Image;
> import org.eclipse.swt.graphics.ImageData;
> import org.eclipse.swt.graphics.PaletteData;
> import org.eclipse.swt.widgets.Display;
>
> public final class ArgbImageAdaptor extends ImageAdaptor
> {
>
> public ArgbImageAdaptor(int inWidth, int inHeight)
> {
> super(inWidth, inHeight, BufferedImage.TYPE_4BYTE_ABGR);
> }
>
> public Image toSwtImage()
> {
> // This implementation is horribly unoptimized.
> int vnWidth = getWidth();
> int vnHeight = getHeight();
> int vnDepth = 32;
> PaletteData vpPalette = new PaletteData(0xff, 0xff00, 0xff0000);
> int vnScanlinePad = vnWidth * vnDepth / 8;
> WritableRaster vpRaster = getRaster();
> DataBufferByte vpBuffer = (DataBufferByte) vpRaster.getDataBuffer();
> byte[] vabData = vpBuffer.getData();
> ImageData vpImageData = new ImageData(vnWidth, vnHeight, vnDepth, vpPalette, vnScanlinePad, vabData);
> WritableRaster vpAlphaRaster = getAlphaRaster();
> DataBufferByte vpAlphaBuffer = (DataBufferByte) vpAlphaRaster.getDataBuffer();
> byte[] vabAlphaData = vpAlphaBuffer.getData();
> for (int vnX = 0; vnX &lt; vnWidth; ++ vnX)
> for (int vnY = 0; vnY &lt; vnHeight; ++ vnY)
> vpImageData.setAlpha(vnX, vnY, 0xff & vabAlphaData[(vnY * vnWidth + vnX) * 4]);
> Image vpImage = new Image(Display.getDefault(), vpImageData);
> return vpImage;
> }
>
> }
>
>
> I've been using these adaptors for quite some time and have never had
> any problems with them. They make it really easy to draw in Swing and
> show in SWT. I hope you find them useful. I would also appreciate public
> comment on these.
>
>
>
> Jaakko Lindvall wrote:
>
>> I'm trying to make a snapshot of an AWT component and convert it into
>> a SWT image. All of this should happen offscreen, so I cannot embed
>> the AWT component into SWT canvas and then take a screenshot.
>>
>> Also, I didn't get any reasonable content at all (only black), when I
>> tried to create a java.awt.image.BufferedImage image and use
>> awtComponent.paint(image.getGraphics()).
>>
>> How can I do this? My apologies if the question is slightly off topic.
>
Previous Topic:Select first node in tree?
Next Topic:Launching a standalone swt application from within eclipse
Goto Forum:
  


Current Time: Fri Mar 29 02:27:42 GMT 2024

Powered by FUDForum. Page generated in 0.04420 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top