Zym*_*mox 5 java swing transparency visibility repaint
在一个简短的方法中,我使用setVisible(false)隐藏JFrame.然后我截取屏幕截图并使用setVisible(true)恢复JFrame.
在再次显示之后,窗口应该显示与之前不同的图片(假设拍摄截图的一部分).
问题是在调用setVisible(true)之后,在调用paintComponent并绘制更新状态之前,窗口会使用旧内容闪烁一瞬间.
我可能会以一种丑陋的方式解决这个问题,但我想知道是否有更好的解决方案.
在此先感谢您的帮助
编辑:在准备一个例子的时候,我注意到,当我不在程序中使用透明度时几乎看不到效果.应该提到这一点.这是我想出的:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.sun.awt.AWTUtilities;
public class Test {
static boolean flag = false;
static Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame();
frame.setUndecorated(true);
AWTUtilities.setWindowOpaque(frame, false); //draw on a transparent window
frame.setSize(scrSize.width, scrSize.height);
frame.setContentPane(new JPanel() {
protected void paintComponent(Graphics g)
{
if (Test.flag) {
g.setColor(Color.RED);
g.drawRect(50, 50, scrSize.width - 100, scrSize.height - 100);
}
else {
g.setColor(Color.GREEN);
g.fillOval(50, 50, scrSize.width - 100, scrSize.height - 100);
}
}
});
frame.setVisible(true); //green oval shown
Thread.sleep(1000);
frame.setVisible(false);
flag = true; // next draw produces red rectangle
Thread.sleep(1000);
frame.setVisible(true); // before the next draw,
// you can see a flash of the green oval
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7058 次 |
| 最近记录: |