Transparent images. What am I doing wrong?

For a game I have created an VolatileImage as a backbuffer. I have tried to blit transparent gif images to this backbuffer and this works ok. The sprite is accelerated. When I try to create my own transparent BufferedImage the sprite works, but is not accelerated. I know that it can be done. So what im I doing wrong.

Below is the part of the code that blits the sprite to the backbuffer. the gif image is accelerated, but my own Image is not accelerated.


public class TestTransparent 
{    
    private Image img = null;
    
    /** Creates a new instance of TestTransparent */
    public TestTransparent(GraphicsConfiguration graphConfig) 
    {
//      img = Toolkit.getDefaultToolkit().getImage("G:\\trans.gif");
        
        img = graphConfig.createCompatibleImage(400, 400, Transparency.BITMASK);
    }
    
    public void draw(Graphics g, int x, int y)
    {
        g.drawImage(img, x, y, null);
    }
}

Actually, I thought I read that transparancy isn’t supported with volitile image (accellerated images) so if there’s transparancy indicated in the image, the ‘automatic’ image will fall back to a buffered image (ie. non-accellerated). I could be wrong tho.

Makes sense, tho, that the vram operations isn’t going to take transparancy into account…it’s just going to do a straight blit.

-Chris

It is true, that transparency doesn’t work on VolatileImages. It is however possible to do hardware acceleration on images with 1bit transparency. This requires a “workaround” that I have read in another post. However I can’t seem to get this working.

Not sure, I think I’ll defer to someone more ‘in the know’. From your example, is the image from createCompatableImage accellerated? What if you don’t create it with Transparency.BITMASK?

-Chris

Alpha compositing is not accelerated.

1 bit alpha masking can be done, though. I don’t know how to manuallly create an accelerated masked image. I’ve tried in many ways and the only way I got it working is by loading a gif file that has transparency in it.

This one can be blitted in a volatile image and remain accelerated.

I’ve have a sprite engine at work that works fine with this (roughly 100 frames per seconds running a about 80 sprites in 800x600x32). But as soon as there’s an alpha image being blitted in there, the frame rate drops dramatically (drawing a 200x200 square at 50% opacity dropped me at 4 fps).

So, I’m looking for another way to do this (or for JDK1.5 to come out :wink: )

Images created with
GraphicsConfiguration.createCompatibleImage(w,h,Transparency.BITMASK) will be accelerated (after the first copy from it to the backbuffer or screen) giving there’s enough vram and you’re not running with your desktop in 8-bit depth mode.

I have been able to create 1-bit transparent accelerated images with gif’s and 8-bit color indexed png files. Creating 1-bit transparent accelerated images with “Transparency.BITMASK” has failed however.

Anyway I want to be able to create images with at least a colordepth of 16-bit and with 1-bit transparency. Is it possible (and so yes. how?) to create 16-bit indexed images (png files) in wich one color represents transparency and are these images accelerated?

Second question: Has anyone a full working example of creating an 1-bit transparent accelerated image with “Transparency.BITMASK”?

Try something like this… I havn´t tried it just coded it from memory (that isn´t that good)…


import .....

public class TestTransparent  
{     
    private Image img = null; 
     
    /** Creates a new instance of TestTransparent */ 
    public TestTransparent(GraphicsConfiguration graphConfig)  
    { 
Image tempImage = Toolkit.getDefaultToolkit().getImage("G:\\trans.gif"); 
   int spriteW = tempImage.getWidth(null);
   int spriteH = tempImage.getHeight(null);
   img = graphConfig.createCompatibleImage(spriteW, spriteH, Transparency.BITMASK); 
   Graphics2D g = (Graphics2D)img.getGraphics();
   g.drawImage(tempImage, null, null);
   g.dispose();
    } 
     
    public void draw(Graphics g, int x, int y) 
    { 
   g.drawImage(img, x, y, null); 
    } 
} 

That should work. Hope it helps.

/Backmask

Drawing an accelerated “gif” image into an Image with Transparency.BITMASK does not accelarate this Image.

The only ways that I have found to create an working accelerated image with 1-bit transparency is by creating an gif or png file with an 8-bit indexed palette in wich one color is specified as transparent.

I’am now using indexed png files as a workaround. What also could work is creating an BufferdImage of type TYPE_BYTE_INDEXED. I havend tried it yet but it seems to fit the patern.

Has anybody else simular experiences with transparent images?

Could you try to create an isolated example which shows the problem?

I can assure you that the images created with createCompatibleImage(w,h, Transparency.BITMASK) will be accelerated unless you’re doing something with the image which forces us to punt on acceleration (like grabbing the raster, or rendering to this image on every frame), or if there’s not enough resources.

Also, which jdk version are you using?
OS, video board?