簡體   English   中英

在透明窗口上繪制非透明內容

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM