【问题标题】:SWT - get actual screen position of an imageSWT - 获取图像的实际屏幕位置
【发布时间】: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 中获取图像的屏幕位置?

【问题讨论】:

标签: java swing swt awt


【解决方案1】:

您需要显示相对坐标。如果您有一个 Composite imageCanvas,并且您在其坐标 120, 150 处绘制图像,那么您可以通过调用获取显示器上的坐标:

imageCanvas.toDisplay(120, 150);

【讨论】:

    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    相关资源
    最近更新 更多