Java Applet Mouse Listener Example

Hello friends here I have attached Simple Java Applet Mouse Listener Example. Which will helps you to understand the Mouse Listener and its events on Applet window. also the below program tested and same out put is included in the post.

Java Applet Mouse Listener Example

MyAppletWindow.java

package com.javatraineronline.example;

import java.applet.Applet;

public class MyAppletWindow extends Applet {

	public void init() {
		
		// Setting the Size of the Window
		super.setSize(650, 400);
		
		// Adding the MouseListener Implemented Class
		addMouseListener(new MyMouseListener(this));
	}
	
}

MyMouseListener.java

package com.javatraineronline.example;

import java.applet.Applet;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyMouseListener implements MouseListener {

	private Applet appletWindow;
	
	public MyMouseListener(Applet appletWindow) {
		
		this.appletWindow = appletWindow;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {

		appletWindow.setBackground(Color.GREEN);
	}

	@Override
	public void mousePressed(MouseEvent e) {

		appletWindow.setBackground(Color.YELLOW);
	}

	@Override
	public void mouseReleased(MouseEvent e) {

		appletWindow.setBackground(Color.BLUE);
	}

	@Override
	public void mouseEntered(MouseEvent e) {

		appletWindow.setBackground(Color.RED);
	}

	@Override
	public void mouseExited(MouseEvent e) {

		appletWindow.setBackground(Color.CYAN);
	}

}

Note: If you are running this example using command prompt. make sure you are using the following command once the source files has be complied. appletviewer MyAppletWindow.java

Output

Java Applet Mouse Listener Example