13

I want to make the JFrame transparent, but the image on top of it to be non-transparent. This is what I have now:

enter image description here

Does anyone know a way to make only the JFrame transparent?

Here's my code:

import javax.swing.*;
import java.awt.*;
import com.sun.awt.AWTUtilities;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class SplashDemo extends JFrame
{
    public SplashDemo()
    {
        setUndecorated(true);
        setSize(200, 200);

        add(new JLabel(new ImageIcon("puppy2.png"))); 
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        setOpacity(0.85f);      
    }

     public static void main(String[] args) 
     {
        new SplashDemo();
     }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
r1nzler
  • 2,533
  • 8
  • 28
  • 30

1 Answers1

29

Basically, you need to make a transparent window and a translucent content pane. This will mean anything added to the content pane will continue to be rendered without additional alphering...

enter image description here

public class TranscluentWindow {

    public static void main(String[] args) {
        new TranscluentWindow();
    }

    public TranscluentWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }

                    JWindow frame = new JWindow();
                    frame.setAlwaysOnTop(true);
                    frame.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            if (e.getClickCount() == 2) {
                                SwingUtilities.getWindowAncestor(e.getComponent()).dispose();
                            }
                        }
                    });
                    frame.setBackground(new Color(0,0,0,0));
                    frame.setContentPane(new TranslucentPane());
                    frame.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("/Puppy.png")))));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        });
    }

    public class TranslucentPane extends JPanel {

        public TranslucentPane() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.85f));
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());

        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    +1, cool. Haven't played much with JDK7. I didn't realize it was so easy to make a top level window transparent. It looks like all you need to do is use a trasparent background Color. – camickr Feb 18 '13 at 05:22
  • @camickr That's the basics (I've not checked for compatibility with the `GraphicsConfiguration`, but it's much simpler then the AWTUtilities method from Java 6) – MadProgrammer Feb 18 '13 at 05:23
  • I have an issue with this code : if I try moving around the puppy, the frame is not properly repainted, meaning that previous puppies are not cleared before painting a new one. Any hint to avoid this behaviour? – Sharcoux Sep 17 '17 at 14:30
  • @Sharcoux What OS? What JRE? – MadProgrammer Sep 17 '17 at 21:52
  • @Sharcoux Are you trying to move the image or the frame? – MadProgrammer Sep 17 '17 at 21:56
  • Well, ok then. It's better to make a separate question. See https://stackoverflow.com/questions/46273464/background-not-refreshed-when-painting-over-a-transparent-jframe – Sharcoux Sep 18 '17 at 07:08