在透明窗口上绘制不透明内容

发布于 2024-09-30 04:11:33 字数 1199 浏览 5 评论 0原文

所以我试图在透明窗口上绘制一个实心红色椭圆形。我后来想要用多个形状做一些更复杂的事情,所以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗳卜坏 2024-10-07 04:11:33

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

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

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

这只会留下您的椭圆形可见,并且它将完全不透明。更多信息可以在本教程中找到

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

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

with

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

This leaves just your oval visible and it will be completely opaque. More infos can be found in this Tutorial

囚你心 2024-10-07 04:11:33
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);   
Graphics2D g2d = (Graphics2D) g.create(); 
g2d.setComposite(AlphaComposite.Clear); 
g.setColor(Color.red);   
g.fillOval(10, 10, getWidth() - 20, getHeight() - 20);   

Code doesn't look quite right. I would try:

Graphics2D g2d = (Graphics2D)g; 
g2d.setComposite(AlphaComposite.Clear); 
g2d.setColor(Color.red);   
g2d.fillOval(10, 10, getWidth() - 20, getHeight() - 20);   

or just use:

g.setColor(Color.red);   
g.fillOval(10, 10, getWidth() - 20, getHeight() - 20);   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文