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

4 Copyright © 2006 The McGraw-Hill Companies, Inc. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SkelApplet extends JApplet { // Global "state" of the interaction private int lastX = 0; private int lastY = 0;...

5 Copyright © 2006 The McGraw-Hill Companies, Inc. public void init( ) { JPanel panel = new JPanel( );... // Create a button and add it to the Panel. JButton clearButton = new JButton("Clear"); clearButton.setForeground(Color.black); clearButton.setBackground(Color.lightGray); panel.add(clearButton); clearButton.addActionListener( new ClearButtonHandler());... getContentPane( ).add(panel); }

6 Copyright © 2006 The McGraw-Hill Companies, Inc. Tic-Tac-Toe State of the board Whose turn: X or O Whether game is over

7 Copyright © 2006 The McGraw-Hill Companies, Inc. private int player = -1; // Current player: EX, or OH private int[ ] board = new int[9];

8 Copyright © 2006 The McGraw-Hill Companies, Inc. public abstract class CellIcon implements Icon { private Color color = Color.black; private int size; public CellIcon(int size) { this.size = size; } public int size( ) { return size; } public Color getColor( ) { return color; } public void setColor(Color c) { color = c; } public int getIconWidth( ) { return size; } public int getIconHeight( ) { return size; } }

9 Copyright © 2006 The McGraw-Hill Companies, Inc. private int[ ][ ] lines = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //across {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //down {0, 4, 8}, {2, 4, 6} //diagonal }; public boolean won( ) { for (int i = 0; i < lines.length; i++) if (board[lines[i][0]] == player && board[lines[i][1]] == player && board[lines[i][2]] == player) return true; return false; } // won

10 Copyright © 2006 The McGraw-Hill Companies, Inc. public class CellXIcon extends CellIcon { public CellXIcon(int size) { super(size); } public void paintIcon(Component c, Graphics g, int x, int y) { int size = size( ); g.setColor(Color.white); g.fillRect(x, y, size, size); g.setColor(getColor( )); g.drawLine(x, y, x+size, y+size); g.drawLine(x+size, y, x, y+size); g.setColor(Color.black); }

11 Copyright © 2006 The McGraw-Hill Companies, Inc. private class ActionHandler implements ActionListener { private int button; public ActionHandler(int b) {button = b;} public void actionPerformed(ActionEvent e) { Icon icon = game.move(button); square[button].setIcon(icon); square[button].setEnabled(false); if (game.won( )) { setButtons(false); echoArea.setText(game.player( ) + " wins!"); } else echoArea.setText("Button clicked: " + button); }}

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

13 Elements of a Typical ATM Machine Transaction User Interface Figure 16.25

14 Copyright © 2006 The McGraw-Hill Companies, Inc. Overall Design of a Home Security System Figure 16.26


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