javatransparencyawtutilities

transparant background in a jframe but with window border etc


So i can try and ask my problem but for your sake and my sake i have created a simple image in photoshop to try to tell you what i want to achieve:

enter image description here

I have tried to work with the Opaque stuff in AWTUtilities but it does not seem to work do you guys have any idea how i can achieve this??

I do need the native windows border so i can drag and drop the window and also need the resize function.

Thanks in advance


Solution

  • For a transparent/transuculent JFrame see here: http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html which explains translucent Windows in Java.

    You need to call setOpacity().

    Here is also a small example :

    Screenshot for app

    import java.awt.*;
    import javax.swing.*;
    import static java.awt.GraphicsDevice.WindowTranslucency.*;
    
    public class TranslucentWindowDemo extends JFrame {
    
        public TranslucentWindowDemo() {
            super("TranslucentWindow");
            setLayout(new GridBagLayout());
    
            setSize(300, 200);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Add a sample button.
            add(new JButton("I am a Button"));
        }
    
        public static void main(String[] args) {
            // Determine if the GraphicsDevice supports translucency.
            GraphicsEnvironment ge =
                    GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
    
            //If translucent windows aren't supported, exit.
            if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
                System.err.println(
                        "Translucency is not supported");
                System.exit(0);
            }
    
            JFrame.setDefaultLookAndFeelDecorated(true);
    
            // Create the GUI on the event-dispatching thread
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    TranslucentWindowDemo tw = new TranslucentWindowDemo();
    
                    // Set the window to 55% opaque (45% translucent).
                    tw.setOpacity(0.55f);
    
                    // Display the window.
                    tw.setVisible(true);
                }
            });
        }
    }