Java transparent window

Viewed 9109

I am trying to create a circle-shaped window that follows the mouse and pass clicks to the underlying windows.

I was doing this with Python and Qt (see Python overlay window) but then I switched to Java and Swing. However I'm not able to make the window transparent. I tried this method but it doesn't work, however I think that my system supports the transparency because if I start Screencast-O-Matic (which is in Java), the rectangle is actually transparent.

How can I achieve something like that? (I'm on Linux KDE4)

5 Answers

I was also facing the same problem. After hours of searching, I finally found the problem! These are the lines you must write, if you want to make a transparent JFrame:

public void enableTransparentWindow(float opacity) {
        GraphicsEnvironment ge =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        f.setLocationRelativeTo(null);
        f.setBackground(new Color(0, 0, 0));
        //If translucent windows aren't supported, exit.
        f.setUndecorated(true);

        if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
            System.err.println(
                    "Translucency is not supported");
            System.exit(0);
        }
        f.setOpacity(opacity);
    }

Don't forget to call the setVisible() method after this code.
Happy Coding!

Related