从Paint Listener中的AWT Canvas中删除闪烁

时间:2014-04-15 03:28:16

标签: java canvas swt flicker

我有一个线程可以通过调用它的重绘方法在Canvas控件中激活一个gif图像。但是观察到的图像闪烁。我尝试使用SWT.NO_REDRAW_RESIZE样式位,但它没有帮助。

以下是Thread的运行代码

while (!UIManager.stop){
                long currentTime = System.currentTimeMillis();
                int delayTime = UIManager.loader.data[UIManager.imageNumber].delayTime;
                while(currentTime + delayTime * 10 > System.currentTimeMillis()){
                  // Wait till the delay time has passed

                }

                UIManager.imageNumber = UIManager.imageNumber == UIManager.loader.data.length-1 ? 0 : UIManager.imageNumber+1;



                display.asyncExec(new Runnable() {                                      
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub

                        if(!UIManager.stop){
                        UIManager.image = new Image(display, UIManager.loader.data[UIManager.imageNumber] );

                        if(!UIManager.gifCanvas.isDisposed())
                            UIManager.gifCanvas.redraw();

                        }
                    }
                });

和Canvas绘图侦听器如下

gifCanvas = new Canvas(gifComposite,SWT.NO_REDRAW_RESIZE);
        gifCanvas.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
        gifCanvas.setLayoutData(introGifData);
        gifCanvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {                
                e.gc.drawImage(image,0,0);
                }
        });     

我不知道我哪里错了。我们将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

Canvas支持SWT.DOUBLE_BUFFERED,尝试将其添加为Canvas创建中的样式:

gifCanvas = new Canvas (gifComposite, SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED);