Advertisement

[java] image transparency

Started by July 11, 2000 05:59 PM
7 comments, last by virtuosity 24 years, 5 months ago
How do I specify a certain color in an image is to be drawn transparently? Thanks in advance, virtuosity
I just made an applet that does alpha transparency.
Here is the steps that you need to take:

1. get the pictures pixels.
use PixelGrabber to send the pixels into an int array

2. put the pixels into an ImageProducer
you can use MemoryImageSource for this

3. make an Image from the producer
Component.createImage(ImageProducer) or Toolkit.createImage(Producer)

4. modify the pixels
for each pixel in the array {  int alpha = (pixelArray[x] & 0xff000000)>>24;  int red = (pixelArray[x] & 0x00ff0000)>>16;  int green = (pixelArray[x] & 0x0000ff00)>>8;  int blue = (pixelArray[x] & 0x000000ff);  //do something with the pixels  // if they fall within a certain range  // then alpha = 0  pixelArray[x] = (alpha<<24) | (red<<16) | (green<<8) | blue;}  


then in your paint routine call Image.flush() to get the image that you made with createImage to get it's pixels fromm the producer again.

Here is an applet that gradually turns an Image transparent, then apparant, perhaps i should have done it with just one color... I fiddeled around with this code and got it to work with 1.1. All that was needed was to get rid of the BufferedImage object in favor of an Image object. Also, I noticed that it looked quite different in the plugin mode vs the browser native mode, have a look.

http://www.tapinternet.com/~mark/applet/DirectDraw.html
http://www.tapinternet.com/~mark/applet/DirectDraw_plug.html

source
http://www.tapinternet.com/~mark/applet/alpha.zip


Edited by - Jim_Ross on July 12, 2000 2:16:34 AM
Advertisement
How would one create a blended transparency between two images rather than turning off pixels as in your approach.
i do some very quick and dirty alpha blending:

get the pixel format info...
(i''ll use 5-6-5 16bit to illustrate, but it works for 24/32/15bit also)

pretend:
red = 1111 1000 0000 0000
green = 0000 0111 1110 0000
blue = 0000 0000 0001 1111

now make an ''inverse top-bit mask'':
this is just a 0 in the top bit for each color.
wMixMask = 0111 1011 1110 1111

then in your code:

result = pixel1 >> 1 & wMixMask; // 50%
result += pixel2 >> 1 & wMixMask; // +50% = 100%

if you want more info, email me or icq me. if you want source, let me know...
Java2 supports the 8 most popular ways to alpha-blend (of the 12 possible). Just use SRC_OVER it works best. You set up a AlphaComposite and set it in the Graphics2D Object before you paint. It's pretty easy, if your interested I'll post a piece of code to do it.

One wierd thing that might pop up is you might need to save the old AlphaComposite with the G2.getComposite() method because if you accidently leave all your rendering on transparent, your game will be all screwed up. It does look kind of cool though, if there's a lot of stuff moving around. I was thinking of using this effect when the player's game is over, as everything blends together and has a trippy look.

Also I forgot to mention that is a lot easier and probably faster to just use a gif for transparency, instead of manually looking at each pixel with a WritableRaster or similar class.

Edited by - Icculus on July 12, 2000 1:02:27 PM
author of the Helping Phriendly Book
Has anyone had problems implementing alpha-blend in browsers using java applets?
Advertisement
quote: Original post by Anonymous Poster

Has anyone had problems implementing alpha-blend in browsers using java applets?


I have, it doesn''t seem to work in netscape 4.72 for linux, which uses jdk1.1.5. It''s a toss up, you can require the plugin and alienate users, or you can write 1.1 code and still alienate users. Overall, I found the quality of the browser plugin to be better. I would much rather have site use the browser plugin because there is less chance of a crash. I have the linux java plugin but if site just use an applet tag, i''m stuck with buggy 1.1.5.
quote: Original post by Jim_Ross

I have the linux java plugin but if site just use an applet tag, i''m stuck with buggy 1.1.5.


Did Blackdown just recently come out with one? Can you tell me where I could find it?

joeG
joeG
quote: Original post by joeG

Did Blackdown just recently come out with one? Can you tell me where I could find it?

joeG


I''m pretty sure that IBM''s newest jre - 1.3.0 - comes with a browser plugin. Yeah I''m looking at it right now, javaplugin.so. I think the one I got before I got this jre was from blackdown.org.

This topic is closed to new replies.

Advertisement