max*_*dev 10 java mouse swing zoom jscrollpane
我需要在放大图像时计算视口的新位置.
UI构建如下:
放大或缩小时,ImagePanel的缩放系数会发生变化,并且会重新计算ImagePanel的首选大小.因此,即使ImagePanel停留在相同的视口点,此面板上的图像也会移动.
当用户按住CTRL并使用鼠标滚轮时,将调用以下方法.给定的点是MouseWheelListener提供的光标位置.利用这些方法中的功能,当放大或缩小时,图像已经停留在相同的左上位置.
问题是我只是想弄清楚如何相对于鼠标移动,例如Paint.NET.有任何想法吗?
/**
*
*/
public void zoomOut(Point point) {
this.imagePanel.setZoom(this.imagePanel.getZoom() * 0.9f);
Point pos = this.getViewport().getViewPosition();
int newX = (int) (pos.x * 0.9f);
int newY = (int) (pos.y * 0.9f);
this.getViewport().setViewPosition(new Point(newX, newY));
this.imagePanel.revalidate();
this.imagePanel.repaint();
}
/**
*
*/
public void zoomIn(Point point) {
this.imagePanel.setZoom(this.imagePanel.getZoom() * 1.1f);
Point pos = this.getViewport().getViewPosition();
int newX = (int) (pos.x * 1.1f);
int newY = (int) (pos.y * 1.1f);
this.getViewport().setViewPosition(new Point(newX, newY));
this.imagePanel.revalidate();
this.imagePanel.repaint();
}
Run Code Online (Sandbox Code Playgroud)
Abs*_*lom 37
如果这些假设成立:
然后可以调整视口,以便在缩放操作之前和之后光标位于图像中的同一点上,如果按以下方式移动:
/**
*
*/
public void zoomOut(Point point) {
this.imagePanel.setZoom(this.imagePanel.getZoom() * 0.9f);
Point pos = this.getViewport().getViewPosition();
int newX = (int)(point.x*(0.9f - 1f) + 0.9f*pos.x);
int newY = (int)(point.y*(0.9f - 1f) + 0.9f*pos.y);
this.getViewport().setViewPosition(new Point(newX, newY));
this.imagePanel.revalidate();
this.imagePanel.repaint();
}
/**
*
*/
public void zoomIn(Point point) {
this.imagePanel.setZoom(this.imagePanel.getZoom() * 1.1f);
Point pos = this.getViewport().getViewPosition();
int newX = (int)(point.x*(1.1f - 1f) + 1.1f*pos.x);
int newY = (int)(point.y*(1.1f - 1f) + 1.1f*pos.y);
this.getViewport().setViewPosition(new Point(newX, newY));
this.imagePanel.revalidate();
this.imagePanel.repaint();
}
Run Code Online (Sandbox Code Playgroud)
这是完整性的数学:

| 归档时间: |
|
| 查看次数: |
9345 次 |
| 最近记录: |