Applets - Part 2 -
Responding to Applet Events

bullet

You need to put controls/components on the display, and then have code - event handlers - that will "catch" events such as mouse clicks or key presses

bullet

The display is handled by a layout manager.

bullet

Overall, the following code will create a label, will have the layout manager put it onto the display in the top/"North" section, and the mouseMove event will cause the X and Y coordinates of the mouse cursor to be displayed in the label. 

bullet

First, note that the paint event will cause the string "Hello" to be displayed at location 50,50 from the upper left corner of the applet's display area.  If you get ambitious, change the commented lines in paint so that you can see the ovals, rectangles, and lines.

import java.applet.*;
import java.awt.*;
       
public class ScrApplet2 extends Applet
{
   
    public void paint(Graphics g)
    {
        // TODO: Add your own implementation. from J++: super.paint(g);
        // WHAT WE ADDED --
        g.drawString("Hello", 50, 50); //text @50,50
        //g.drawLine(0,0,200,200); //line from 0,0 to 200,200
        //g.fillRect(20,20,80,80); //filled Rectfrom 20,20 width & height=80
        //g.drawRect(101,20,80,80); //unfilled Rect from 101,20 & H,W=80
        //g.drawOval(20,101,80,80); //oval from 20,101 & H,W = 80
        //g.fillOval(101,101,80,80); //filled oval from 101,101 & H,W=80
    }

   

bullet

When a mouse Up event is detected, the display area background will be made red --

    public boolean mouseUp(Event e, int x, int y)
    {
        // TODO: Add your own implementation.        

        // OLD GENERATED CODE: return super.mouseUp(e,x,y);
        // OUR NEW CODE -
        setBackground(Color.red);
        repaint();
        return true;
    }

bullet

When a mouse Down event occurs, the background will be made green --
   
   public boolean mouseDown(Event e, int x, int y)
    {
        // TODO: Add your own implementation.

        // OLD GENERATED CODE: return super.mouseUp(e,x,y);
        // OUR NEW CODE --
        setBackground(Color.green);
        repaint();
        return true;
    }

bullet

Add the label called mousePositionLbl and the mouseMove event handler --

    // We're adding a label to the applet, ...
    private Label mousePositionLbl = new Label();
   
    // ... and a mouseMove event handler that will
    // track the cursor as it moves inside the applet


    public boolean mouseMove(Event e, int x, int y)
    {
        mousePositionLbl.setText("Cursor is at "+x+", "+y);
        repaint();
        return true;
    }

bullet

And modify init (called when the applet is initialized) to select a layout manager and to use it via the add to place the label on the display -

    public void init()
    {
        // TODO: Add your own implementation. FROM J++:   super.init();
        // our code -- to add a BorderLayout manager,
        // and then to add a label to the layout
        //
        setLayout(new BorderLayout());
        add("North", mousePositionLbl);
    }
}  /* end of applet  */

 

wpe6.jpg (17506 bytes)

Created & Maintained by DrB - Comments welcome.
Last edited on Friday, July 30, 2004