transparency

I was looking for hours to find something in api that can make the shapes i draw transperent (non-colored. That means: i have a image covered with black rectangle, and i want to reveal certain parts of it using a transperent shape ill put within the black rectangle limits (therefore making part of the image obsereveable). I was looking for hours in google.com, and in java api and just found nothing :|

Can you help me please?

Thanks in advance

[465 byte] By [Aviva] at [2007-10-2 2:59:30]
# 1

Don't be so disappointed, you've been looking for something which is transparent, remember? :-)

Got this little baby for you

http://www2.adultreviews.net/request/qualitypics/30186_04.jpg

public BufferedImage getTransparentImage(BufferedImage im,

Color transParencyColor) {

Color transparentColor = new Color(198, 198, 198, 0);

BufferedImage bim = new BufferedImage(im.getWidth(), im.getHeight(),

BufferedImage.TYPE_INT_RGB);

bim.getGraphics().drawImage(im, 0, 0, this);

BufferedImage bimTransp = new BufferedImage(im.getWidth(), im.getHeight(),

BufferedImage.TYPE_INT_ARGB);

Graphics2D g2D = (Graphics2D) bimTransp.getGraphics();

for (int y = 0; y < bim.getHeight(); y++) {

for (int x = 0; x < bim.getWidth(); x++) {

if (bim.getRGB(x, y) == transParencyColor.getRGB()) {

//System.out.println("Transparent pixel found!");

g2D.setComposite(AlphaComposite.Src);

g2D.setColor(transparentColor);

g2D.drawRect(x, y, 1, 1);

} else {

//System.out.println("Not transp");

g2D.setColor(new Color(bim.getRGB(x, y)));

g2D.drawRect(x, y, 1, 1);

}

}

}

return bimTransp;

}

MortenHjerlHansena at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 2
haha they will know i didnt write that :]but thanks man, i'll just try to realize how that works :] (They throw on us a project and the hardest thing i know is how to paint rectangles :] )thanks :]
Aviva at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 3
hahah. Employers nowadays are unpredictable. Rectangles eh? The next thing is sure to be ovals. Terrific!
MortenHjerlHansena at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 4

ok ok :\

i understand parts of it, and yet couldnt understand how it works nor what it does. (espically all those AlphaComposite.src :\ that sounds very high to me).

also i didnt fully understand the Color transParencyColor and the new Color(198,198,198,0).

if i could i have explanations (if thats not a trouble of course) i would be glad to see them :]

Aviva at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 5
Not employee :] im a pupil :(
Aviva at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 6

If I understand myself correctly I have no clue as to what AlphaComposite.src means. I simply use it as a stripper would occasionally use a flute. Sometimes I read the API docs to amuse myself...

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/AlphaComposite.html

To me it contains a Ganges of indian words inaccessible to mortal speech. Each time I say something in return to that document it insults my intelligence. Why bang our heads against the wall? I don't wan't to dig into things! I want to use the API and make some great software instead.

Here's what this method does:

public BufferedImage getTransparentImage(Image im,

Color transParencyColor) {

// make a color which will be transparent. 198=red, 198=green, 198=blue, 0=transparency (0=completely transparent)

Color transparentColor = new Color(198, 198, 198, 0);

// copy the Image 'im' onto a BufferedImage which supports getRGB(). note that the type is TYPE_INT_RGB which means that it doesn't contain any transparent pixels

BufferedImage bim = new BufferedImage(im.getWidth(), im.getHeight(),

BufferedImage.TYPE_INT_RGB);

bim.getGraphics().drawImage(im, 0, 0, this);

// make the image to be returned. same size as 'im' of course. now the type is TYPE_INT_ARGB. This is important, because it means that it is capable of having transparent pixels

BufferedImage bimTransp = new BufferedImage(im.getWidth(), im.getHeight(),

BufferedImage.TYPE_INT_ARGB);

// get a 'pencil' i.e. a Graphics2D so we can draw some transparent pixels on 'bimTransp'

Graphics2D g2D = (Graphics2D) bimTransp.getGraphics();

// step through every single pixel on 'bim'

for (int y = 0; y < bim.getHeight(); y++) {

for (int x = 0; x < bim.getWidth(); x++) {

// if we find a color of 'transParencyColor' make this pixel transparent

if (bim.getRGB(x, y) == transParencyColor.getRGB()) {

//System.out.println("Transparent pixel found!");

g2D.setComposite(AlphaComposite.Src);

g2D.setColor(transparentColor);

g2D.drawRect(x, y, 1, 1);

} else {

//System.out.println("Not transp");

g2D.setColor(new Color(bim.getRGB(x, y)));

g2D.drawRect(x, y, 1, 1);

}

}

}

return bimTransp;

}

There you go. Good luck.

MortenHjerlHansena at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 7
O dearest hat!
MortenHjerlHansena at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 8

True :] though i meant that couldnt understand even from the api but what the heck :] this little section of code is very usfull :]

just need to change the fact it will change from certain location instead of when detecting a certain color.

however, THANKS :] you helped me greatly :D

Aviva at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...
# 9
You're welcome.Best regardsMorten Hjerl-Hansen
MortenHjerlHansena at 2007-7-15 21:25:36 > top of Java-index,Other Topics,Java Game Development...