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]

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;
}
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.