Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Using Java 2D API with SWT...
Using Java 2D API with SWT... [message #447215] Thu, 09 December 2004 16:25
Cyril Chéné is currently offline Cyril ChénéFriend
Messages: 19
Registered: July 2009
Location: France
Junior Member
------------QFb71h7z24gZ1WuVCqHZEm
Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15
Content-Transfer-Encoding: Quoted-Printable

Hello

I wanted to use some API of Java 2D with my SWT components (fillOval wit=
h =

a GradientPaint).

Using the article "Java 2D imaging for the Standard Widget Toolkit" (fou=
nd =

at http://www-128.ibm.com/developerworks/library/j-2dswt/), I encountere=
d =

some problems.

First, I had to fix a bug (ArrayIndexOutOfBounds exceptions systematical=
ly =

raised) when transferring bits from AWT image to SWT image. The new code=
=

is attached. I don't know why I had this bug... Did anybody encouter it =
?

Second, the result was quite disappointing because of "refresh" problems=
.. =

When I move a GEF figure drawn with SWT API over a GEF figure drawn with=
=

Java2D API, the latter is not refreshed correctly.

Has anybody tried the example, experienced the same problems, and found =
a =

solution ?

Thanks

Cyril
------------QFb71h7z24gZ1WuVCqHZEm
Content-Disposition: attachment; filename=Graphics2DRenderer.java
Content-Type: application/octet-stream; name=Graphics2DRenderer.java
Content-Transfer-Encoding: Quoted-Printable

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Display;

/**
* Helper class allowing the use of Java 2D on SWT or Draw2D graphical c=
ontext.
* =

* @author cchene
*/
public class Graphics2DRenderer
{
/**
* =

*/
private static final PaletteData PALETTE_DATA =3D new PaletteData(0xF=
F0000,
0xFF00, 0xFF);

/**
* =

*/
private BufferedImage _awtImage;

/**
* =

*/
private Image _swtImage;

/**
* =

*/
private ImageData _swtImageData;

/**
* =

*/
private int[] _awtPixels;

/**
* RGB value to use as transparent color
*/
private static final int TRANSPARENT_COLOR =3D 0x123456;

/**
* Prepare to render on a SWT graphics context.
*/
public void prepareRendering(GC gc)
{
org.eclipse.swt.graphics.Rectangle clip =3D gc.getClipping();
//FIX: do not use clip.x and clip.y
prepareRendering(clip.width, clip.height);
}

/**
* Prepare to render on a Draw2D graphics context.
*/
public void prepareRendering(org.eclipse.draw2d.Graphics graphics)
{
org.eclipse.draw2d.geometry.Rectangle clip =3D graphics
.getClip(new org.eclipse.draw2d.geometry.Rectangle());
//FIX: do not use clip.x and clip.y
prepareRendering(clip.width, clip.height);
}

/**
* Prepare the AWT offscreen image for the rendering of the rectangul=
ar
* region given as parameter.
*/
private void prepareRendering(int clipW, int clipH)
{
// check that the offscreen images are initialized and large enoug=
h
checkOffScreenImages(clipW, clipH);
// fill the region in the AWT image with the transparent color
java.awt.Graphics awtGraphics =3D _awtImage.getGraphics();
awtGraphics.setColor(new java.awt.Color(TRANSPARENT_COLOR));
//FIX: use (0,0) as coordinates
awtGraphics.fillRect(0, 0, clipW, clipH);
}

/**
* Returns the Graphics2D context to use.
*/
public Graphics2D getGraphics2D()
{
if (_awtImage =3D=3D null)
{
return null;
}
return (Graphics2D) _awtImage.getGraphics();
}

/**
* Complete the rendering by flushing the 2D renderer on a SWT graphi=
cal
* context.
*/
public void render(GC gc)
{
if (_awtImage =3D=3D null)
{
return;
}

org.eclipse.swt.graphics.Rectangle clip =3D gc.getClipping();
transferPixels(clip.x, clip.y, clip.width, clip.height);
gc.drawImage(_swtImage, clip.x, clip.y, clip.width, clip.height, c=
lip.x,
clip.y, clip.width, clip.height);
}

/**
* Complete the rendering by flushing the 2D renderer on a Draw2D gra=
phical
* context.
*/
public void render(org.eclipse.draw2d.Graphics graphics)
{
if (_awtImage =3D=3D null)
{
return;
}

org.eclipse.draw2d.geometry.Rectangle clip =3D graphics
.getClip(new org.eclipse.draw2d.geometry.Rectangle());
//FIX: use (0,0) instead of clip.x and clip.y
transferPixels(0, 0, clip.width, clip.height);
//FIX: use (0,0) instead of clip.x and clip.y
graphics.drawImage(_swtImage, 0, 0, clip.width, clip.height,
clip.x, clip.y, clip.width, clip.height);
}

/**
* Transfer a rectangular region from the AWT image to the SWT image.=

*/
private void transferPixels(int clipX, int clipY, int clipW, int clip=
H)
{
int step =3D _swtImageData.depth / 8;
byte[] data =3D _swtImageData.data;
_awtImage.getRGB(clipX, clipY, clipW, clipH, _awtPixels, 0, clipW)=
;
for (int i =3D 0; i < clipH; i++)
{
int idx =3D (clipY + i) * _swtImageData.bytesPerLine + clipX * =
step;
for (int j =3D 0; j < clipW; j++)
{
int rgb =3D _awtPixels[j + i * clipW];
for (int k =3D _swtImageData.depth - 8; k >=3D 0; k -=3D 8)
{
data[idx++] =3D (byte) ((rgb >> k) & 0xFF);
}
}
}
if (_swtImage !=3D null)
{
_swtImage.dispose();
}
_swtImage =3D new Image(Display.getDefault(), _swtImageData);
}

/**
* Dispose the resources attached to this 2D renderer.
*/
public void dispose()
{
if (_awtImage !=3D null)
{
_awtImage.flush();
}
if (_swtImage !=3D null)
{
_swtImage.dispose();
}
_awtImage =3D null;
_swtImageData =3D null;
_awtPixels =3D null;
}

/**
* Ensure that the offscreen images are initialized and are at least =
as large
* as the size given as parameter.
*/
private void checkOffScreenImages(int width, int height)
{
int currentImageWidth =3D 0;
int currentImageHeight =3D 0;
if (_swtImage !=3D null)
{
currentImageWidth =3D _swtImage.getImageData().width;
currentImageHeight =3D _swtImage.getImageData().height;
}

// if the offscreen images are too small, recreate them
if (width > currentImageWidth || height > currentImageHeight)
{
dispose();
width =3D Math.max(width, currentImageWidth);
height =3D Math.max(height, currentImageHeight);
_awtImage =3D new BufferedImage(width, height,
BufferedImage.TYPE_INT_ARGB);
_swtImageData =3D new ImageData(width, height, 24, PALETTE_DATA=
);
_swtImageData.transparentPixel =3D TRANSPARENT_COLOR;
_awtPixels =3D new int[width * height];
}
}
}
------------QFb71h7z24gZ1WuVCqHZEm--
Previous Topic:vertical sliding tabs
Next Topic:Formatted Text
Goto Forum:
  


Current Time: Thu Oct 29 13:38:51 GMT 2020

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

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

Back to the top