Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.

Similar presentations


Presentation on theme: "COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters."— Presentation transcript:

1 COMP 321 Week 2

2 Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters

3 Event-Driven Programming Used in GUI applications, where user is in control Isolated pieces of code that respond to user actions Non-sequential execution Initial setup; then: event handling Harder than forcing the user to supply input in a fixed order

4 Event Handling – Key Concepts Event Event listener - provided by application programmer (you) Event source - UI component that generates a particular event

5 Event Handling During initialization, Listener objects register themselves with the components they want to listen to, using the addXXXListener methods of those components When the event occurs, an event object is created with information about the event The component issuing the event calls the appropriate event handler method on each object that has been registered as a listener

6 Some Commonly-used Event Classes MouseEvent - occurs when the mouse is moved, or a mouse button is pressed or released KeyEvent - occurs when a key is pressed or released WindowEvent - occurs when a window is activated, deactivated, minimized, maximized, or closed ActionEvent - occurs when a button is pressed, a menu item is selected, or enter is pressed while entering text in a JTextField

7 ActionListener // Defined in JDK public interface ActionListener { void actionPerformed(ActionEvent event); void actionPerformed(ActionEvent event);} // Defined in your code public class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) public void actionPerformed(ActionEvent event) { System.out.println(“I was clicked”); System.out.println(“I was clicked”); }}

8 Attaching an ActionListener JFrame frame = new JFrame(); frame.setSize(150, 150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Click me!"); frame.add(button); ActionListener listener = new ClickListener(); button.addActionListener(listener); frame.setVisible(true);

9 Learning Activity 2-1 Problem description: write a program that creates and displays a frame with buttons allowing the value in the window to be manipulated Purpose: to demonstrate the use of buttons, as well as creating and attaching an action listener

10 Timer Class Defined in javax.swing package Generates a sequence of action events, spaced apart at even time intervals When you create a Timer, you specify (1) the frequency of the events and (2) an object of a class that implements the ActionListener interface: final int DELAY = 100; // 100 ms Timer t = new Timer(DELAY, new ActionListener() { public void actionPerformed(ActionEvent event) { // Do something } });

11 Learning Activity 2-2 Problem description: write a program that uses timer events to display a counter that increments ten times per second Purpose: to demonstrate the use of the Timer class

12 Event Adapters Adapter pattern: convert the interface of a class into another interface clients expect Enable developers to only implement methods they are interested in (convenience)

13 MouseAdapter // Defined in java.awt.event public interface MouseListener { public void mouseClicked(MouseEvent e); public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); public void mouseExited(MouseEvent e);} public class MouseAdapter implements MouseListener { public void mouseClicked(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseExited(MouseEvent e) {}}

14 Learning Activity 2-3 Problem description: write a program that uses MouseMotionListener to allow a circle to be dragged around the window. Purpose: to demonstrate the processing of Mouse Events

15 Supplemental Activity Problem description: write a program that creates and displays an animated 20x20 bouncing ball using timer events Purpose: putting all the concepts together

16 Deliverables - Progress Check Due this week –1-3 Obtain Proctor –1-4 Graphics Design Due next week –1-2 Basic Application Interfaces


Download ppt "COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters."

Similar presentations


Ads by Google