Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I do a graduated fade on an image?

Status
Not open for further replies.

modalman

Programmer
Feb 14, 2001
156
GB
Hi. I have a Graphics object in an Applet that I am placing images onto. I would like to then perform a graduated tint across the complete image. Is it possible and how do I go about it. Many thanks in advance.
 
Create a mask BufferedImage with TYPE_INT_RGBA, paint a gradient from (0, 0, 0, 0) to (255, 255, 255, 255) with the values being (Red, Green, Blue, Alpha = transparency) (I'll call it mask). Check out Java2D (the Graphics2D class, have a look at the methods g.setPaint(...) with a GradientPaint object and g.fillRect(...) for that). Create a buffer BufferedImage (buffer). And create an Image that contains your image data (image).

The following code will blend in the image (I did not check this code, please post if it does not work):

Code:
buffer.drawImage(image, x, y, width, height);
buffer.setComposite(AlphaComposite.DstIn);
buffer.drawImage(mask, x, y, width, height);

x and y values are up to you, width and height in fact too (the image will be scaled), but you can also obtain them from your image (if it's a BufferedImage) easily via the image.getSize().width and image.getSize().height values. It it is not a BufferedImage you will most probably be able to pass "this" in any component's paint(g) method.

Hope this helps, please post if you need more explanation.
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
I created such a program before. Mail to me if you want to have a look at the codes.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top