
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.security.SecureRandom;

public class Test {

    JFrame background;
    BufferedImage foreground;
    Window window;
    volatile boolean gradientBackgroundEnabled = false;
    volatile int gradientWidth = 255;
    volatile int gradientHeight = 255;

    float opacity = 1.0f;
    float perPixelTranslucency = 1.0f;
    static Color BG_COLOR = Color.BLUE;
    static Color FG_COLOR = Color.RED;
    static final int dl = 100;

    public Test(float opacity, float perPixelTranslucency, boolean gradient) throws Exception {
        this.gradientBackgroundEnabled = gradient;
        this.perPixelTranslucency = perPixelTranslucency;
        this.opacity = opacity;
        EventQueue.invokeAndWait(this::initBackgroundFrame);
        EventQueue.invokeAndWait(this::initGUI);
    }

    public void applyShape() {
        gradientWidth = window.getWidth();
        gradientHeight = window.getHeight();
    }

    public static boolean checkTranslucencyMode(GraphicsDevice.WindowTranslucency mode) {

        if (!GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .isWindowTranslucencySupported(mode)){
            System.out.println(mode+" translucency mode isn't supported");
            return false;
        } else {
            return true;
        }
    }

    public void initBackgroundFrame() {
        background = new JFrame();
        background.setUndecorated(true);
        background.getContentPane().setBackground(BG_COLOR);
        background.setSize(500, 500);
        background.setLocation(dl, dl);
        background.setVisible(true);
    }

    public void initGUI() {
        Container contentPane;

        window = new JDialog(background);
        ((JDialog) window).setUndecorated(true);
        contentPane = ((JDialog) window).getContentPane();

        if (perPixelTranslucency < 1.0f) {
            contentPane.setBackground(colorWithOpacity(FG_COLOR, perPixelTranslucency));
            window.setBackground(colorWithOpacity(FG_COLOR, perPixelTranslucency));
        } else {
            contentPane.setBackground(FG_COLOR);
            window.setBackground(FG_COLOR);
        }

        window.setLocation(2 * dl, 2 * dl);
        window.setSize(255, 255);
        window.setPreferredSize(new Dimension(255, 255));
        createSwingComponents();
        if (opacity < 1.0f)
            window.setOpacity(opacity);

        window.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                applyShape();
            }
        });
        applyShape();
        window.setVisible(true);
        window.toFront();
    }

    public void createSwingComponents() {
        Container contentPane;
        if (gradientBackgroundEnabled) {
            JPanel jPanel = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
                    if (g instanceof Graphics2D) {
                        Color background = Color.RED;
                        Paint p = new GradientPaint(0.0f, 0.0f, colorWithOpacity(background, 0),
                                0.0f, gradientHeight - 3, colorWithOpacity(background, 1), true);
                        Graphics2D g2d = (Graphics2D) g;
                        g2d.setPaint(p);
                        g2d.fillRect(0, 3, gradientWidth, gradientHeight - 3);
                    } else {
                        super.paintComponent(g);
                    }
                }
            };
            jPanel.setBorder(new EmptyBorder(15, 5, 5, 5));
            jPanel.setOpaque(false);

            contentPane = jPanel;

            RootPaneContainer.class.cast(window).setContentPane(contentPane);
        } else {
            contentPane = RootPaneContainer.class.cast(window).getContentPane();
        }
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        JTextArea textArea = new JTextArea("JTextArea");
        window.add(textArea);

        JCheckBox checkbox = new JCheckBox("JCheckBox");
        checkbox.setOpaque(false);
        window.add(checkbox);

        JComboBox comboBox = new JComboBox(new String[]{"JComboBox", "Some item"});
        window.add(comboBox);

        JLabel label = new JLabel("JLabel");
        window.add(label);

        JTextField textField = new JTextField("JTextField");
        window.add(textField);
    }

    Color colorWithOpacity(Color color, float opacity) {
        return new Color(color.getColorSpace(), color.getColorComponents(null), opacity);
    }

    public static void main(String[] ignored) throws Exception {
        if (checkTranslucencyMode(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)
            && checkTranslucencyMode(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)
            && checkTranslucencyMode(GraphicsDevice.WindowTranslucency.TRANSLUCENT))
        { new Test(0.7f, 0.0f, true); }
    }
}
