Skip to main content

Translucent volatile image appears opaque

2 replies [Last post]
Joined: 2006-12-09

I create a TRANSLUCENT volatile image, render some stuff in it and then draw the image on to the screen. I expected that the rendering in the image would be "overlayed" over the existing screen graphics and that the regions of the image that I didn't render to would be transparent and not obscure any graphics on the screen.

But that is not the case. Instead I see an opaque black background which obliterates anything already on the screen. The graphics I rendered in the image are appearing but the image itself does not appear to be translucent.

So, is this the expected behaviour? Does a translucent volatile image start off life with an opaque black background? If so, how do I get it to be translucent?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2012-05-30

Create VolatileImage:

public VolatileImage createVolatileImage(int width, int height) {
    return GraphicsEnvironment.getLocalGraphicsEnvironment().
        createCompatibleVolatileImage(width, height, Transparency.TRANSLUCENT);

Make VolatileImage transparent:
public void clearVolatileImage(VolatileImage dstImg) {
    Graphics2D g = dstImg.createGraphics();
    g.fillRect(0, 0, dstImg.getWidth(), dstImg.getHeight());

Draw semi-transparent image to another image:
public void drawSemiTransparentImage(VolatileImage dstImg, VolatileImage srcImg) {
    Graphics2D g = dstImg.createGraphics();
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f));
    g.drawImage(srcImg, 0, 0, null);

The default composite rule is AlphaComposite.SRC_OVER (doc).
More on compositing is here.

Joined: 2005-10-11

This works for me:

public static VolatileImage createVolatileImage(int width, int height, int transparency) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
VolatileImage image = null;

image = gc.createCompatibleVolatileImage(width, height, transparency);

int valid = image.validate(gc);

if (valid == VolatileImage.IMAGE_INCOMPATIBLE) {
image = createVolatileImage(width, height, transparency);
//System.out.println(ImageBank.class.getSimpleName() + ": created new VolatileImage");
return image;
public static VolatileImage createTransparentVolatileImage(int width, int height) {
VolatileImage image = createVolatileImage(width, height, Transparency.TRANSLUCENT);
Graphics2D g = (Graphics2D)image.getGraphics();
g.setColor(new Color(0,0,0,0));
g.fillRect(0, 0, image.getWidth(), image.getHeight());
return image;