Lecture Summary 5
Events and Listeners
- A event is an object that represents some occurance
which we may want to handle.
- Often generated by user actions: keyboard, mouse,
etc.
- Can also be generated by other programs (GUI components such as butons or sliders).
- There are different types of events represented by
different classes: MouseEvent,
ActionEvent, etc.
- A listener is an object that waits for and responses
to events.
- To respond to an event, we must do two things:
- create a listener object for the event of interest
- add that listener to the graphical component that may generate the event
- For example, to respond to a mouse event,
- we must create a MouseListener object
- to do that, we define a class that implements MouseListene interface
- we add the object to the graphical component that may generate the mouse event
Eg., an applet can generate mouse events, so we can call its addMouseListener method that takes a MouseListener as parameter, to add a MouseListener to the applet.
- the listener object has methods that are automatically called when an event occurs
- each method receives a MouseEvent object as parameter that represents the event.
- we can then invoke a service of MouseEvent object.
Eg., invoke getPoint in mousePressed method by using event.getPoint() to get the point where the mouse event occured.
To create a listener object we define a class that implement a listener interface.
Components that can generate certain events have methods that can be called to add a listener.
There are different types of listeners represented
by different listener interfaces: MouseListener ,
ActionListener,etc.
A listener class should implement one of the listener interfaces.
Interface MouseListener
- void mousePressed(MouseEvent event)
- void mouseReleased(MouseEvent event)
- void mouseClicked(MouseEvent event)
- void mouseEntered(MouseEvent event)
- void mouseExited(MouseEvent event)
Class MouseEvent
- Point getPoint()
- int getX()
- int getY()
- int getClickCount()
Example: Dots.java,
DotsMouseListener.java
[Dots.html]
Notes: (1) We can remove init() method and include all its statements in paint method. This wpuld ork just fine.
(2) Try to modify the program (in a version similar to RubberLines.java) to have only one class that extends Applet and implements MouseListener.
Example:
DotsAppletModified.java,
DotsAppletModified.html
Interface MouseMotionListener
- void mouseMoved(MouseEvent event)
- void mouseDragged(MouseEvent event)
Example: RubberLines.java
[RubberLines.html]
Interface KeyListener
- void keyPressed(KeyEvent event)
- void keyReleased(KeyEvent event)
- void keyTyped(KeyEvent event)
Class KeyEvent
- int getKeyCode()
- constants for all keys
Example: Direction.java
[Direction.html]
Animation
- An animation is a constantly changing series of
pictures or images that create the illusion of movement
- We can create animations in Java by changing a picture
slightly over time
- The speed of a Java animation is usually controlled by
a Timer object
- Timer
class
- Timer(int delay, ActionListener
listener)
- void addActionListener(ActionListener
listener)
- boolean isRunning()
- void start()
- void stop()
- Example: Rebound.java
[Rebound.html]
- This applet requires Java 2. Some browsers does not
support Java 2. You can run this applet using appletviewer.