Transparent PNG image on SWT

I have a Composite and I would like to use a png file as a background image. I can do this, the problem is that the image uses transparency, this does not work, and white is displayed instead. Any thoughts on how to make this work?

Thanks!

+3
source share
2 answers

Does this article help?

A look at the SWT image

It talks about how to draw an image (albeit a GIF) Canvaswith transparency ( Canvasextends Composite).

+3
source

! - PNG , . ( : " " ), . Image, , . , , - setRegion, , (, ) , .

ImageData id = new ImageData("basket.png");
Image image = new Image (display, id, id); //3rd parameter is transparency mask
Canvas c = new Canvas (shell, SWT.TRANSPARENT);
c.addPaintListener(
    new PaintListener(){
        public void paintControl(PaintEvent e) 
        {
            e.gc.drawImage(image, 0, 0);
        }
    }
);

//the image has been created, with transparent regions. Now set the active region
//so that mouse click (enter, exit etc) events only fire when they occur over
//visible pixels. If you're not worried about this ignore the code that follows
Region region = new Region();
Rectangle pixel = new Rectangle(0, 0, 1, 1);
for (int y = 0; y < id.height; y++)
{
    for (int x = 0; x < id.width; x++)
    {
        if (id.getAlpha(x,y) > 0)
        {
            pixel.x = id.x + x;
            pixel.y = id.y + y;
            region.add(pixel);
        }
    }
}
c.setRegion(region);
+1

Source: https://habr.com/ru/post/1724486/


All Articles