Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 16 Event-Driven Programming Of all men’s miseries.

Similar presentations


Presentation on theme: "Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 16 Event-Driven Programming Of all men’s miseries."— Presentation transcript:

1 Copyright © 2006 The McGraw-Hill Companies, Inc. 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)

2 Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 16.1 Event-Driven Control 16.2 Event Handling 16.3 Three Examples 16.4 Other Applications

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

4 Copyright © 2006 The McGraw-Hill Companies, Inc. Examples GUI applications: Model-View-Controller design Embedded applications: cell phones car engines airplanes

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

6 Copyright © 2006 The McGraw-Hill Companies, Inc. Imperative and Event-Driven Paradigms Contrasted Figure 16.1

7 Copyright © 2006 The McGraw-Hill Companies, Inc. Input to an event-driven program comes from autonomous event sources. Eg: human, robot sensors, engine sensors.

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

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

10 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?

11 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)

12 Copyright © 2006 The McGraw-Hill Companies, Inc. Java Class AWTEvent and Its Subclasses* Figure 16.2

13 Copyright © 2006 The McGraw-Hill Companies, Inc.

14 Java EventListener Class Interface and Its Subclasses* Figure 16.4

15 Copyright © 2006 The McGraw-Hill Companies, Inc. WidgetListenerInterface JButtonActionListeneractionPerformed JComboBoxActionListeneractionPerformed JLabel JTextAreaActionListeneractionPerformed JTextFieldActionListeneractionPerformed MouseListener... MouseMotionListener...

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

17 Copyright © 2006 The McGraw-Hill Companies, Inc. import javax.swing.JFrame; public class GUIApp { public static void main (String[ ] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); MyApp app = new MyApp( ); // JPanel frame.getContentPane().add(app); frame.show( ); }

18 Copyright © 2006 The McGraw-Hill Companies, Inc. 16.3.1 A Simple GUI Iinterface combo : Nothing, Rectangle, Message echoArea : report events typing : enter messages

19 Copyright © 2006 The McGraw-Hill Companies, Inc. Initial Frame Design for a Graphical Drawing Tool Figure 16.6

20 Copyright © 2006 The McGraw-Hill Companies, Inc. private int lastX = 0; // first click's x-y coordinates private int lastY = 0; private int clickNumber = 0; private JComboBox combo; private String[ ] choices = {"Nothing", "Rectangle", "Message"}; private JTextArea echoArea; private JTextField typing;

21 Copyright © 2006 The McGraw-Hill Companies, Inc. public Skeleton( ) { // Set the background color and mouse listener setBackground(Color.white); addMouseListener(new MouseHandler()); // Add a button to the Panel. JButton clearButton = new JButton("Clear"); clearButton.setForeground(Color.black); clearButton.setBackground(Color.lightGray); add(clearButton); clearButton.addActionListener( new ClearButtonHandler());

22 Copyright © 2006 The McGraw-Hill Companies, Inc. // Create a menu of user combos and add it combo = new JComboBox(choices); add(combo); combo.addActionListener( new ComboHandler()); // Add a TextField and a TextArea typing = new JTextField(20); add(typing); typing.addActionListener(new TextHandler()); echoArea = new JTextArea(2, 40); echoArea.setEditable(false); add(echoArea); }

23 Copyright © 2006 The McGraw-Hill Companies, Inc. private class ComboHandler implements ActionListener { public void actionPerformed (ActionEvent e) { String c = (String) (combo.getSelectedItem()); echoArea.setText("Combo selected: " + c); clickNumber = 0; if (c.equals("Rectangle")) echoArea.append("\nClick to set upper " + " left corner of the rectangle"); else if (c.equals("Message")) echoArea.append( "\nEnter a message in the text area"); }}

24 Copyright © 2006 The McGraw-Hill Companies, Inc. First Step in an Interaction: The User Selects Rectangle from the Menu Figure 16.9

25 Copyright © 2006 The McGraw-Hill Companies, Inc. private class MouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); echoArea.setText("Mouse Clicked at " + e.getX() + ", " + e.getY() + "\n"); Graphics g = getGraphics(); if (combo.getSelectedItem(). equals("Rectangle")) { clickNumber = clickNumber + 1;

26 Copyright © 2006 The McGraw-Hill Companies, Inc. // is it the first click? if (clickNumber % 2 == 1) { echoArea.append("Click to set lower right" + " corner of the rectangle"); lastX = x; lastY = y; } // or the second? else g.drawRect(lastX, lastY, Math.abs(x-lastX), Math.abs(y-lastY)); }

27 Copyright © 2006 The McGraw-Hill Companies, Inc. else if (combo.getSelectedItem().equals("Message")) // for a message, display it g.drawString(typing.getText(), x, y); } // mouseClicked }

28 Copyright © 2006 The McGraw-Hill Companies, Inc. Effect of Selecting Rectangle Choice and Clicking the Mouse Twice Figure 16.12

29 Copyright © 2006 The McGraw-Hill Companies, Inc. public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); Skeleton panel = new Skeleton( ); frame.getContentPane().add(panel); frame.setSize(500, 500); frame.show(); }


Download ppt "Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 16 Event-Driven Programming Of all men’s miseries."

Similar presentations


Ads by Google