【问题标题】:Java SWT GC, how to flush the doublebuffering image forcefully?Java SWT GC,如何强制刷新双缓冲图像?
【发布时间】:2011-10-26 13:11:23
【问题描述】:

有如下代码:

image = new Image(display, imageData);
offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y, imageData.width, imageData.height);
/* Draw the off-screen image to the shell. */
shellGC.drawImage(offScreenImage, 0, 0);

... 执行底部指令后:shellGC.drawImage(offScreenImage, 0, 0); 有时我会在 shellGC 组件上看到图像,有时 - 不是。只有当我“减慢”程序的执行速度时,我才会看到它,例如当我处于调试模式时。但是当它运行得很快时 - 它不会显示。我希望它强制显示、刷新或任何你的名字,这可能吗?

让我澄清一下,我想要实现的是实现一个基于帧的动画,但还不能双缓冲播放,能够停止它,只显示暂停的特定单帧等等。 .

谢谢。

【问题讨论】:

  • 这段代码是在paint事件上运行的吗?
  • 不,它没有在paintControl上运行。这实际上是错误的。它在 windows7 上运行,但在其他平台上很可能会出现问题。我从一个论坛上拿了上面的例子,这显然是错误的。有人告诉我,唯一安全的方法是从 PaintListener.paintControl(PaintEvent) 调用中绘制到小部件的 GC。这就是我现在所做的。我解决了我的问题,现在不再有不可预测的缓冲行为。我正在等待所需的时间来回答我自己的问题。
  • @PatlaDJ 你应该接受你的回答。 :)

标签: java graphics swt doublebuffered double-buffering


【解决方案1】:

事实证明,这是使用 SWT 双重缓冲的唯一安全方法:

canvas.addPaintListener(new PaintListener() {
              public void paintControl(PaintEvent event) {
                 //Obtain the next frame
                ImageData imageData = imageDataArray[iad.imageNumber];
                Image imageFrame = new Image(display, imageData);

                // Create the image to fill the canvas
                Image image = new Image(display, canvas.getBounds());

                // Set up the offscreen gc
                GC gcImage = new GC(image);

                //Draw the image offscreen
                gcImage.setBackground(event.gc.getBackground());
                gcImage.drawImage(imageFrame, 0, 0);

                // Draw the offscreen buffer to the screen
                event.gc.drawImage(image, 0, 0);

                imageFrame.dispose();
                image.dispose();
                gcImage.dispose();
              }
            });

.... 仅将这种方法用于双缓冲,可以保证跨平台的运行时行为相同,并且不可预测的缓冲区行为也消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多