如何在JScrollPane中实现位置敏感缩放?
时间:2020-03-06 14:32:30 来源:igfitidea点击:
我正在尝试在JScrollPane内部实现位置敏感缩放。 " JScrollPane"包含一个带有自定义"画图"的组件,该组件可以在分配的任何空间内绘制自己,因此缩放就像使用" MouseWheelListener"一样,可以根据需要调整内部组件的大小。
但是我也想放大(或者缩小)一个点,以在生成的放大(或者-缩小)视图中将该点保持在尽可能中心的位置(这就是我所说的"位置敏感"缩放),类似在Google地图中如何缩放。我敢肯定,在没有人知道Java Swing下执行"正确"方法之前,已经做过很多次了。玩" Graphic2D"的转换而不是使用" JScrollPanes"会更好吗?
示例代码如下:
package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; public class FPanel extends javax.swing.JPanel { private Dimension preferredSize = new Dimension(400, 400); private Rectangle2D[] rects = new Rectangle2D[50]; public static void main(String[] args) { JFrame jf = new JFrame("test"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(400, 400); jf.add(new JScrollPane(new FPanel())); jf.setVisible(true); } public FPanel() { // generate rectangles with pseudo-random coords for (int i=0; i<rects.length; i++) { rects[i] = new Rectangle2D.Double( Math.random()*.8, Math.random()*.8, Math.random()*.2, Math.random()*.2); } // mouse listener to detect scrollwheel events addMouseWheelListener(new MouseWheelListener() { public void mouseWheelMoved(MouseWheelEvent e) { updatePreferredSize(e.getWheelRotation(), e.getPoint()); } }); } private void updatePreferredSize(int n, Point p) { double d = (double) n * 1.08; d = (n > 0) ? 1 / d : -d; int w = (int) (getWidth() * d); int h = (int) (getHeight() * d); preferredSize.setSize(w, h); getParent().doLayout(); // Question: how do I keep 'p' centered in the resulting view? } public Dimension getPreferredSize() { return preferredSize; } private Rectangle2D r = new Rectangle2D.Float(); public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); int w = getWidth(); int h = getHeight(); for (Rectangle2D rect : rects) { r.setRect(rect.getX() * w, rect.getY() * h, rect.getWidth() * w, rect.getHeight() * h); ((Graphics2D)g).draw(r); } } }
解决方案
MouseWheelListener也必须找到光标,将其移动到JScrollPane的中心,并调整要查看的内容的xmin / ymin和xmax / ymax。
我认为像这样的smt应该可以工作...
private void updatePreferredSize(int n, Point p) { double d = (double) n * 1.08; d = (n > 0) ? 1 / d : -d; int w = (int) (getWidth() * d); int h = (int) (getHeight() * d); preferredSize.setSize(w, h); // Question: how do I keep 'p' centered in the resulting view? int parentWdt = this.getParent( ).getWidth( ) ; int parentHgt = this.getParent( ).getHeight( ) ; int newLeft = p.getLocation( ).x - ( p.x - ( parentWdt / 2 ) ) ; int newTop = p.getLocation( ).y - ( p.y - ( parentHgt / 2 ) ) ; this.setLocation( newLeft, newTop ) ; getParent().doLayout(); }
编辑:
改变了几件事。
经过测试,似乎可以正常工作...
private void updatePreferredSize(int n, Point p) { double d = (double) n * 1.08; d = (n > 0) ? 1 / d : -d; int w = (int) (getWidth() * d); int h = (int) (getHeight() * d); preferredSize.setSize(w, h); int offX = (int)(p.x * d) - p.x; int offY = (int)(p.y * d) - p.y; setLocation(getLocation().x-offX,getLocation().y-offY); getParent().doLayout(); }
更新
这是一种解释:点" p"是鼠标相对于" FPanel"的位置。由于我们要缩放面板的尺寸,因此" p"的位置(相对于面板的尺寸)将以相同的比例缩放。通过从缩放位置中减去当前位置,我们可以获得调整面板大小时点"移动"了多少。然后,只需在相反方向上将滚动窗格中的面板位置移动相同的数量,即可将" p"放回鼠标光标下方。
这是@Kevin K解决方案的一个小重构:
private void updatePreferredSize(int wheelRotation, Point stablePoint) { double scaleFactor = findScaleFactor(wheelRotation); scaleBy(scaleFactor); Point offset = findOffset(stablePoint, scaleFactor); offsetBy(offset); getParent().doLayout(); } private double findScaleFactor(int wheelRotation) { double d = wheelRotation * 1.08; return (d > 0) ? 1 / d : -d; } private void scaleBy(double scaleFactor) { int w = (int) (getWidth() * scaleFactor); int h = (int) (getHeight() * scaleFactor); preferredSize.setSize(w, h); } private Point findOffset(Point stablePoint, double scaleFactor) { int x = (int) (stablePoint.x * scaleFactor) - stablePoint.x; int y = (int) (stablePoint.y * scaleFactor) - stablePoint.y; return new Point(x, y); } private void offsetBy(Point offset) { Point location = getLocation(); setLocation(location.x - offset.x, location.y - offset.y); }