我一直在使用java进行绘图应用程序。绘图区域是一个jcomponent,我希望能够在类似于cadd(pan,放大/缩小等)中移动。我遇到问题的部分是相对于鼠标位置进行缩放。我试过翻译原点到鼠标位置,缩放,翻译回来;这让我接近我想要的,但屏幕反弹太多而且不是很顺利。我似乎无法正确计算要转换回的新位置。到目前为止,我的缩放功能看起来像这样(任何带有d_的变量,你可以假设它是jcomponent的数据成员):
private void changeZoom(int steps) {
Point2D.Float cursorScreen = d_mouseData.getCurrentPointInScreen();
final double FACTOR = Math.sqrt(Math.sqrt(2));
for (int i = 1; i <= steps; i++)
d_fScale *= FACTOR;
for (int i = 1; i <= -steps; i++)
d_fScale /= FACTOR;
d_fScale = (float)Math.max(0.2, d_fScale);
d_newPoint.x = cursorScreen.x + (cursorScreen.x - d_newPoint.x) / d_fScale;
d_newPoint.y = cursorScreen.y + (cursorScreen.y - d_newPoint.y) / d_fScale;
d_scaleMatrix.setToTranslation(d_newPoint.x, d_newPoint.y);
d_scaleMatrix.scale(d_fScale, d_fScale);
d_scaleMatrix.translate(-cursorScreen.x, -cursorScreen.y);
repaint();
}
我在这个网站上看过几篇关于此的帖子,但它们似乎都是javascript和css,我不知道,我在试图关注时遇到麻烦。任何帮助,将不胜感激。