JAVA: Handling events with Applets

As known, GUI contains components to form user interface. These components may generate events based on user interactions such as pressing the Enter key or a mouse click and so on. When an applet is designed, these events are trapped and suitable actions to be performed in response to each of those events are provided.

To handle events, generally event handlers are available that must be suitably manipulated. The procedure to be followed when an event is generated is:

1.     Determine the type of event that took place.

2.     Determine the component which generated the event.

3.     Write appropriate code to handle the event.

The following example illustrates the use of event handling in an applet:

/*
<applet code = Mousey width = 300 height = 300>
</applet>
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Mousey extends Applet implements MouseListener
{
    int x1, y1, x2, y2;

    public void init()
    {
        setLayout(new FlowLayout());
        setBounds(100, 100, 300, 300);
        addMouseListener(this);
        this.setVisible(true);
    }

    public void mouseClicked(MouseEvent e)
    {}

    public void mousePressed(MouseEvent e)
    {
        x1 = e.getX();
        y1 = e.getY();
    }

    public void mouseMoved(MouseEvent e)
    {}

    public void mouseReleased(MouseEvent e)
    {
        x2 = e.getX();
        y2 = e.getY();
        repaint();
    }

    public void mouseEntered(MouseEvent e)
    {}

    public void mouseDragged(MouseEvent e)
    {}

    public void mouseExited(MouseEvent e)
    {}

    public void paint(Graphics g)
    {
        g.drawRect(x1, y1, x2-x1, y2-y1);
        x2 = 0;
        y2 = 0;
    }
}

hen the user clicks on the mouse a mousePressed event is generated. This event is trapped by the applet and processed. The screen coordinates of the position where the mouse click occurred is retrieved and retained in two variables x1 and y1. Similarly when the mouse is released, the corresponding value of the position are stored in x2 and y2.Using these four coordinate values, a rectangle drawn using the drawRect() method of the Graphics class. When the mouse is dreagged by keeping the mouse button pressed, a rectangle is drawn as shown in the figure below.

Keyboard events can also be handeled in the same wayas mouse events were handled. The only difference is that, theKeyListener interface has to be implemented. When a key is pressed, a KEY_PRESSED event is generated and this will inturn call the keyPressed() event handler. When the key is released it will generate keyReleased() event handler. If the character is generated by a keystroke, the KEY_TYPED event is generated and the keyTyped() event handler is called. When writing a program to handle keyboard events, input focus is requested by calling the method requestFocus()defined by the Component class. If this method is not invoked in the init() method then the program will not receive any keyboard events.

The following example demonstrates the keyboard input:

/*
<applet code = "KeyTest" width = 300 height = 300>
</applet>
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class KeyTest extends Applet implements KeyListener
{
    String msg = " ";
    // will display the output at this coordinate
    int num = 10, num1 = 10;

    public void init()
    {
        addKeyListener(this);
        // request for focus otherwise program will not recieve keyboard event
        requestFocus();
    }

    public void keyPressed(KeyEvent k)
    {
         //will display the message on the status bar
        showStatus("Key Down");
    }

    public void keyReleased(KeyEvent k)
    {
        showStatus("Key is Up");
    }

    public void keyTyped(KeyEvent k)
    {
        // will get the key typed by the user and store it in a variable
        msg += k.getKeyChar();
        repaint();
    }

    public void paint(Graphics g)
    {
        // will display the characters typed by the user at the num, num1 point
        g.drawString(msg, num, num1);
    }
}

The output of the above code is: