Java中的全屏透明颜色叠加

时间:2018-03-15 13:55:23

标签: java swing awt fullscreen graphic

是否可以在Java中创建透明色彩叠加层?它还应该涵盖任务栏和工具栏。以下代码不起作用。它只是全屏创建一个黑色窗口。

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
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

此代码适用于我但有时会抛出异常(每10次运行9次成功)。我在其他计算机上测试过,它总是失败。无论如何,我发布它,因为它可能会有所帮助,你可以弄清楚要做什么让它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();
    }
}

结果是全屏窗口,不透明度约为50%。

编辑:窗口行为取决于平台。我测试了Linux Mint 18.3 Cinnamont 64位。

相关问题