EventHandling

advertisement
Event Handling
Events and Listeners
Timers and Animation
Event Handling
• The event-handling model of Java is
based on the concept known as the
delegation-based event model.
• With this model, event handling is
implemented by two types of objects:
– event sources - most components can
generate events
– event listeners
Computer Graphics and User Interfaces
Event Handling
• Most components can generate events
• java.awt.Event is the superclass for all events
– ActionEvent
– ItemEvent
– MouseEvent
–…
• Each component that generates an event
needs to know what listener to send the event
to
Computer Graphics and User Interfaces
Connecting Source and Listener
event source
JButton
event listener
notify
Handler
register
A listener must be registered to a event source. Once
registered, it will get notified when the event source
generates events.
Listener Interfaces
• Each event class has a corresponding Listener
interface
– ActionListener
– ItemListener
– MouseListener
–…
• When you create a component, you need to tell
it which listener should get the events it
generates
– the component classed have
add___Listener methods for doing this
Computer Graphics and User Interfaces
ActionListener
public interface ActionListener
extends EventListener {
public void actionPerformed(
ActionEvent e);
}
• Any class that implements this interface needs
to define the actionPerformed method which
has code to execute the desired response to
the event.
Computer Graphics and User Interfaces
Implementing Listeners
• One approach is to have the container class
that holds the components implement the
listener interface.
– This is the approach used by your text
• You can also create inner classes that
implement the appropriate listener interface.
Computer Graphics and User Interfaces
Timer
• Timer is a Component that can be used to
generate events at regular intervals
– Use it for creating animated graphics
– A Timer generates ActionEvents
Computer Graphics and User Interfaces
Using a Timer
public class <AppletClass> extends JApplet
public void init() {
<PanelClass> panel = new <PanelClass>();
Timer alarm = new Timer( int millis, panel);
}
}
public class <PanelClass> extends JPanel
implements ActionListener {
public void paintComponent( Graphics g)
{
//drawing depends on some state variable(s)
}
public void actionPerformed( ActionEvent e)
{
// modify state of panel and repaint
}
}
Computer Graphics and User Interfaces
Building Menus
• JMenuBar
– JFrame and JApplet have a JMenuBar
associated with them
• JMenu
– Add JMenu objects to the JMenuBar
• JMenuItem
– Add JMenuItems or JMenus to a Jmenu
• See DrawShapes.java,
Transformations.java
Computer Graphics and User Interfaces
Handling Mouse Events
• Mouse events include such user interactions as
– moving the mouse
– dragging the mouse (moving the mouse while the
mouse button is being pressed)
– clicking the mouse buttons.
• Mouse interactions are handled by two different
Listener interfaces
– MouseListener handles events generated by the
mouse buttons
– MouseMotionListener handles mouse movement
MouseListener
• Three methods for button events
– mousePressed
– mouseReleased
– mouseClicked - both clicked and released
• Two methods detect when the mouse moves
over and away form a component
– mouseEntered
– mouseExited
• All have a MouseEvent parameter
• Generally only need one or two of these
– Others will have empty bodies
MouseMotionListener
• Handles mouse movement
– mouseMoved
– mouseDragged - mouse moves with button down
• Parameter is a MouseEvent
• Usually use mouseDragged in conjunction with
mousePressed
MouseEvent
• getPoint returns the coordinates of the
mouse when the event occurred
• There are methods for determining
which button was pressed and whether a
meta-key was also pressed
Computer Graphics and User Interfaces
Using Components
• To make your program more interactive,
add JButtons and JTextFields
• Both generate ActionEvents
– JButton when it is clicked
– JTextField when the enter key is pressed
• Generally divide the JFrame or JApplet
into areas and put the components in a
separate panel from the drawing panel
Computer Graphics and User Interfaces
JApplet as Listener
• The interactive components are created
in the JApplet
– If they are instance variables, a JApplet
Listener can identify the component
event.getSource == componentName
– They are used to modify parameters in the
JPanel
• The panel class needs set methods for the
parameters that need to change
• These parameters need to be instance variables
• Example: transformType in TransformPanel
Computer Graphics and User Interfaces
JPanel as Listener
• If the panel is the listener, it doesn't have
direct access to the Component
– could pass references to the panel but not
usually done this way
• How to identify the component
– If there is only one component of a particular
type, check the type with instanceOf
– ActionEvent has a getActionCommand which
can be used to identify a component like a
JMenuItem or a JButton
Computer Graphics and User Interfaces
Download