user2157432
user2157432

Reputation: 1

Transparent JFrame in java swing

I need to create a transparent jframe in java swing which will have an opacity of 0.05f. I tried the code below but it doesn't work. I work in windows.

What do I need to do to make it work?

import java.awt.AlphaComposite;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import java.awt.Color;


public class BackgroundNull {

   private JFrame frame;

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                BackgroundNull window = new BackgroundNull();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public BackgroundNull() {
    initialize();

private void initialize() {
    frame = new JFrame();
    frame.setBackground(new Color(0, 0, 0, 0));
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setOpacity(0.5f);
}

    public void paint(Graphics g) { 
        Graphics2D g2 = (Graphics2D) g.create(); 
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f)); 

        frame.getContentPane().paint(g2); 
        g2.dispose(); 
    }
}

Upvotes: 0

Views: 401

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Depending on your platform, Window transparency may only work with the default (Metal) look at feel.

I tried using the system look and feel on Mac OSX and Windows 7 and either would work.

The part you are missing in your code is this...

JFrame.setDefaultLookAndFeelDecorated(true);

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() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                TranslucentWindowDemo tw = new TranslucentWindowDemo();

                // Set the window to 55% opaque (45% translucent).
                tw.setOpacity(0.55f);

                // Display the window.
                tw.setVisible(true);
            }
        });
    }
}

Now, here's the sad news. Under Java 6, you can make it work.

The following code (under Java 6) will make a native Window transparent...

public static void setOpacity(Window window, float opacity) {
    try {
        Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
        if (awtUtilsClass != null) {
            Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
            method.invoke(null, window, opacity);
        }
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}

Upvotes: 0

Related Questions