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

Started by
-1 comments, last by Darrn 18 years, 7 months ago
I want to create a drawing programm with layers using Java/Eclipse/SWT, but having problems with transparency of "org.eclipse.swt.graphics.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
      
      gcPassive.dispose();
   }
   
   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
      
      gcActive.dispose();
   }
   
   class MyPaintListener
   implements PaintLister
   {
      public void paintControl( PaintEvent paintEvent)
      {
         GC gc = paintEvent.gc;
         
         gc.drawImage( passiveGround, 0, 0);
         gc.drawImage( activeGround, 0, 0);
         
         gc.dispose();
      }
   }
}
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?

This topic is closed to new replies.

Advertisement