问题描述
我已经用下面的code画在复合图像
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)
}
}
});
现在,我试图找回屏幕上的图像位置。所述image.getBounds函数返回父容器和getLocationOnScreen()在AWT使用内的边界/ Swing是不适用于SWT。我怎样才能获得图像的屏幕位置在SWT?
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?
推荐答案
您需要显示相对坐标。如果你有一个复合 imageCanvas
,而你在坐标上绘制图像的 120,150 ,然后就可以得到上的坐标通过调用显示:
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);
这篇关于SWT - 获取图像的实际屏幕位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!