• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:MouseWheelで画像のズームイン・アウト表示
#navi(../)
#tags(MouseWheelListener, ImageIcon, JComponent)
RIGHT:Posted by &author(aterai); at 2006-10-16
* MouseWheelで画像のズームイン・アウト表示 [#g39f1afa]
---
category: swing
folder: Zoom
title: MouseWheelで画像のズームイン・アウト表示
tags: [MouseWheelListener, ImageIcon, JComponent]
author: aterai
pubdate: 2006-10-16T13:11:04+09:00
description: マウスホイールなどで画像をズームイン、ズームアウトします。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTW1yBNHvI/AAAAAAAAAp4/ba6eQ-Ul2sg/s800/Zoom.png
---
* 概要 [#summary]
マウスホイールなどで画像をズームイン、ズームアウトします。

#download
#ref(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTW1yBNHvI/AAAAAAAAAp4/ba6eQ-Ul2sg/s800/Zoom.png)
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTW1yBNHvI/AAAAAAAAAp4/ba6eQ-Ul2sg/s800/Zoom.png)

** サンプルコード [#n33b6ada]
* サンプルコード [#sourcecode]
#code(link){{
class ZoomImage extends JComponent implements MouseWheelListener {
  private final ImageIcon icon;
class ZoomImage extends JPanel {
  private transient MouseWheelListener handler;
  private final transient Image image;
  private final int iw;
  private final int ih;
  private double scale = 1.0d;
  public ZoomImage(final ImageIcon icon) {
  private double scale = 1d;
  protected ZoomImage(Image image) {
    super();
    this.icon = icon;
    iw = icon.getIconWidth();
    ih = icon.getIconHeight();
    addMouseWheelListener(this);
    this.image = image;
    iw = image.getWidth(this);
    ih = image.getHeight(this);
  }
  @Override public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

  @Override public void updateUI() {
    removeMouseWheelListener(handler);
    super.updateUI();
    handler = e -> changeScale(e.getWheelRotation());
    addMouseWheelListener(handler);
  }

  @Override protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g.create();
    g2.scale(scale, scale);
    g2.drawImage(icon.getImage(), 0, 0, iw, ih, this);
    g2.drawImage(image, 0, 0, iw, ih, this);
    g2.dispose();
  }
  @Override public void mouseWheelMoved(MouseWheelEvent e) {
    changeScale(e.getWheelRotation());
  }

  public void initScale() {
    scale = 1.0d;
    scale = 1d;
    repaint();
  }
  @Override public void changeScale(int iv) {
    scale = Math.max(0.05d, Math.min(5.0d, scale-iv*0.05d));

  public void changeScale(int iv) {
    scale = Math.max(.05, Math.min(5d, scale - iv * .05));
    repaint();
  }
}
}}

** 解説 [#h16d4c28]
上記のサンプルではホイールの上回転で拡大、下回転で縮小しています。
* 解説 [#explanation]
- 画像の拡大・縮小表示は`JPanel#paintComponent(...)`をオーバーライドして`Graphics2D#scale(...)`で設定
- 拡大・縮小率はマウスホイールの上方向回転を拡大、下方向回転を縮小として適用

//**参考リンク
** コメント [#g84ab533]
- `ih = icon.getIconWidth();`の部分は`getIconHeight();`ではありませんでしょうか? -- [[syo]] &new{2007-05-18 (金) 13:11:04};
-- ご指摘ありがとうございます。スクリーンショットもよく見たら中の画像が正方形になってますね(^^;。修正しておきます。 -- [[aterai]] &new{2007-05-18 (金) 13:25:06};
* 参考リンク [#reference]
- [[JPanelに表示した画像のズームとスクロール>Swing/ZoomingAndPanning]]

* コメント [#comment]
#comment
- `ih = icon.getIconWidth();`の部分は`getIconHeight();`ではありませんでしょうか? -- &user(syo); &new{2007-05-18 (金) 13:11:04};
-- ご指摘ありがとうございます。スクリーンショットもよく見たら中の画像が正方形になってますね(^^;。修正しました。 -- &user(aterai); &new{2007-05-18 (金) 13:25:06};

#comment