【问题标题】:How do you implement position-sensitive zooming inside a JScrollPane?如何在 JScrollPane 中实现位置敏感缩放?
【发布时间】:2010-09-12 00:30:33
【问题描述】:

我正在尝试在 JScrollPane 内实现位置敏感缩放。 JScrollPane 包含一个带有自定义 paint 的组件,该组件将在分配的任何空间内绘制自身 - 因此缩放就像使用 MouseWheelListener 一样简单,它可以根据需要调整内部组件的大小。

但我也想放大(或缩小)一个点,以使该点在生成的放大(或缩小)视图中尽可能居中(这就是我所说的“位置敏感”缩放),类似于缩放在谷歌地图中的工作方式。我相信这已经做过很多次了——有人知道在 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);
    }       
  }
}

【问题讨论】:

  • 添加了赏金,看看我是否能得到完整的答案(理想情况下:代码 sn-p,当添加到上面时,可以回答问题)。

标签: java swing user-interface zooming


【解决方案1】:

您的 MouseWheelListener 还必须定位光标,将其移动到 JScrollPane 的中心并调整要查看的内容的 xmin/ymin 和 xmax/ymax。

【讨论】:

  • 是的,这是真的——这也是我一开始就想做的(光标位于'p',正确调整这些边界是我不知道如何做)。
【解决方案2】:

我认为这样的 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();
}

编辑: 改变了几件事。

【讨论】:

  • 即使在将int newTop = p.y - w / 2; 更改为int newTop = p.y - h / 2; 之后,您的代码也不会将点“p”保持在结果视图的中心(例如,尝试在右下角附近进行缩放)。请先测试代码,然后再将其作为答案。
  • 更新后的代码似乎放大了左上角(XP 上的 java 6),而不是鼠标指针指向的地方。我正在使用问题中列出的相同测试程序。
【解决方案3】:

测试了一下,好像可以了……

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 放回鼠标光标下。

【讨论】:

  • 正是我想要的。谢谢!
【解决方案4】:

这是@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);
}

【讨论】:

    猜你喜欢
    • 2012-10-20
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    相关资源
    最近更新 更多