Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2003

Tip: Looking for answers? Try searching our database.

Tinting an image

Thread view: 
Jeremy - 24 Aug 2003 03:40 GMT
Hello,
I have a class which extends JLabel, which is basically for an Icon View
kind of a thing.  Minimize your newsreader and click on any item on your
desktop and you will have the idea of what I'm trying to do.

I'm no slouch at java programming, so the component is pretty much done.
There is just one thing I can't figure out:  How can I tint the image when
the icon is highlighted?  I could overlay a rectangle using g.fillRect and a
color with an alpha channel, but that would give me a rectangle around the
icon which I don't want.  I just want to tint the icon, exactly as Windows
does - is there any way I can "mask" before I overlay a rectangle, or,
better yet, a simple way to tint the icon itself?  I have looked everywhere
for a way to do this gracefully and I just can't find it.  I really don't
want to have to go through the icon pixel by pixel and apply the tint - this
shouldn't be that complicated!

Thanks,
-Jeremy
Jeremy - 24 Aug 2003 05:43 GMT
Hello everyone,
I've solved this problem with a bit of tricky programming, so in case anyone
else is wondering how to do this I'm going to post the solution here.  It's
kind of a kludgy hack but it works.

Normally, to paint the icon, you would use this:
icon.paintIcon(c, g, x, y);

where c is the component, g is the Graphics2D context, etc.

To paint the highlighted icon, use this instead:

BufferedImage tmpImage = new BufferedImage(icon.getIconWidth(),
icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D tmpGfx = tmpImage.createGraphics();
icon.paintIcon(c, tmpGfx, 0, 0);    //lay down the original icon
tmpGfx.setXORMode(new Color(0, 0, 80));     //replace this color with your
highlight color of choice

//this will make an area of solid highlight color wherever the icon was
painted (but not where it was transparent)
icon.paintIcon(c, tmpGfx, 0, 0);

tmpGfx.dispose();    //you must free up native resources

icon.paintIcon(c, g, x, y);    //lay down original icon in our main gfx
context.

ImageIcon highlight = new ImageIcon(tmpImage);
Composite tempCmp = g.getComposite;    //save the current composite
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
/*set the alpha blending, you can fiddle with the alpha value if you like*/
highlight.paintIcon(c, g, x, y);    //overlay the highlight
g.setComposite(tempCmp);    //restore the original composite.

That's it.

-Jeremy

> Hello,
> I have a class which extends JLabel, which is basically for an Icon View
[quoted text clipped - 14 lines]
> Thanks,
> -Jeremy


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.