Presentation is loading. Please wait.

Presentation is loading. Please wait.

Event Handling Events and Listeners Timers and Animation.

Similar presentations


Presentation on theme: "Event Handling Events and Listeners Timers and Animation."— Presentation transcript:

1 Event Handling Events and Listeners Timers and Animation

2 Computer Graphics and User Interfaces 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

3 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

4 Connecting Source and Listener JButton Handler event source event listener notify register A listener must be registered to a event source. Once registered, it will get notified when the event source generates events.

5 Computer Graphics and User Interfaces 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

6 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.

7 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.

8 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

9 Computer Graphics and User Interfaces Using a Timer public class extends JApplet public void init() { panel = new (); Timer alarm = new Timer( int millis, panel); } public class 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 }

10 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

11 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

12 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

13 MouseMotionListener Handles mouse movement –mouseMoved –mouseDragged - mouse moves with button down Parameter is a MouseEvent Usually use mouseDragged in conjunction with mousePressed

14 Computer Graphics and User Interfaces 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

15 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

16 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

17 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


Download ppt "Event Handling Events and Listeners Timers and Animation."

Similar presentations


Ads by Google