Mouse as an input device can generate major 8 events. Among those 8 events below 1 of them can be handled by MouseWheelListener, 2 are handled by MouseMotionListener and rest can be handled using MouseListener.

MouseWheelListener Interface

As per the event delegation model we are going to implement this interface in our class. MouseWheelListener consists of 1 method which is as below.

  1. public void mouseWheelMoved(MouseWheelEvent e)

To add MouseWheelListener on whole applet area the syntax will be: this.addMouseMotionListener(this);
In above line 1st “this” stands for current class on which we would like to handle the Mouse related events. And 2nd “this” is the object of current class which has implemented MouseMotionListener and defined all the methods. For further reference you can refer my article on Event Handling Methodologies.

To manually invoke paint(Graphics) method: we can call repaint(). This will internally create object of java.awt.Graphics and call paint(Graphics) internally.

Program:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AppletMouseWheelEvents extends Applet implements MouseWheelListener
{
	int mouseX = 0, mouseY = 0;
	int scrollAmount = 0;
	int scrollType = 0;
	int scrollUnits = 0;
	int scrollRotation = 0;
	
	public void init() 
	{
		this.addMouseWheelListener(this);
	}
	public void mouseWheelMoved(MouseWheelEvent me) 
	{
		scrollAmount = me.getScrollAmount();
		scrollType = me.getScrollType();
		scrollUnits = me.getUnitsToScroll();
		scrollRotation = me.getWheelRotation();
		repaint();
	}
	
	public void paint(Graphics g) 
	{
		g.drawString("Scroll Amount:" + scrollAmount, 50, 50);
		g.drawString("Scroll Type:" + scrollType, 50, 70);
		g.drawString("Scroll Units:" + scrollUnits, 50, 90);
		g.drawString("Scroll Rotations:" + scrollRotation, 50, 110);
	}
}
/*
<applet code="AppletMouseWheelEvents" width="300" height="300"></applet>
*/