Java透明窗口
我正在尝试创建一个圆形窗口,它跟随鼠标并将单击传递到底层窗口 我使用Python和Qt(请参阅)来实现这一点,但后来我切换到Java和Swing。但是,我无法使窗口透明。我试过了,但是没有成功,但是我认为我的系统支持透明,因为如果我启动(Java),矩形实际上是透明的Java透明窗口,java,linux,swing,window,transparent,Java,Linux,Swing,Window,Transparent,我正在尝试创建一个圆形窗口,它跟随鼠标并将单击传递到底层窗口 我使用Python和Qt(请参阅)来实现这一点,但后来我切换到Java和Swing。但是,我无法使窗口透明。我试过了,但是没有成功,但是我认为我的系统支持透明,因为如果我启动(Java),矩形实际上是透明的 我怎样才能实现这样的目标?(我在Linux KDE4上)如果您想自己做,而不使用外部库,可以启动一个执行以下操作的线程: 将透明窗口设置为不可见 制作桌面的屏幕截图 将此屏幕截图作为窗口的背景图像 或者您可以使用为什么Java
我怎样才能实现这样的目标?(我在Linux KDE4上)如果您想自己做,而不使用外部库,可以启动一个执行以下操作的线程:
- 将透明窗口设置为不可见
- 制作桌面的屏幕截图
- 将此屏幕截图作为窗口的背景图像
import java.awt.*; //Graphics2D, LinearGradientPaint, Point, Window, Window.Type;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
* From JavaMagazine May/June 2012
* @author josh
*/
public class ShapedAboutWindowDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//switch to the right thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("About box");
//turn of window decorations
frame.setUndecorated(true);
//turn off the background
frame.setBackground(new Color(0,0,0,0));
frame.setContentPane(new AboutComponent());
frame.pack();
//size the window
frame.setSize(500, 200);
frame.setVisible(true);
//center on screen
frame.setLocationRelativeTo(null);
}
}
);
}
private static class AboutComponent extends JComponent {
public void paintComponent(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
//create a translucent gradient
Color[] colors = new Color[]{
new Color(0,0,0,0)
,new Color(0.3f,0.3f,0.3f,1f)
,new Color(0.3f,0.3f,0.3f,1f)
,new Color(0,0,0,0)};
float[] stops = new float[]{0,0.2f,0.8f,1f};
LinearGradientPaint paint = new LinearGradientPaint(
new Point(0,0), new Point(500,0),
stops,colors);
//fill a rect then paint with text
g.setPaint(paint);
g.fillRect(0, 0, 500, 200);
g.setPaint(Color.WHITE);
g.drawString("My Killer App", 200, 100);
}
}
}
如果您使用的是Java6,则需要使用私有API特性。有关更多详细信息,请查看 示例 这是一个有点快的黑客,但它得到了想法
public class TransparentWindow {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
MyFrame frame = new MyFrame();
frame.setUndecorated(true);
String version = System.getProperty("java.version");
if (version.startsWith("1.7")) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicsDevice = ge.getDefaultScreenDevice();
System.out.println("Transparent from under Java 7");
/* This won't run under Java 6, uncomment if you are using Java 7
System.out.println("isPerPixelAlphaTranslucent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT));
System.out.println("isPerPixelAlphaTransparent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT));
System.out.println("isPerPixelAlphaTranslucent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT));
*/
frame.setBackground(new Color(0, 0, 0, 0));
} else if (version.startsWith("1.6")) {
System.out.println("Transparent from under Java 6");
System.out.println("isPerPixelAlphaSupported = " + supportsPerAlphaPixel());
setOpaque(frame, false);
}
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class MyFrame extends JFrame {
public MyFrame() throws HeadlessException {
setContentPane(new MyContentPane());
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
dispose();
}
}
});
}
}
public static class MyContentPane extends JPanel {
public MyContentPane() {
setLayout(new GridBagLayout());
add(new JLabel("Hello, I'm a transparent frame under Java " + System.getProperty("java.version")));
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.BLUE);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2d.fillRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
}
}
public static boolean supportsPerAlphaPixel() {
boolean support = false;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
support = true;
} catch (Exception exp) {
}
return support;
}
public static void setOpaque(Window window, boolean opaque) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, window, opaque);
// com.sun.awt.AWTUtilities.setWindowOpaque(this, opaque);
// ((JComponent) window.getContentPane()).setOpaque(opaque);
}
} catch (Exception exp) {
}
}
public static void setOpacity(Window window, float opacity) {
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
method.invoke(null, window, opacity);
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
public static float getOpacity(Window window) {
float opacity = 1f;
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("getWindowOpacity", Window.class);
Object value = method.invoke(null, window);
if (value != null && value instanceof Float) {
opacity = ((Float) value).floatValue();
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
return opacity;
}
}
公共类透明twindow{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
MyFrame=新的MyFrame();
框架。设置未装饰(真实);
String version=System.getProperty(“java.version”);
if(版本.startsWith(“1.7”)){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice GraphicsDevice=ge.getDefaultScreenDevice();
System.out.println(“在Java7下是透明的”);
/*这不会在Java6下运行,如果您使用的是Java7,请取消注释
System.out.println(“isPerPixelAlphaTranslucent=“+graphicsDevice.IsWindowTranslucent受支持(graphicsDevice.WindowTranslucent.PERPIXEL_TRANSLUCENT));
System.out.println(“ISPERPIXELAlphatTransparent=“+graphicsDevice.IsWindowTransparency受支持(graphicsDevice.WindowTransparency.PERPIXEL_Transparency));
System.out.println(“isPerPixelAlphaTranslucent=“+graphicsDevice.isWindowTranslucent受支持(graphicsDevice.WindowTranslucent.TRANSLUCENT));
*/
帧背景(新颜色(0,0,0,0));
}else if(版本.startsWith(“1.6”)){
System.out.println(“在Java6下是透明的”);
System.out.println(“isperpixelalphassupport=“+supportsPerAlphaPixel());
set不透明(框,假);
}
框架。设置尺寸(400400);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共静态类MyFrame扩展了JFrame{
public MyFrame()抛出HeadlessException{
setContentPane(新的MyContentPane());
setDefaultCloseOperation(关闭时退出);
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
如果(如getClickCount()==2){
处置();
}
}
});
}
}
公共静态类MyContentPane扩展JPanel{
公共MyContentPane(){
setLayout(新的GridBagLayout());
添加(新的JLabel(“您好,我是Java下的透明框架”+System.getProperty(“Java.version”));
设置不透明(假);
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
g2d.setColor(Color.BLUE);
setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f));
g2d.fillRoundRect(0,0,getWidth()-1,getHeight()-1,20,20);
}
}
公共静态布尔支持speralphapixel(){
布尔支持=false;
试一试{
Class awtUtilsClass=Class.forName(“com.sun.awt.AWTUtilities”);
支持=正确;
}捕获(异常扩展){
}
回归支持;
}
公共静态void setOpaque(窗口,布尔不透明){
试一试{
Class awtUtilsClass=Class.forName(“com.sun.awt.AWTUtilities”);
如果(awtUtilsClass!=null){
方法Method=awtUtilsClass.getMethod(“setWindow不透明”,Window.class,boolean.class);
调用(null、window、不透明);
//com.sun.awt.AWTUtilities.setWindow不透明(this,不透明);
//((JComponent)window.getContentPane()).set不透明(不透明);
}
}捕获(异常扩展){
}
}
公共静态void setOpacity(窗口、浮动不透明度){
试一试{
Class awtUtilsClass=Class.forName(“com.sun.awt.AWTUtilities”);
如果(awtUtilsClass!=null){
方法Method=awtUtilsClass.getMethod(“setWindowOpacity”,Window.class,float.class);
调用(null、窗口、不透明度);
}
}捕获(异常扩展){
exp.printStackTrace();
}
}
公共静态浮点getOpacity(窗口){
浮动不透明度=1f;
试一试{
Class awtUtilsClass=Class.forName(“com.sun.awt.AWTUtilities”);
如果(awtUtilsClass!=null){
方法Method=awtUtilsClass.getMethod(“getWindowOpacity”,Window.class);
对象值=method.invoke(null,
import javax.swing.*;
import com.sun.awt.AWTUtilities;
import java.awt.Color;
class transFrame {
private JFrame f=new JFrame();
private JLabel msg=new JLabel("Hello I'm a Transparent Window");
transFrame() {
f.setBounds(400,150,500,500);
f.setLayout(null);
f.setUndecorated(true); // Undecorates the Window
f.setBackground(new Color(0,0,0,25)); // fourth index decides the opacity
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
msg.setBounds(150,250,300,25);
f.add(msg);
f.setVisible(true);
}
public static void main(String[] args) {
new transFrame();
}
}