带有透明度的java全屏窗口 [英] java fullscreen window with transparency

查看:176
本文介绍了带有透明度的java全屏窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java创建一个覆盖整个屏幕的全屏窗口。此窗口还必须具有一定的透明度(约30%-50%透明)。在说整个屏幕时,我的意思是它涵盖了所有内容(包括OSX / Linux / Windows中的dock / taskbar / menubar),当我说透明时,我的意思是实时透明,而不仅仅是一个被黑客攻击的截图。以下是我所知道的/尝试过:

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:


  • 使用Java Fullscreen API:虽然它创建了一个真正的全屏,但你不能有一些透明度用它(只有不透明的颜色)。一个黑客是截取整个桌面的屏幕截图并将其设置为窗口的背景,但这意味着它不是实时透明度。

  • 设置窗口大小以匹配屏幕尺寸:当它填满整个屏幕时,在某些操作系统(例如Mac OS X)中,窗口将呈现在dock / menubar后面,而不是在它上面。但是,透明度在这里有效。

  • 使用setWindowOpacity API:它适用于第二种情况,但不适用于第一种情况(全屏API)

  • 使用带有alpha的setBackground:它的工作方式与setWindowOpacity类似,但仅限于某些操作系统。但也不适用于Fullscreen API。

  • 使用JFrame / JWindow / JDialog / Frame / Window:尝试了每个窗口模型,没有任何运气

  • 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.

推荐答案


  • 只能使用可见的TaskBar ei

  • GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    




    • 否则你得到和例外

    • Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
      The effects for full-screen windows are not supported. 
      

      或使用 brutte_force DirectX 冻结我的PC twicw,只有power_off来保存PC的GPU

      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();
                  }
              });
          }
      }
      

      这篇关于带有透明度的java全屏窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆