SWT - get actual screen position of an image

别说谁变了你拦得住时间么 提交于 2019-12-24 15:55:54

问题


I have drawn an image on a Composite using the following code

imageCanvas = new Composite(shell, SWT.BORDER);  

//adding paintListener to the canvas  
imageCanvas.addPaintListener(new PaintListener() {  
@Override  
public void paintControl(PaintEvent e)  
{  
    if (sourceImage != null)  
    {  
        // draw the image   
        e.gc.drawImage(sourceImage, 120, 150);  

        //check the bounds of the image using the getBounds function  
        Rectangle newrec = sourceImage.getBounds();  
        System.out.println("X: " +newrec.x + " Y: "+newrec.y );    // prints (0, 0)      instead of (120, 150)  
    }  
}   
});  

Now, I am trying to retrieve the image position on the screen. The image.getBounds function returns the bounds within the parent container and getLocationOnScreen() used in AWT/Swing is not available for SWT. How can I get the screen position of an image in SWT?


回答1:


You need the display-relative coordinates. If you have a Composite imageCanvas, and you're drawing an image on it at coordinates 120, 150, then you can get the coordinates on the display by calling:

imageCanvas.toDisplay(120, 150);


来源:https://stackoverflow.com/questions/9508589/swt-get-actual-screen-position-of-an-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!