2016-02-12 41 views
0

我需要在顯示動畫時達到「關燈」的效果。目前已經完成透明Jframe,黑色背景,顯示器的大小不透明度爲50%。那麼,這個畫布組件應該繪製RGB緩衝圖像的RGB A如何忽略JFrame不透明度?

當JFrame不透明度也影響Canvas時彈出問題,使其半透明。這就是我想要避免的。

//** Window class extends Canvas 

public Window(){ 

    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 
    int hostMonitorWidth = gd.getDisplayMode().getWidth(); 
    int hostMonitorHeight = gd.getDisplayMode().getHeight(); 

    Dimension dimension = new Dimension(hostMonitorWidth, hostMonitorHeight); 
    super.setPreferredSize(dimension); 

    window = new JFrame(); 
    window.setUndecorated(true); 
    window.setOpacity(0.55f); 
    window.setLayout(new GridLayout()); 
    window.setSize(hostMonitorWidth, hostMonitorHeight); 
    window.add(this); 

    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    window.setLocationRelativeTo(null); 
    window.setVisible(true); 
    window.requestFocus(); 
    window.setFocusableWindowState(true); 


    super.createBufferStrategy(3); 
} 

public void draw(){ 
    BufferStrategy buffer = super.getBufferStrategy(); 
    java.awt.Graphics g = buffer.getDrawGraphics(); 

    g.setColor(Color.BLACK); 
    g.fillRect(0,0, super.getWidth(), super.getHeight()); 
    g.drawImage(batch.getImage(), 0, 0, super.getWidth(), super.getHeight(), null); 

    g.dispose(); 
    buffer.show(); 
} 

我已經嘗試了以下代碼與Jpanel,Layered Panes,Jlabel以及哪些不同的組合。似乎總是保持不透明/拋出無法解釋的例外/不能以任何理由工作。

我在做正確的方法嗎?我在這裏錯過了什麼?

回答

1

請勿在JFrame上使用setOpacity,使用setBackground並將其傳遞給基於alpha的顏色。這將允許框架變得透明而不影響其他組件。

您可能,但是,發現Canvas不喜歡阿爾法爲主色調(因爲它只有一個不透明狀態)

+0

我已經試過了,好像在研究背景顏色接管畫布。你能否證明它應該如何正確完成? –

+0

而且,正如我所說'Canvas'可能不喜歡你在做什麼,因爲它只有一個不透明的狀態(它不支持透明度),如果你想繪製一個半透明的組件,你將需要使用一個Swing而不是基於組件 – MadProgrammer

+0

你最好的建議是什麼? –