JFrame: addMouseWheelListener(MouseWheelListener l) : JFrame « javax.swing « Java by API






JFrame: addMouseWheelListener(MouseWheelListener l)

  

import java.awt.Graphics;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class Main extends JFrame {
  private BufferedImage bi;

  private int zoom = 0;

  private static final double ZOOM_AMOUNT = 1.1;

  public static void main(String[] args) {
      new Main("your.jpg").setVisible(true);
  }

  private void sizeToZoom() {
    double factor = Math.pow(ZOOM_AMOUNT, zoom);
    setSize((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor));
  }

  public Main(String filename) {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    try {
      bi = ImageIO.read(new File(filename));
    } catch (Exception e) {
      e.printStackTrace();
    }

    sizeToZoom();

    addMouseWheelListener(new MouseWheelListener() {
      public void mouseWheelMoved(MouseWheelEvent e) {
        int steps = e.getWheelRotation();
        zoom += steps;
        sizeToZoom();
      }
    });
  }

  public void paint(Graphics g) {
    g.drawImage(bi, 0, 0, getWidth(), getHeight(), this);
  }
}

   
    
  








Related examples in the same category

1.JFrame.DISPOSE_ON_CLOSE
2.JFrame.DO_NOTHING_ON_CLOSE
3.JFrame.EXIT_ON_CLOSE
4.JFrame: addComponentListener(ComponentListener l)
5.JFrame: addContainerListener(ContainerListener l)1
6.JFrame: addWindowListener(WindowListener lis)
7.JFrame: getAccessibleContext()
8.JFrame: getContentPane().getActionMap() [Bind key action to JFrame]
9.JFrame: getDefaultCloseOperation()
10.JFrame: getFontMetrics(Font font)
11.JFrame: getGlassPane()
12.JFrame: getLayeredPane()
13.JFrame: isFocusOwner()
14.JFrame: isUndecorated()
15.JFrame: processWindowEvent(WindowEvent e)
16.JFrame: setAlwaysOnTop(boolean alwaysOnTop)
17.JFrame: setBackground(Color c)
18.JFrame: setCursor(Cursor c)
19.JFrame: setDefaultButton(JButton defaultButton)
20.JFrame: setDefaultButton(JButton bn) (Remove 'Enter' key stroke from JTextField)
21.JFrame: setDefaultCloseOperation(int option)
22.JFrame: setDefaultLookAndFeelDecorated(boolean b)
23.JFrame: setExtendedState(int state)
24.JFrame: setFocusable(boolean focusable)
25.JFrame: setFocusableWindowState(boolean focusableWindowState)
26.JFrame: setFocusTraversalPolicy(FocusTraversalPolicy lo)
27.JFrame: setIconImage(Image image)
28.JFrame: setJMenuBar(JMenuBar bar)
29.JFrame: setLocation(int x, int y)
30.JFrame: setLocationByPlatform(boolean locationByPlatform)
31.JFrame: setLocationRelativeTo(Component c)
32.JFrame: setMaximizedBounds(Rectangle bounds)
33.JFrame: setSize(int width, int height)
34.JFrame: setTitle(String title)
35.JFrame: setUndecorated(boolean b)
36.JFrame: setVisible(boolean visible)