![](/img/trans.png)
[英]Drawing a transparent BufferedImage over a non-transparent BufferedImage
[英]Drawing non-transparent content on transparent window
因此,我正在尝试在透明窗口上绘制纯红色的椭圆形。 后来我想对多种形状做一些更复杂的事情,所以使用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);
这将使您的椭圆形可见,并且将完全不透明。 更多信息可以在本教程中找到
Graphics2D g2d = (Graphics2D) g.create();
g2d.setComposite(AlphaComposite.Clear);
g.setColor(Color.red);
g.fillOval(10, 10, getWidth() - 20, getHeight() - 20);
代码看起来不太正确。 我会尝试:
Graphics2D g2d = (Graphics2D)g;
g2d.setComposite(AlphaComposite.Clear);
g2d.setColor(Color.red);
g2d.fillOval(10, 10, getWidth() - 20, getHeight() - 20);
或仅使用:
g.setColor(Color.red);
g.fillOval(10, 10, getWidth() - 20, getHeight() - 20);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.