Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SWT画布中显示快速变化的图像?(未快速调用paintControl()侦听器方法)_Java_Event Handling_Swt_Event Loop - Fatal编程技术网

Java 如何在SWT画布中显示快速变化的图像?(未快速调用paintControl()侦听器方法)

Java 如何在SWT画布中显示快速变化的图像?(未快速调用paintControl()侦听器方法),java,event-handling,swt,event-loop,Java,Event Handling,Swt,Event Loop,我在合成图上使用MouseMoveListener来更新另一个画布上的图像。基本上,如果用户将Composite1拖动到屏幕上的某个位置,则每次鼠标移动时都会调用mouseMoveEventListener,这将获得鼠标的x、y位置,使用Awt.robot.captureScreenRegion截图,并尝试通过调用其repaint()方法更新画布1的图像。其目的只是在用户拖动组合时向用户显示组合下的内容预览 问题出在这里。调用mouseMove回调时,它会依次获取屏幕截图并及时调用repaint

我在合成图上使用
MouseMoveListener
来更新另一个画布上的图像。基本上,如果用户将Composite1拖动到屏幕上的某个位置,则每次鼠标移动时都会调用mouseMoveEventListener,这将获得鼠标的x、y位置,使用
Awt.robot.captureScreenRegion
截图,并尝试通过调用其
repaint()
方法更新画布1的图像。其目的只是在用户拖动组合时向用户显示组合下的内容预览

问题出在这里。调用mouseMove回调时,它会依次获取屏幕截图并及时调用repaint方法,但不会在每次鼠标移动时调用画布的实际
paintControl()
listener方法(侦听其绘制事件)。也就是说,如果拖动合成,然后暂停几秒钟,这将调用repaint方法。但是,如果您只是将其一直拖动到屏幕左侧而不暂停,则不会在每次鼠标移动时调用repaint方法,它只会在您停止/暂停移动暂停时调用

是否有解决方案,即每次鼠标移动时是否都会强制发生绘制事件?我应该为重新绘制/预览启动一个新线程吗?预览壳和主壳都是两个独立的壳

编辑:绘制图像的方法:

canvas.addPaintListener(new PaintListener()
        {
            public void paintControl(PaintEvent e)
            {
                if (img != null)
                    e.gc.drawImage(img, 0, 0);
            }
        });
(在调用repaint()之前,图像将从BuffereImage转换为image。从system.out.println日志中,我可以看到所有这些都正确发生,只有停止移动鼠标后,才会打印
paintControl()
中的日志。)

编辑2:事件处理代码:

public void mouseMove(MouseEvent e)
{
    if (e.stateMask == 0)
        return;
    //Just some wrapper methods to do shell.setLocation(), shell.getBounds().x, shell.getBounds.y
    setLocation(getX() + e.x, getY() + e.y);
}

public void controlMoved(ControlEvent e)
{
    updatePreview();
}



public void updatePreview()
{
    //Just a wrapper around Awt.robot, uses selectionCanvas's toDisplay() to get coordinates, takes
    //screenshot, converts it from BufferedImage to Image, and returns. 
    Image img = 
            ImgUtility.getScreenShot( selectionCanvas );


    this.image = img;
    //Upto this point it works fine.

    canvas.redraw(); //this calls the preview canvas's redraw method which 
    //should call the PaintListener previously described, but doesn't.
}

找到了解决方案,结果非常简单

刚刚从以下位置更改了
控件:

public void controlMoved(ControlEvent e)
{
    updatePreview();
}
为此:

public void controlMoved(ControlEvent e)
{
    Runnable r = new Runnable()
    {
        @Override
        public void run()
        {
           updatePreview();
        }
    };
    Display.getDefault().asyncExec(r);
}

现在它工作得很好

只需在
canvas.redraw()之后调用
canvas.update()

public void update()

强制在此方法返回之前处理小部件的所有未完成绘制请求


请您添加代码块,您正在使用该代码块绘制机器人捕获的BuffereImage?很抱歉,没有一次性询问。你可以添加eventhandler代码吗?你正在使用Robot捕捉图像吗?@ShishirPandey没问题,补充道。嘿。。祝贺……)我试图在我的终点复制。只要有机会,你可以添加一些关于如何解决问题的评论,那就太好了。@ShishirPandey我已经发布了,你在下面没有看到我的答案吗?;)