Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Transparent "Image" objects in SWT
Transparent "Image" objects in SWT [message #460924] Sun, 11 September 2005 10:05 Go to next message
Eclipse UserFriend
Originally posted by:

I want to create a drawing programm with layers using SWT, but having
problems with transparency of "Image". My problem is, that I don't know
how to define a color as transparent or draw transparent area.
Transparency is easy with GIF or PNG files, because it is included in the
file, but in my case I can't use existing images, because the DrawingArea
must redraw every time it changes.

The DrawingArea of my program is a "Canvas" and is made up of two "Image"
objects, called "passiveGround" (background, grid aso.) and "acitveGround"
(drawing aso.). I store the DrawingArea in two seperate objects, because
each layer takes very long to redraw ("passiveGround" doens't change very
often unlike "activeGround"). If the whole DrawingArea needs to redraw,
than first "passivGround" should be copied and afterwards "activeGround"
should be layed above.

When I redraw "activeGround", the Image should first be filled with a
transparent color and afterswards each figure should be drawn.

The code looks like this:

public class DrawingArea
extends Canvas
   Image passiveGround, activeGround;
   public void redrawPassiveGround()
      passiveGround = new Image( getDisplay(), getBounds());
      GC gcPassive = new GC( passiveGround);

      gcPassive.draw... // drawing routines
   public void redrawPassiveGround()
      Image activeGround = new Image( getDisplay(), getBounds());
      GC gcActive = new GC( activeGround);

      gcActive.fill_with_transparent_color(); // how to?
      gcActive.draw... // drawing routines
   class MyPaintListener
   implements PaintLister
      public void paintControl( PaintEvent paintEvent)
         GC gc = paintEvent.gc;
         gc.drawImage( passiveGround, 0, 0);
         gc.drawImage( activeGround, 0, 0);

In several books I only find solution with "PaletteData", in which every
color is set individually and than every pixel is set individually, or
something difficult with "alpha mask". This solution won't help me much,
when I want to draw with "GC", because how do I the color in
"setForeground()", when the color is is definded in PaletteData?
Re: Transparent "Image" objects in SWT [message #460934 is a reply to message #460924] Mon, 12 September 2005 12:41 Go to previous messageGo to next message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
I think this is a misunderstanding of how transparency works, and colours in general.

An image, at its lowest form, is a collection of 'Red', 'Green' and 'Blue' values for each pixel (picture element) in the image. The size of the red/blue/green data is dependent on the operating system's capabilities; for example, there may be 3 bits of red, 3 bits of blue, and 2 of green. In general, these bitmaps have a fixed size of each pixel. These days, 24-bit colours are the norm (so there is 8 bits for each of red, blue and green) but older systems may only be able to support 12-bit colour (4 bits each), or even just black and white.

Since graphics have been around a lot longer than colour displays have been 24-bit, compromises were made to approximate colours in an image. For example, there are 255 separate colours defined bt HTML that you can use that equate to different colours (a subset of which is known as the 'web-safe' colours). These range from white to black, and the exact mapping between the value (e.g. 3) and the colour (e.g. yellow) are defined from a standard palette (e.g.

In GIFs and PNGs, it is possible for an entry to be marked as 'no pixel at all'. This is done in one of two ways:

1) A special value, like 0, is marked to be the 'transparent' colour. Instead of drawing the colour black, the pixel will be left blank. GIFs use this method.

2) A seperate Alpha channel that dictates how much of the value to be shown. If there is a 1-bit alpha channel, then it's assumed to be fully visible or fully invisible.

In fact, most graphics formats (e.g. PNGs) use alpha rather than a special value to represent transparency. 32-bit colours don't actually add any more colour realism than 24-bit colours do, but they do have an extra 8 bits of transparency that allows you to have a fade-out in the image (e.g.

Back to the question; in this case, the Image data is assumed to be a fully normalised (i.e. with no palette data). Since it's only the palette that tells the image which special colours are supposed to be transparent, the transparency isn't filled (and importantly, it's not possible to fill something with 'the transparent colour'). What you'd have to do is set the alpha channel for the image with the alpha data for this to work correctly.

It will only be transparent if there was nothing there beforehand, so you might want to clear the area during a redraw operation as well.
Re: Transparent "Image" objects in SWT [message #460954 is a reply to message #460934] Tue, 13 September 2005 06:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

In my case, the colors are limited to some standard 4-bit colors.

1) alpha channel: a little overkill, since I only need binary
transparency (visible or not) and I don't want to define the whole alpha
channel everytime I redraw the image.

2) bit mask: ...has binary transparency, but I still have to define every
pixel of the whole mask to be transparent or not. I think thats not the
way it's meant to be used with "GC".

3) palette: excatly what I want, but I don't know how to use it with

let's assume the following code:

Color TRANSPARENT_COLOR = getDisplay().getSystemColor( SWT.COLOR_BLACK);
Color DRAWING_COLOR = getDisplay().getSystemColor( SWT.COLOR_RED);

PaletteData paletteData = new PaletteData( new RGB[]{

ImageData imageData = new ImageData( width, height, 4, paletteData);
imageData.transparentPixel = 0; // index of the palette

Image image = new Image( getDisplay(), imageData);

GC gcImage = new GC( image);

gcImage.setBackground( ??? );
gcImage.setForeground( ??? );
gcImage.fillRectangle( image.getBounds()); // clear image with transparent 
gcImage.draw... // drawing routines


???: The setBackground() and setForeground() methods only accept a "Color"
object here, but the valid colors are defined in the palette. So how can I
tell "GC" to use "color number 2 of the palette"? I think that
"gcImage.setBackground( TRANSPARENT_COLOR);" is not the way it's meant to
be, because what is the palette than good for?
Re: Transparent "Image" objects in SWT [message #460976 is a reply to message #460954] Tue, 13 September 2005 14:24 Go to previous message
Alex Blewitt is currently offline Alex BlewittFriend
Messages: 946
Registered: July 2009
Senior Member
My point was, there is no concept of a palette at the operating system image level. Rather, it's a function of the graphic that you've loaded. So what you're trying to achieve can't be done with Color. You have to use an alpha channel if you have the image data loaded. Hell, it's trivial to write a process that runs through all the image data, when it finds a specific value (e.g. 0) it then masks it with the alpha channel (col || 0xff000000).
Previous Topic:TableTree details help
Next Topic:hover help for dialog page does not wrap
Goto Forum:

Current Time: Mon Sep 23 10:56:51 GMT 2019

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

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

Back to the top