Search |
||
Java Doodle: fading translucent windows, on PC & MacPosted by joshy on June 6, 2008 at 3:05 PM PDT
This is the next in my series of Java Doodles. There is a link to my previous one in the references below. This time I'm going to show you how to make a translucent window by setting the opacity value using new apis in JavaSE 6 update 10. However, I'm also going to show you how to make it fade in when you mouse over it, similar to some popular chat applications, as well as work properly on the Mac and degrade gracefully when running versions of Java. The basic ideaHere is a screenshot of the basic app. It's a simple translucent window. I've you've read the recent SDN article then you probably already know the basic APIs. This app will add Mac support, a rollover effect, and show you how to degrade gracefully.
Degrading gracefullyTurning on translucency with Java SE 6 is as simple as calling a new private API:
Now, if the method doesn't exist at runtime because the user has an older version of Java then the exception will be caught and nothing will happen. It degrades gracefully. Mac supportSo what about the Mac? Well, Mac OS X doesn't have a version of Java SE 6 update 10 yet, however in Leopard they did introduce a new API for doing translucency. Using this new support we can add translucency on Mac with a single extra line. In the catch block above we can add this:
This sets a magic property on the rootpane of the frame called The fade effectNow that we can make a window translucent lets do something interesting with it. I want the window to be mostly transparent, but whenever the mouse moves into the window I want to return to mostly opaque. So let's say the opacity will go from 0.5 to 0.9. I could set a mouse listener on the background of th window, but that wouldn't work on subcomponents. I could also put in a glasspane to check for mouse events, but then I would have to redispatch all events back down to the real components as I did in my book Swing Hacks. Very messy. Fortunately, as of Java 5, we have an easier method: just poll the global mouse position using the
The code above will check the mouse position ten times a second. If the mouse has moved in or out of the window then it changes the opacity. It's that simple. Run the appYou can try the live application here. It will work on MacOSX 10.5 or Windows/Solaris/Linux if you have a recent beta of JavaSE 6 update 10. On older versions of Java 6 you won't get the translucency effect, but the app will still run.
That's all there is to it. This is just one of the many cool new features in JavaSE 6 update 10. Go check'em out then come back to my blog for more Java Doodles in the future. References»
Comments
Comments are listed in date ascending order (oldest first)
Submitted by jwenting on Mon, 2008-06-09 23:55.
Josh, fixing a bug isn't changing an API. And if the method responds incorrectly to colours with transparancy that's a bug that needs to be fixed. Of course the behaviour of the API will change, but that's the nature of bugfixes, to change the behaviour of APIs or applications so they work as specified...
Submitted by cowwoc on Mon, 2008-06-09 19:23.
Polling the global mouse position? ... Polling? Seriously, there has to be a better way. Why isn't there a global mouse position event listener?
Submitted by mediarazzo on Mon, 2008-06-09 08:59.
I'm curious to know why Sun didn't fix JComponent.setBackground(Color c) where Color has an alpha parameter. That would seem to be an obvious solution.
It is a nice example though.
Submitted by joshy on Mon, 2008-06-09 09:05.
mediarazzo changing the definition of setBackground() would effectively be changing the API, which is not allowed in an update release. However, that method or something like it may end up being the final API which is included in JavaSE 7.
Submitted by shemnon on Fri, 2008-06-06 17:34.
I notice this screenshot is on OSX. Can you comment yet on the availability of 6u10 on Mac yet? Now or after next week?
Submitted by shemnon on Fri, 2008-06-06 17:36.
I mean, you can't keep using these if platform workarounds on WORA stuff forever, c an you?
Submitted by joshy on Fri, 2008-06-06 17:49.
I have no news on 6u10 for Mac. However, as this blog shows, you can do some of the u10 features on Mac already.
Submitted by ant001 on Sat, 2008-06-07 04:04.
If its a com.sun class doesn't that mean it most probably wont be implemented on other OS's?
It's a fun Doodle; What about custom shaped windows?
|
||
|
|