【发布时间】:2016-05-23 00:32:20
【问题描述】:
我需要在显示动画的同时实现“关灯”的效果。目前使用透明 Jframe、黑色背景和 50% 的不透明度(显示器大小)完成。然后,有这个画布组件应该绘制 RGBA 缓冲图像。
当 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、分层窗格、Jlabel 等的组合。它似乎总是保持不透明/抛出无法解释的异常/出于任何原因无法正常工作。
我的做法是否正确?我在这里错过了什么?
【问题讨论】:
标签: java swing canvas jframe awt