如何忽略JFrame不透明度?

时间:2016-02-12 00:32:26

标签: java swing canvas jframe awt

我需要在显示动画时达到“关灯”的效果。它目前使用透明的Jframe,黑色背景,显示器大小为50%不透明度。然后,有一个画布组件应该绘制RGB A 缓冲图像。

当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 个答案:

答案 0 :(得分:1)

请勿使用setOpacity,在setBackground上使用JFrame并将其传递给基于alpha的颜色。这将允许框架变得透明而不影响其他组件。

然而,您可能会发现Canvas不喜欢基于alpha的颜色(因为它只有不透明状态)

相关问题