2013-09-23 49 views
2

我使用複合材料的MouseMoveListener更新關於另一畫布圖像(paintControl()監聽方法不被迅速地調用)。基本上,如果用戶拖動Composite1到他的屏幕上的某個地方,將調用mouseMoveEventListener每次鼠標移動,其將獲得X,鼠標的y位置,使用Awt.robot.captureScreenRegion採取截圖,並嘗試通過更新Canvas1的圖像調用其方法repaint()。目的只是爲了向用戶展示組合下的內容,因爲他正在拖動它。如何在SWT畫布中顯示快速變化的圖像?

這是問題所在。當調用mouseMove回調函數時,它會輪流獲取屏幕截圖並及時調用repaint方法,但每當鼠標移動時,畫布的實際paintControl()偵聽器方法(偵聽其Paint事件)都不會被調用。也就是說,如果您拖動複合材料,然後暫停幾秒鐘,這將調用重繪方法。但是,如果只是將其拖動到屏幕左側而不暫停,則每次移動鼠標時都不會調用重繪方法,只有在停止/暫停移動暫停時纔會調用重繪方法。

有沒有一種解決方案,即可以Paint事件被迫發生每次移動鼠標?我應該爲重新繪製/預覽啓動一個新線程嗎?預覽外殼和主外殼都是兩個獨立的外殼。

編輯:方法,其繪製圖像:

canvas.addPaintListener(new PaintListener() 
     { 
      public void paintControl(PaintEvent e) 
      { 
       if (img != null) 
        e.gc.drawImage(img, 0, 0); 
      } 
     }); 

(該圖像從BufferedImage的轉換爲圖像重繪之前()被調用從原木的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. 
} 
+0

請你添加代碼塊,它使用的是繪製由機器人拍攝的BufferedImage? –

+0

@ShishirPandey添加 – user2790209

+0

對不起,一口氣問。您可以添加事件處理程序代碼以及您用於使用Robot捕獲圖像的代碼嗎? –

回答

0

找到了解決辦法,這竟然是真的很簡單!

只是改變從這個controlMoved

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

這樣:

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

,現在,它完美的作品!

1

canvas.redraw()後只要打電話canvas.update()

public void update()

強制所有的部件是突出的油漆要求該方法返回前被處理。