读书人

请教在SWT中一个图被缩放后怎么根据

发布时间: 2012-01-26 19:40:46 作者: rapoo

请问在SWT中一个图被缩放后,如何根据鼠标事件的坐标得到原图的坐标?
我有一个40*40的图[原图],用GC画在一个可以随意缩放的Canvas里面,当Canvas缩放时,重画这个图[修改图],
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
int nWidth = canvas.getClientArea().width;
int nHeight = canvas.getClientArea().height;
e.gc.drawImage(image, 0, 0, imageData.width, imageData.height, 0, 0, nWidth,nHeight);
}
}
当鼠标点到这个[修改图]中的一个点时,可以得到坐标,但是我想得到[原图]的坐标,有什么好的办法吗??

我之前的做法是使用BigDecimal对象,[修改图]宽度/[原图]宽度,得到一个宽度的对比度,然后再根据鼠标当前坐标得到原图坐标,可是当图很小的时候,误差很大

请个一点提示,不胜感激

[解决办法]
没太明白你的意思。
用相对坐标 不行吗?
[解决办法]
巧了,我正好有这么一个算法 ^ ^

不过只是针对成倍数缩放的,希望对你有参考价值

Java code
    private int getRealValue( int scaledValue, int zoomRate )    {        if( zoomRate == 0 )            return 0;        if( zoomRate == 1 )        {            return scaledValue;        }        /*         * For example:          * if the scaledValue is 8, zoomRate is 8, then the real value is ( 8 - 1 )/8 + 1 = 1          * if the scaledValue is 9, zoomRate is 8, then the real value is ( 9 - 1 )/8 + 1 = 2         * if the scaledValue is 4, zoomRate is -4, then the real value is -( 4 * (-4) = 16         */        int realValue = ( zoomRate > 0 ) ? ( ( scaledValue - 1 ) / zoomRate + 1 ) : ( -( scaledValue * zoomRate ) );        return realValue;    } 

读书人网 >J2SE开发

热点推荐