所以我試圖在透明窗口上繪製一個堅實的紅色橢圓。我後來想要做更復雜的多個形狀的東西,所以使用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();
}
}
我不確定我是否理解這個問題。我看到了紅色的橢圓形。我無法訪問AWTUtilities,所以也許這是不同的。 – camickr 2010-10-31 16:01:01
確實如此。該窗口被認爲是透明的,但橢圓形需要是不透明的。 – William 2010-10-31 16:04:55
+1:我也問過。並仍在尋找一個好的解決方案。 http://stackoverflow.com/questions/3372016/java-transparent-windows-with-non-transparent-components – 2010-11-07 13:25:39