在透明窗口上绘制不透明内容
所以我试图在透明窗口上绘制一个实心红色椭圆形。我后来想要用多个形状做一些更复杂的事情,所以使用 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();
}
}
So I'm trying to draw a solid red oval on a transparent window. I later want to do something more complex with multiple shapes, so using setWindowShape isn't what I'm looking for. This is the code I'm using so far:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在对窗口应用全局透明度,因此自然地,其中的所有内容都将至少与全局值一样透明。您可能需要每像素半透明度。替换
为
这只会留下您的椭圆形可见,并且它将完全不透明。更多信息可以在本教程中找到
You are applying a global transparency to you window, so naturally everything in it will be at least as transparent as the global value. You probably want per-pixel translucency. Replace
with
This leaves just your oval visible and it will be completely opaque. More infos can be found in this Tutorial
代码看起来不太正确。我会尝试:
或者只是使用:
Code doesn't look quite right. I would try:
or just use: