10

画像を拡大するときに、ビューポートの新しい位置を計算する必要があります。

UI は次のように構築されます。

  • ImagePanel は画像を描画します
  • ImagePanelWrapper は、imagePanel をラップする JPanel です。
  • JScrollPane には 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();
}
4

2 に答える 2

38

これらの仮定が正しい場合:

  • 指定された Point は、ビューポートの左上隅を基準にしています。
  • ビューポートの寸法は、基になる 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();
}

完全を期すための計算は次のとおりです。

ここに画像の説明を入力

于 2012-12-29T19:26:45.880 に答える
2

point.xandを使用してマウス ポインタの位置を取得できるはずです。ここのドキュメントpoint.yを参照してください。ドキュメントhereによると、とはマウスの下のコンポーネント ( ) に相対的ですPointMouseMotionEventpoint.xpoint.yJScrollPane

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

于 2012-10-31T10:14:11.433 に答える