dooxe
dooxe

Reputation: 1490

How to create transluent window in Java swing?

I work with java and I want to make a translucent window (like a black with 80% alpha). The basis of my code is those in the following post: How to make a transparent JFrame but keep everything else the same?

The problem is that when I run my program, the window is translucent but as you may see on the following picture, the background color (magenta) appears only on the text, not on the white. enter image description here Even worse, when I choose a black with transparency Color(0,0,0,125) as drawing color, nothing appears on the screen.

Here is what I have so far :

public class DssWindow extends JFrame
{
    /**
     * 
     */
    private static final long serialVersionUID = 2455172975892566000L;

    /**
     * 
     */
    public DssWindow()
    {
        super("My frame");

        setUndecorated(true);   
        setBackground(new Color(0,0,0,0));
        setAlwaysOnTop(true);
        JPanel contentPane = new JPanel()
        {
            @Override
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                if(!(g instanceof Graphics2D))
                {
                    return;
                }
                Graphics2D gr = (Graphics2D)g.create();
                gr.setColor(new Color(255,0,255,200));
                gr.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        contentPane.setOpaque(false);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        contentPane.setPreferredSize(new Dimension(screen.width,screen.height));
        setContentPane(contentPane);
        pack();
    }
}

Do someone knows/has a clue of what is the problem ? I am working on Linux (may this is a part of the answer).

Thank you.

Upvotes: 1

Views: 109

Answers (1)

dooxe
dooxe

Reputation: 1490

Ok, my problem is stated here: Java Window Translucency on Linux Graphics drivers seems to be the cause of this behaviour.

Upvotes: 1

Related Questions