HUDs on Windows (finally)
June 19, 2009
I finally got around to making the Heads Up Display (HUD) use transparency on non-Mac platforms. I wanted to keep compatibility with Java 5, so I decided to use reflection to try and gracefully use the com.sun.awt.AWTUtilities.setWindowOpaque method. Here are the two utility methods I use to make a window non-opque:
/** * Try's to make the given {@link Window} non-opqaue (transparent) across * platforms and JREs. This method is not guaranteed to succeed, and will fail * silently if the given {@code Window} cannot be made non-opaque. * * This method is useful, for example, when creating a HUD style window that * is semi-transparent, and thus doesn't want the window background to be * drawn. * @param window the {@code Window} to make non-opaque. */ public static void makeWindowNonOpaque(Window window) { // on the mac, simply setting the window's background color to be fully // transparent makes the window non-opaque. window.setBackground(new Color(0, 0, 0, 0)); // on non-mac platforms, try to use the facilities of Java 6 update 10. if (!PlatformUtils.isMac()) { quietlyTryToMakeWindowNonOqaque(window); } } /** * Trys to invoke {@code com.sun.awt.AWTUtilities.setWindowOpaque(window, false)} * on the given {@link Window}. This will only work when running with JRE 6 update 10 * or higher. This method will silently fail if the method cannot be invoked. * @param window the {@code Window} to try and make non-opaque. */ private static void quietlyTryToMakeWindowNonOqaque(Window window) { try { Class clazz = Class.forName("com.sun.awt.AWTUtilities"); Method method = clazz.getMethod("setWindowOpaque", java.awt.Window.class, Boolean.TYPE); method.invoke(clazz, window, false); } catch (Exception e) { // silently ignore this exception. } }
This enhancement will be part of Mac Widgets for Java 0.9.5, which will hopefully be out in the next few weeks. If you want access earlier, you can either download the code and build it from the Subversion repository, or you can email me and I’ll send you the latest jar file.
June 19, 2009 at 4:31 pm
hi…
i am currently trying to move your changes into my maven source base for macwidgets.
I will build a verion 0.95 and you will find it here:
http://maven.javastream.de/com/explodingpixels/
you can also use this url as a maven repository for using mac-widgets within a maven project.
June 19, 2009 at 4:38 pm
Sounds good… thanks for doing that Philipp.
June 19, 2009 at 6:29 pm
Looks great. But on windows the close widget should go on the other side. :-)
June 19, 2009 at 6:34 pm
Fair enough! (I’ve updated the button accordingly)
June 19, 2009 at 11:20 pm
good work man :)
June 21, 2009 at 11:53 am
Nice going!
June 22, 2009 at 6:15 am
Note that with the latest JDK7 builds setting window background on non-Mac platforms is enough as well, and you don’t have to use AWTUtilities methods…
June 22, 2009 at 9:32 am
Nice… thanks for the tip Artem.
July 16, 2009 at 10:13 pm
[…] Orr posts that his heads-up display is now available on Windows with proper […]
August 21, 2009 at 6:33 pm
Ken,
Great job on the HUDs. I am currently using mac widgets on mac, windows, and linux and was wondering if there were any plans or you had any tips on how to make the HUDs transparent in Linux.
Thanks!
Ryan
August 21, 2009 at 6:46 pm
Hi Ryan,
If you’re using Java 6 update 10 or greater on Linux, you should automatically get the transparency. If not, let me know what version of Java your running with.
-Ken
November 20, 2009 at 3:12 pm
Might be a stupid question but how to I set how transparent the HUD is?
Also does this degrade fine in Java 5 on windows?
November 20, 2009 at 3:22 pm
There is no way to change the transparency of the HUD. This visual attribute is defined by Apple’s user interface guidelines.
-Ken