Alpha Color on Images?

Okay, Let's see if anyone knows this. Let's say I have a Image and I want to make the background transparent. It's certainly possible to load a transparent GIF89A, but say I want a normal gif and I want to "transparentize" it in code. Here's a code I have:

//Precondition: 'global' is a predefined JApplet

// 'opaque' is a predefined Image with w,h > 0,0

//Postcondition: An Image is returned with the background (as defined

//as the color of the top-left pixel) replaced by the GIF89A

//"transparent color"

public Image makeTransparent(Image opaque)

{

//makes an Image that is the same size as 'opaque'

Image trans = global.createImage( opaque.getWidth(global), opaque.getHeight(global) );

//gives me a graphics object to draw on this image

Graphics g = trans.getGraphics();

<redraw image substituting transparentfor bg>

}

I need to know how to do the <>. My most obvious response would be to go though the Image's array of colors (the way an Image is stored) and change the "bg" color to transparent. But that brings up two questions. (a) Is it possible to access and change that array, and (b) What is the "transparent color" and can the Color object hold it?

If anyone can answer this question or has another algorithm, I am offering "duke dollars" for this.

[1726 byte] By [FortisVenalitera] at [2007-10-2 18:23:05]
# 1

Use java.awt.image.BufferedImage im=new BufferedImage(someWidth, someHeight, BufferedImage.TYPE_INT_ARGB);

Then use im.setRGB(x, y, colorBits) where colorBits is a 4-byte integer with the first byte being red, the second green, the third blue, and the fourth is your alpha value, which would be 0 in the case of a completely transparent picture.

The java.awt.Color object can hold transparent values. Use new java.awt.Color(red, green, blue, alpha).

Hope this helps.

Updownquarka at 2007-7-13 19:43:51 > top of Java-index,Desktop,Core GUI APIs...
# 2
Okay, I'm not at the current PC that I can check this out at but that makes sense. I'm going to try it out this evening. Hope it works, and you'll get the duke dollars.
FortisVenalitera at 2007-7-13 19:43:51 > top of Java-index,Desktop,Core GUI APIs...
# 3
Didn't get a chance to do it last night. I did most though. To byte-map the integer like you said (ARGB), would I just logical-or them together, such as 0|0|0|0?
FortisVenalitera at 2007-7-13 19:43:51 > top of Java-index,Desktop,Core GUI APIs...
# 4

Great news! It worked! Here is the algorithm: All you need is a BufferedImage. It isn't really as inefficient as it may look, either, but don't use it every frame.

Color bg = new Color(bimage.getRGB(0,bimage.getHeight(null)-1), false);

for(int xxx=0;xxx<bimage.getWidth(null);xxx++)

{

for(int yyy=0;yyy<bimage.getHeight(null);yyy++)

{

if(new Color(bimage.getRGB(xxx,yyy), false).equals(bg))

bimage.setRGB(xxx,yyy,0&0&0&0);

}

}

>

FortisVenalitera at 2007-7-13 19:43:51 > top of Java-index,Desktop,Core GUI APIs...
# 5

I want to subclass the JLabel object and give it a graduated background. I'm new to Java, but as I understand it, I should be able to create the background graphic using this technic, graduating from the user selectable color to whatever the background is, and then apply it to the JLabel using prepareImage, etc.

Am I on the right track - or have I gotten completely derailed?

Neeeeeola at 2007-7-13 19:43:51 > top of Java-index,Desktop,Core GUI APIs...