2012-10-31 7 views
9

イメージをズームインするときに、ビューポートの新しい位置を計算する必要があります。次のようにJScrollPane - マウスの位置を基準にしたズームズーム

は、UIが構築される。

  • ImagePanelは
  • ImagePanelWrapperがimagePanel
  • JScrollPaneの周りにJPanelの包装である

内またはズームImagePanelWrapperを含む画像を描画します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(); 
} 

答えて

34

これらの前提条件が満たされている場合:

  • 供給ポイントは、ビューポートの左上隅からの相対です。
  • ビューポートのサイズは、基本となるImagePanelよりも小さくなっています。以下のように移動した場合、カーソルは、ズーム操作の前と後の画像における同じ点の上になるよう

そしてビューポートを調整することができる。

/** 
* 
*/ 
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(); 
} 

ここで完全性について数学です'のため:

enter image description here

+1

+1恐ろしい、答え。私はしばらく見てきました。 – max

+0

あなたは大きな感謝をあなたに負う価値があります:) – EyeSpy

+0

私は同様の質問があります。私はAffineTransformを使って同じことをしようとしていますが、この質問から数学を適用する際に問題があります。あなたが時間を見つけたら、私を助けてくれますか? http://stackoverflow.com/questions/37509908/zoom-in-at-mouse-cursor – bigblind

2

あなたはpoint.xpoint.yを使用して、マウスポインタの位置を取得することができるはず - Pointドキュメントhereを参照してください。 MouseMotionEvent文書hereに従うと、point.xおよびpoint.yは、マウスの下の構成要素(JScrollPane)に関連しています。

これらの値を計算に組み込むことができます。これはあなたが探していたものですか?

関連する問題