Programming Languages 2nd edition Tucker and Noonan Chapter 16 Event-Driven Programming Of all men’s miseries the bitterest is this, to know so much and to have control over nothing. Herodotus (484-432 BC) Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 16.1 16.2 16.3 16.4 Event-Driven Control Event Handling Three Examples Other Applications Copyright © 2006 The McGraw-Hill Companies, Inc. A conventional model of computation has the program prescribe the exact order of input. Programs terminate once the input is exhausted. Event-driven programs do not control the sequence in which input events occur. Copyright © 2006 The McGraw-Hill Companies, Inc. Examples GUI applications: Model-View-Controller design Embedded applications: cell phones car engines airplanes Copyright © 2006 The McGraw-Hill Companies, Inc. 16.1 Event-Driven Control Computation as interaction [Stein] Computation is a community of persistent entities coupled together by their ongoing interactive behavior ... Beginning and end, when present, are special cases that can often be ignored. Copyright © 2006 The McGraw-Hill Companies, Inc. Imperative and Event-Driven Paradigms Contrasted Figure 16.1 Copyright © 2006 The McGraw-Hill Companies, Inc. Input to an event-driven program comes from autonomous event sources. Eg: human, robot sensors, engine sensors. Copyright © 2006 The McGraw-Hill Companies, Inc. Properties 1. An event-driven program has no perceived stopping point. 2. The traditional read-eval-print loop does not explicitly appear. 3. An application processes an input and exits. Copyright © 2006 The McGraw-Hill Companies, Inc. Model-View-Controller (MVC) • Model: the object being implemented. Ex: game, calculator. • Controller: input mechanisms. Ex: buttons, menus, combo boxes. • View: output. Copyright © 2006 The McGraw-Hill Companies, Inc. Ex: Tic-Tac-Toe Model • Whose turn is it? • State of the board. • Has someone won? • Are there no empty squares? Copyright © 2006 The McGraw-Hill Companies, Inc. Events in Java • Subclasses of AWTEvent • Event sources in Swing are subclasses of JComponent • Program must listen for events • Ex: for a JButton b – b.addActionListener(listener) Copyright © 2006 The McGraw-Hill Companies, Inc. Java Class AWTEvent and Its Subclasses* Figure 16.2 Copyright © 2006 The McGraw-Hill Companies, Inc. Copyright © 2006 The McGraw-Hill Companies, Inc. Java EventListener Class Interface and Its Subclasses* Figure 16.4 Copyright © 2006 The McGraw-Hill Companies, Inc. Widget Listener Interface JButton ActionListener actionPerformed JComboBox ActionListener actionPerformed JLabel JTextArea ActionListener actionPerformed JTextField ActionListener actionPerformed MouseListener ... MouseMotionListener ... Copyright © 2006 The McGraw-Hill Companies, Inc. Java GUI Application A GUI application is a program that runs in its own window and communicates with users using buttons, menus, mouse clicks, etc. A GUI application often has a paint method, which is invoked whenever the application needs to repaint itself. Copyright © 2006 The McGraw-Hill Companies, Inc. 16.3.2 Designing a Java Applet • Can convert previous application • An applet runs inside a web browser • Differences – Extend JApplet – Lacks a main method – Method init replaces constructor Copyright © 2006 The McGraw-Hill Companies, Inc. Tic-Tac-Toe • State of the board • Whose turn: X or O • Whether game is over Copyright © 2006 The McGraw-Hill Companies, Inc. private int player = -1; // Current player: EX, or OH private int[ ] board = new int[9]; Copyright © 2006 The McGraw-Hill Companies, Inc. Copyright © 2006 The McGraw-Hill Companies, Inc. Elements of a Typical ATM Machine Transaction User Interface Figure 16.25 Copyright © 2006 The McGraw-Hill Companies, Inc. Overall Design of a Home Security System Figure 16.26 Copyright © 2006 The McGraw-Hill Companies, Inc.
© Copyright 2026 Paperzz