Attempting to set transparency in an imported bmp image
Looking at a previous response to a similar question to this, I developed this code:
public static BufferedImage getImage(String imagefile) {
    BufferedImage image = null;
    try {
        image = javax.imageio.ImageIO.read(new java.io.File(imagefile));    
        int trans = image.getRGB(0,0);
        final int width = image.getWidth();
        int[] imgData = new int[width];
        for (int y = 0; y < image.getHeight(); y++) {
            // fetch a line of data from each image
            image.getRGB(0, y, width, 1, imgData, 0, 1);
            for (int x = 0; x < width; x++)
                if (imgData[x] == trans)
                    imgData[x] |= 0xFF000000;
            // replace the data
            image.setR开发者_开发知识库GB(0, y, width, 1, imgData, 0, 1);
        }           
    } catch (Exception e) {
            e.printStackTrace();
    }
    return image;
}
In it, the idea is to take the upper left hand color and apply transparency on all pixels that matches that color in the image. The problem is when I do
g.drawImage(img, across, down, cells, cells, null);
I still get the color that is suppose to be transparent. Am I forgetting a step somewhere?
I used a bmp file to do the test.
Thank you for your time.
When I use alpha values in a BufferedImage I create the image using:
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
So maybe you need to copy/convert all the pixels to an image that is aware of the alpha values?
Got it:
public static BufferedImage getImage(String imagefile) {
    BufferedImage image = null;
    BufferedImage image_copy = null;
    try {
        image = javax.imageio.ImageIO.read(new java.io.File(imagefile));    
        image_copy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        int trans = image.getRGB(0,0);
        final int width = image.getWidth();
        int[] imgData = new int[width];
        for (int y = 0; y < image.getHeight(); y++) {
            // fetch a line of data from each image
            image.getRGB(0, y, width, 1, imgData, 0, 1);
            for (int x = 0; x < width; x++)
                if (imgData[x] == trans)
                    imgData[x] &= 0x00FFFFFF;
            // replace the data
            image_copy.setRGB(0, y, width, 1, imgData, 0, 1);
        }           
    } catch (Exception e) {
            e.printStackTrace();
    }
    return image_copy;
}
Thanks for the help!
Looks like it was a logical error.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论