gpt4 book ai didi

java - 在透明窗口绘制非透明内容

转载 作者:行者123 更新时间:2023-11-30 07:36:00 27 4
gpt4 key购买 nike

所以我试图在透明窗口上绘制一个实心的红色椭圆。我稍后想用多种形状做一些更复杂的事情,所以使用 setWindowShape 不是我想要的。这是我目前使用的代码:

import java.awt.*;
import javax.swing.*;

public class JavaDock extends JFrame{

public JavaDock(){
super("This is a test");

setSize(400, 150);

setUndecorated(true);
getContentPane().setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel()
{
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.Clear);
g.setColor(Color.red);

//Draw an oval in the panel
g.fillOval(10, 10, getWidth() - 20, getHeight() - 20);
}
};

panel.setOpaque(false);
setGlassPane(panel);
getGlassPane().setVisible(true);
com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.5f);
setVisible(true);
}

protected void paintComponent(Graphics g) {

}

public static void main(String[] args){
JavaDock jd = new JavaDock();
}
}

最佳答案

您正在为您的窗口应用全局透明度,因此其中的所有内容自然至少与全局值一样透明。您可能需要逐像素半透明。替换

com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.5f);

com.sun.awt.AWTUtilities.setWindowOpaque(this, false);

这只会让您的椭圆形可见,而它将完全不透明。可以找到更多信息in this Tutorial

关于java - 在透明窗口绘制非透明内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4063802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com