Howto create transparent Images?

Hi there!

I have to create transparent images, but I do not know howto do this at all.

I already tried offscreen-images created with Component.createImage() and did a fillRect with a transparent color, but it did not seem to work well.
When blitting them the whole area is overpainted, with no respect to the transparent areas :frowning:

What I want to do is to create a image which is transparent except in the areas where I overpainted it.

1.) Is there a way to do this hardware-accerlated?
2.) Is this also possible with buffered-images under 1.2+?

I know this is a bit childish question, but I googled and did not found anything…

Thank you in advance, lg Clemens

Use a BufferedImage:


BufferedImage(int width, int height, int imageType) 
          Constructs a BufferedImage of one of the predefined image types.

Where imageType = BufferedImage.TYPE_INT_ARGB

This will give you a full alpha channel image type so you can set the alpha to your desired transparency. I think this will only work if the display depth is 32bit though.

typically what I use is this:


public static BufferedImage createBI (int width, int height, int transparency) {            
            return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, transparency);
}

I keep that accessible somewhere, and call it with the “transparency” argument being Transparency.TRANSLUCENT. That’ll work for you no matter what bit depth or OS you’re running on, and if there’s any chance that it can be accelerated, that method will get it accelerated.

Translucent images are never accellerated. If you want an image with transparent capabilities (not translucent), use Transparency.BITMASK instead. These images can/will be accellerated.

Actually, they could quite possibly be in the 1.5 OpenGL pipeline.

Kev

well the buffered images i create with getGraphicsConfiguration().createCompatibleImage();
say that they arent accelerated if i ask them: Img.getCapabilities(getGraphicsConfiguration()).isAccelerated();
while using -Dsun.java2d.opengl=True
however… drawing with them is with this option quiet fast… but drawing into sucks really

You may get accelleration through OpenGL pipeline, or through DirectDraw/Direct3D on windows with the following flags:

-Dsun.java2d.translaccel=true
-Dsun.java2d.ddforcevram=true // needed pre 1.5

However, in my experience, this is only a workable solution for your own personal machine. There are quite a few unforseeable sideeffects and artifacts when running code using these flags, and ironing out these problems is usually only possible on your own computer with your own set of hardware.

An “idiot proof” implementation of accellerated translucency in java has yet to show itself, at least to my knowledge. However, I hope to be proved wrong! :slight_smile: