问题
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