HUDs on Windows (finally)

June 19, 2009

hud_on_windows

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.

13 Responses to “HUDs on Windows (finally)”

  1. philipp Says:

    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.


  2. Looks great. But on windows the close widget should go on the other side. :-)

  3. dyorgio Says:

    good work man :)

  4. Artem Ananiev Says:

    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…


  5. […] Orr posts that his heads-up display is now available on Windows with proper […]

  6. Ryan Says:

    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

  7. Ken Says:

    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

  8. zammbi Says:

    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?

    • Ken Says:

      There is no way to change the transparency of the HUD. This visual attribute is defined by Apple’s user interface guidelines.

      -Ken


Leave a comment