Home » Eclipse Projects » Standard Widget Toolkit (SWT) » AWT component into SWT image
| | | |
Re: AWT component into SWT image [message #444446 is a reply to message #443938] |
Tue, 12 October 2004 23:00 |
Charlton Rose 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 < vnWidth; ++ vnX)
for (int vnY = 0; vnY < 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 |
Florian Huber 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 < vnWidth; ++ vnX)
> for (int vnY = 0; vnY < 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.
>
|
|
|
Goto Forum:
Current Time: Mon Dec 09 04:19:03 GMT 2024
Powered by FUDForum. Page generated in 0.04096 seconds
|