1. Create a class that extends Applet and implements an interface.
class<name> extends Applets implements <interface>
Class Skey extends Applet implements MouseListener
There are different Interfaces for different events i.e.for keyboard->KeyListner, for Mouse->MouseListner/MouseMotionListner etc.
2. Initialize applet by init method.
init() { }
3. Add or Register the listener to source for receiving notification. Each type of event has its own listener.
- add TypeListener(TypeListener);
- addMouseListener(this);
4. Implement the method to receive and process these notifications.
- public void method(eventclass obj){}
- public void mouseEntered(MouseEvent m){}
- public void mousePressed(MouseEvent m){}
- public void mouseClicked(MouseEvent m){}
- public void mouseExited(MouseEvent m){}
- public void mouseReleased(MouseEvent m){}
Components of Event Handling are the following:
- Event Source: ex.Button, CheckBox
- EventListener Interface: MouseListener
- Event classes: Mouse Event
- Methods of EventListener Interface: mouseEntered(),mousePressed(),mouseReleased(),mouseExited(),mouseClicked()
Ex.Program:
- package proj2;
- import java.awt.*;
- import java.applet.*;
- import java.awt.event.*;
- public class One extends Applet implements MouseListener {
- String message = " ";
- int mouseX = 0;
- int mouseY = 0;
- public void init() {
- addMouseListener(this);
- }
- public void mouseEntered(MouseEvent m) {
- mouseX = m.getX();
- mouseY = m.getY();
- repaint();
- }
- public void mouseExited(MouseEvent m) {
- mouseX = m.getX();
- mouseY = m.getY();
- repaint();
- }
- public void mouseClicked(MouseEvent m) {
- message = "Mouse Clicked at :";
- mouseX = m.getX();
- mouseY = m.getY();
- repaint();
- }
- public void mousePressed(MouseEvent m) {
- message = "Mouse button is pressed at :";
- mouseX = m.getX();
- mouseY = m.getY();
- repaint();
- }
- public void mouseReleased(MouseEvent m) {
- message = " mouse released";
- mouseX = m.getX();
- mouseY = m.getY();
- repaint();
- }
- public void paint(Graphics g) {
- g.drawString(message, mouseX, mouseY);
- }
- }