Home > Java > Java Program to write an applet with Mouse Event Handling – Q50

Java Program to write an applet with Mouse Event Handling – Q50

January 25, 2009 Leave a comment Go to comments

Q50: Java Program to write an applet which does mouse event handling by extending MouseAdapter class.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Ap_AdapterDemo extends Applet{
	public void init(){
		addMouseListener(new MyMouseAdapter(this));
		addMouseMotionListener(new MyMouseMotionAdapter(this));
	}
}

class MyMouseAdapter extends MouseAdapter{
	Ap_AdapterDemo adapterDemo;
	public MyMouseAdapter(Ap_AdapterDemo adapterDemo){
		this.adapterDemo = adapterDemo;
	}
	public void mouseClicked(MouseEvent me){
		adapterDemo.showStatus("Mouse clicked");
	}
}

class MyMouseMotionAdapter extends MouseMotionAdapter{
	Ap_AdapterDemo adapterDemo;
	public MyMouseMotionAdapter(Ap_AdapterDemo adapterDemo){
		this.adapterDemo = adapterDemo;
	}
	public void mouseDragged(MouseEvent me){
		adapterDemo.showStatus("Mouse Draged");
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: