java透明度,带有透明度的java全屏窗口

I am trying to create a fullscreen window that cover the whole screen using Java. This window must also have some transparency (about 30%-50% transparent). When saying whole screen, I do mean it cover everything (including the dock/taskbar/menubar in OSX/Linux/Windows), and when I say with transparancy, I mean a real-time transparancy and not just a hacked screenshot. Here is what I am aware-of/tried:

Using Java Fullscreen API: while it creates a true fullscreen, you cannot have some transparency with it (only opaque color). One hack is to take a screenshot of the whole desktop and set it as background for the window, but this mean it is not real-time transparency.

Setting window size to match screen dimension: while it fills the whole screen, in certain OSes (e.g. Mac OS X) the window will be rendered behind the dock/menubar, and not above it. However, transparency do work here.

Using setWindowOpacity API: it work in the second case, but not in the first (Fullscreen API)

Using setBackground with alpha: it work like the setWindowOpacity, but only in certain OSes. But also doesn't work with Fullscreen API.

Use JFrame/JWindow/JDialog/Frame/Window: tried every window model I could, without any luck

So I am asking if this is possible through a another hack that I am not aware of, then I would be happy to hear about.

The goal is to overlay a semi-transparent fullscreen over the desktop.

解决方案

is possible only with visible TaskBar e.i.

.

GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

otherwise you got and exception

.

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:

The effects for full-screen windows are not supported.

or by using brutte_force to DirectX freezed my PC twicw, only power_off to save PC's GPU

import com.sun.awt.AWTUtilities;

import java.awt.Dimension;

import java.awt.GraphicsEnvironment;

import java.awt.Rectangle;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class JFrameOpacityExample {

private JFrame myFrame = new JFrame("Test Frame");

private boolean opacity = true;

private boolean resize = true;

private JButton button = new JButton("Opacity");

private JButton button1 = new JButton("Resize");

public JFrameOpacityExample() {

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

Object src = evt.getSource();

if (opacity) {

AWTUtilities.setWindowOpacity(myFrame, 0.50f);

opacity = false;

} else {

AWTUtilities.setWindowOpacity(myFrame, 1.0f);

opacity = true;

}

}

});

button1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

Object src = evt.getSource();

if (resize) {

Rectangle dim = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

int h = dim.height;

int w = dim.width;

myFrame.setBounds(00, 00, w, h);

resize = false;

} else {

myFrame.setBounds(100, 100, 400, 400);

resize = true;

}

}

});

JPanel panel = new JPanel();

panel.add(button);

panel.add(button1);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

myFrame.add(panel);

myFrame.setSize(400, 400);

myFrame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

JFrameOpacityExample jFrameOpacityExample = new JFrameOpacityExample();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值