Undecorated JFrame shadow
Asked Answered
W

2

2

How do you add a shadow to a undecorated jframe?

From what I found online, you might be able to add the jframe to another black translucent window to give a shadow effect. Or somehow apply something like this to a JFrame:

    Border loweredBorder = new EtchedBorder(EtchedBorder.LOWERED);
    setBorder(loweredBorder);

Either way I just want to know the best method or maybe a completely different way of getting the same effect like extending from another class and not jframe. I'm new to Java so I might be going down the wrong direction so any advice is appreciated.

Waiter answered 30/9, 2013 at 22:52 Comment(3)
Technically, you can't add a frame to any other container, but the idea is in the right direction...Damico
not even a JInternalFrame?Waiter
JInternalFrame is itself a container, unlike JFrame (or anything that extends JWindow)Damico
D
8

Basically, you need to make a series of layers.

  • JFrame
  • ShadowPanel
  • and content...

enter image description here

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

public class ShadowWindow {

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

    public ShadowWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                frame.setBackground(new Color(0, 0, 0, 0));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(new ShadowPane());

                JPanel panel = new JPanel(new GridBagLayout());
                panel.add(new JLabel("Look ma, no hands"));

                frame.add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ShadowPane extends JPanel {

        public ShadowPane() {
            setLayout(new BorderLayout());
            setOpaque(false);
            setBackground(Color.BLACK);
            setBorder(new EmptyBorder(0, 0, 10, 10));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
            g2d.fillRect(10, 10, getWidth(), getHeight());
            g2d.dispose();
        }
    }
}
Damico answered 1/10, 2013 at 0:34 Comment(0)
N
0

MadProgammer,

frame.setBackground(new Color(0, 0, 0, 0));

is useless if you don't use also:

frame.setOpacity(1.0f);

Setting the default close operation to EXIT_ON_CLSE is bad, the app should close naturely with the end of both processes, so DISPOSE_ON_CLOSE is the right way, reserving the other one to "fix" bugs.

frame.setContentPane(new ShadowPanel());

is useless if you use also (and after):

frame.add(panel);

ShadowPanel's constructor should start with:

super(new GridBagLayout());

instead of:

setLayout(new GridBagLayout());

Additionnally, using a GridBagLayout to only add a single component is a bit disproportionned, isn't it ? What about a GridLayout(1, 1) or even a lazy BorderLayout ?

Return a new Dimension on each call on the preferred size (many of them are done by the machine, during a lot of operation, like repainting, etc) is very expensive. You should return a variable defined in the constructor or the class.

Why override getPreferredSize(), but not getBackground(), nor isOpaque() ?

GraphicsD should be placed inside a try block to dispose of it inside a finally block.

Newsworthy answered 2/11, 2020 at 4:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.