我想要为一款外国应用程序创建HUD样式的显示。
为此,我需要创建一个透明的叠加窗口,该窗口将放置在外部应用程序的窗口之上。这个叠加窗口应该允许我在其上放置小部件和绘制文本。如果事件发生在透明区域上,应将其转发到底层窗口(否则应允许小部件按预期工作)。
我正在使用Java在OSX上完成此操作。我希望能够使用纯Java实现并具有可移植性到其他平台,但如果不可能,我可以接受只在OSX上通过Cocoa(Rococoa)或Carbon实现此目标的解决方案。
Java教程展示了一种实现该效果的方法。这里介绍另一种使用RGBA(红、绿、蓝、透明度)值作为背景色的方法:
public class Overlay {
public static void main(String[] args) {
JFrame frame = new JFrame("Transparent Window");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setAlwaysOnTop(true);
// Without this, the window is draggable from any non transparent
// point, including points inside textboxes.
frame.getRootPane().putClientProperty("apple.awt.draggableWindowBackground", false);
frame.getContentPane().setLayout(new java.awt.BorderLayout());
frame.getContentPane().add(new JTextField("text field north"), java.awt.BorderLayout.NORTH);
frame.getContentPane().add(new JTextField("text field south"), java.awt.BorderLayout.SOUTH);
frame.setVisible(true);
frame.pack();
}
}