1

Is it possible to create a transparent color overlay in Java? It should also cover the taskbar and toolbar. The following code does not work. It just creates a black window in full screen.

public class Overlay extends Window {
    private static final long serialVersionUID = 1 L;
    public Overlay(Window owner) {
        super(owner);
    }

    public void show() {
        try {
            setVisible(true);
            setBackground(new Color(0, 0, 0, 0.5 f));
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
        } catch (Exception error) {
            // Error
        }
    }
}

Thanks!

phflack
  • 2,729
  • 1
  • 9
  • 21
Ood
  • 1,445
  • 4
  • 23
  • 43

1 Answers1

1

This code works for me but sometimes throws an exception (per 10 runs 9 successes). I tested it on other computer and it fails always. Anyway i post it because maybe it will be helpful and you figure it out what is to be done to make it work 100%.

import javax.swing.*;
import java.awt.*;

public class Overlay extends Window {
    private static final long serialVersionUID = 1L;
    public Overlay(Window owner) {
        super(owner);
    }

    public void showIt() {
        try {
            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
            setVisible(true);
            SwingUtilities.invokeLater(() -> {
                setBackground(new Color(255,255,255,124));
            });
        } catch (Exception error) {
            System.out.println(error);
        }
    }

    public static void main(String[] args) {
        new Overlay(null).showIt();
    }
}

The result is full screen window with opacity about 50%.

Edit: Window behaviour depends on the platform. I tested on Linux Mint 18.3 Cinnamont 64-bit.

Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
  • Thanks for the answer! However this produces a grey window for me. I am assuming this is a platform dependent issue on the way MacOS handles full screen. – Ood Mar 15 '18 at 16:53