Java Graphics2D - 缩放鼠标位置
Java Graphics2D - Zoom on mouse location
我有一个 jpanel 可以用来画画。我希望能够使用鼠标滚轮进行放大和缩小,但我想缩放到鼠标的位置,以便鼠标下方的点保持不变。我找到了一些 ,但它们对我不起作用。
通过实施所描述的 here,我已经非常接近做我想做的事了。这是我的代码:
public class MyPanel extends JPanel {
...
private double zoom = 1;
private double zoom_old = 1;
private int zoomPointX;
private int zoomPointY;
...
class CustomMouseWheelListener implements MouseWheelListener {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
zoomPointX = e.getX();
zoomPointY = e.getY();
if (e.getPreciseWheelRotation() < 0) {
zoom -= 0.1;
} else {
zoom += 0.1;
}
if (zoom < 0.01) {
zoom = 0.01;
}
repaint();
}
}
...
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
if (zoom != zoom_old) {
double scalechange = zoom - zoom_old;
zoom_old = zoom;
double offsetX = -(zoomPointX * scalechange);
double offsetY = -(zoomPointY * scalechange) ;
AffineTransform at = new AffineTransform();
at.scale(zoom, zoom);
at.translate(offsetX, offsetY);
g2D.setTransform(at);
}
a_different_class_where_i_do_some_drawing.draw(g2D);
}
}
这几乎可以满足我的要求。如果我尝试缩放,我注意到鼠标的位置被考虑在内,例如,如果我将鼠标放在面板的左侧,它会粗略地放大左侧。但是,它并没有完全缩放到鼠标上,所以鼠标下方的点仍然会发生变化。
谁能帮我解决这个问题?
编辑:
这是上面发布的代码所发生情况的图片:我开始时将鼠标放在蓝色方块上,然后只需使用鼠标滚轮滚动即可。如您所见,鼠标位置如果被更改:
我通过实施所描述的内容解决了问题here
这是更新后的代码:
public class MyPanel extends JPanel {
...
private double zoom = 1;
private int zoomPointX;
private int zoomPointY;
...
class CustomMouseWheelListener implements MouseWheelListener {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
zoomPointX = e.getX();
zoomPointY = e.getY();
if (e.getPreciseWheelRotation() < 0) {
zoom -= 0.1;
} else {
zoom += 0.1;
}
if (zoom < 0.01) {
zoom = 0.01;
}
repaint();
}
}
...
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
AffineTransform at = g2D.getTransform();
at.translate(zoomPointX, zoomPointY);
at.scale(zoom, zoom);
at.translate(-zoomPointX, -zoomPointY);
g2D.setTransform(at);
a_different_class_where_i_do_some_drawing.draw(g2D);
}
}
我有一个 jpanel 可以用来画画。我希望能够使用鼠标滚轮进行放大和缩小,但我想缩放到鼠标的位置,以便鼠标下方的点保持不变。我找到了一些
通过实施所描述的 here,我已经非常接近做我想做的事了。这是我的代码:
public class MyPanel extends JPanel {
...
private double zoom = 1;
private double zoom_old = 1;
private int zoomPointX;
private int zoomPointY;
...
class CustomMouseWheelListener implements MouseWheelListener {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
zoomPointX = e.getX();
zoomPointY = e.getY();
if (e.getPreciseWheelRotation() < 0) {
zoom -= 0.1;
} else {
zoom += 0.1;
}
if (zoom < 0.01) {
zoom = 0.01;
}
repaint();
}
}
...
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
if (zoom != zoom_old) {
double scalechange = zoom - zoom_old;
zoom_old = zoom;
double offsetX = -(zoomPointX * scalechange);
double offsetY = -(zoomPointY * scalechange) ;
AffineTransform at = new AffineTransform();
at.scale(zoom, zoom);
at.translate(offsetX, offsetY);
g2D.setTransform(at);
}
a_different_class_where_i_do_some_drawing.draw(g2D);
}
}
这几乎可以满足我的要求。如果我尝试缩放,我注意到鼠标的位置被考虑在内,例如,如果我将鼠标放在面板的左侧,它会粗略地放大左侧。但是,它并没有完全缩放到鼠标上,所以鼠标下方的点仍然会发生变化。
谁能帮我解决这个问题?
编辑:
这是上面发布的代码所发生情况的图片:我开始时将鼠标放在蓝色方块上,然后只需使用鼠标滚轮滚动即可。如您所见,鼠标位置如果被更改:
我通过实施所描述的内容解决了问题here
这是更新后的代码:
public class MyPanel extends JPanel {
...
private double zoom = 1;
private int zoomPointX;
private int zoomPointY;
...
class CustomMouseWheelListener implements MouseWheelListener {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
zoomPointX = e.getX();
zoomPointY = e.getY();
if (e.getPreciseWheelRotation() < 0) {
zoom -= 0.1;
} else {
zoom += 0.1;
}
if (zoom < 0.01) {
zoom = 0.01;
}
repaint();
}
}
...
protected void paintComponent(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
super.paintComponent(g2D);
AffineTransform at = g2D.getTransform();
at.translate(zoomPointX, zoomPointY);
at.scale(zoom, zoom);
at.translate(-zoomPointX, -zoomPointY);
g2D.setTransform(at);
a_different_class_where_i_do_some_drawing.draw(g2D);
}
}