Object-Oriented Programming with Java Lecture Notes

8 April 2008 • Event Handling


Outline

Sequential Programming

Sequential Breakdown

Sequential Rehabilitation

So What?

$ echo 100 400 100 100 400 400 100 400 | 
  java DCShow 15

viola!

  • This is a sequential program.

    a sequential program

Improving the Viewer

Interactive Programs

Events

The Event Class

Mouse Events

mouse-event class hierarchy

  • The MouseEvent class abstracts occurrences associated with a mouse (pointing-picking device).

  • Mouse events inherits around forty fields from ancestors:
    21 fields from AWT Event.
    5 fields from Component Event.
    15 fields from Input Event.

  • And the getX() and getY() methods.

Event Handlers

The Mouse Listener

Point Picking

Completing The Listener

Attaching the Listener

Attaching the Listener

Mousing Around

In Action

The Current State

Handling Scope

Sharing State

In the panel:

private int 
  xCoord[], yCoord[], n

DragonPanel()
  setMouseListener(
    new MouseHandler(this))

addPoint(int x, int y)
  xCoord[n] = x
  xCoord[n++] = y
  repaint()

In the handler:

private JPanel panel

MouseHandler(JPanel p)
  panel = p

mouseClicked(MouseEvent e)
  panel.addPoint(
    e.getX(), e.getY())

Sharing Analysis

Inner Classes

  • The panel defines the mouse handler as an inner class.

  • The inner class uses outer-class scope to access panel features.

In the panel:

private int 
  xCoord[], yCoord[], n

DragonPanel()
  setMouseListener(
    new MouseHandler(this))

private class MouseHandler
extends MouseListener

  mouseClicked(MouseEvent e)
    xCoord[n] = x
    xCoord[n++] = y
    repaint()

Inner-Class Analysis

Mushed Classes

Mushed-Class Analysis

Changing Order

Keyboard Events

keyboard-event class hierarchy

  • The KeyEvent class abstracts the key presses associated with the keyboard.

  • Key events are primitive, only vaguely related to the text characters they apparently produce.

    • Key codes are involved, which aren't Unicode.

The Keyboard Listener

Key Handling

New Curve Orders


This page last modified on 10 April 2008.

Creative
    Commons License