【发布时间】:2016-07-22 04:08:45
【问题描述】:
我已经使用以下代码在 Composite 上绘制了图像
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 函数返回父容器内的边界,而 AWT/Swing 中使用的 getLocationOnScreen() 不适用于 SWT。如何在 SWT 中获取图像的屏幕位置?
【问题讨论】:
-
检查此stackoverflow.com/questions/339821/… 和ibm.com/developerworks/java/tutorials/j-swing2swt/section4.html。如果您正在共同关联 swing 和 swt API,那么 IBM 链接很好