Advertisement Jump to content
Sign in to follow this  

[java] Transparent "Image" objects in Java/Eclipse/SWT

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a drawing programm with layers using Java/Eclipse/SWT, but having problems with transparency of "". 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? Does someone have experience with SWT and can help me to make transparent "Image" objects?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!