JScrollPane 内で位置に応じたズームを実装するにはどうすればよいでしょうか?
-
02-07-2019 - |
質問
内部に位置依存のズームを実装しようとしています JScrollPane
. 。の JScrollPane
カスタマイズされたコンポーネントが含まれています paint
これは、割り当てられたスペース内に自身を描画します。そのため、ズームは、 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);
}
}
}
解決
これをテストしたところ、うまくいくようです...
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);
}
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();
}
編集:いくつかの点を変更しました。