Programming Languages 2nd edition Tucker and Noonan"

Similar documents
COMPSCI 230. Software Design and Construction. Swing

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

2IS45 Programming

CS 251 Intermediate Programming GUIs: Event Listeners

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

User interfaces and Swing

Lecture 5: Java Graphics

Lecture 3: Java Graphics & Events

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Agenda. Container and Component

H212 Introduction to Software Systems Honors

Graphical User Interfaces. Comp 152

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

PIC 20A GUI with swing

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

GUI Software Architecture

CSE 331 Software Design & Implementation

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

MIT AITI Swing Event Model Lecture 17

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Lecture 19 GUI Events

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Programming Language Concepts: Lecture 8

MVC: Model View Controller

Swing from A to Z Some Simple Components. Preface

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Lecture 9. Lecture

Programming graphics

AP CS Unit 12: Drawing and Mouse Events

Class 16: The Swing Event Model

Graphical User Interface (GUI)

Graphical User Interfaces (GUIs)

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Systems Programming Graphical User Interfaces

Advanced Java Unit 6: Review of Graphics and Events

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Part I: Learn Common Graphics Components

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

AP CS Unit 11: Graphics and Events

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Graphical User Interfaces 2

Chapter 1 GUI Applications

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Programming Languages and Techniques (CIS120)

Java for Interfaces and Networks (DT3010, HT10)

Unit 7: Event driven programming

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Graphic User Interfaces. - GUI concepts - Swing - AWT

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Design patterns for graphical user interface applications

Jonathan Aldrich Charlie Garrod

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

Graphical User Interfaces 2

Java GUI Design: the Basics

Programming Languages and Techniques (CIS120)

UI Software Organization

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Graphical User Interfaces 2

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Programming Languages and Techniques (CIS120)

We are on the GUI fast track path

Window Interfaces Using Swing Objects

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

Computer Science 210: Data Structures. Intro to Java Graphics

COMP 102: Test 2 Model Solutions

Homework 6 part 2: Turtle Etch-A-Sketch (40pts)

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

SINGLE EVENT HANDLING

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

CMP 326 Midterm Fall 2015

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

Using Several Components

Programming Languages and Techniques (CIS120)

Graphical User Interface (GUI)

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Packages: Putting Classes Together

SD Module-1 Advanced JAVA

Java. GUI building with the AWT

SD Module-1 Advanced JAVA. Assignment No. 4

Dr. Hikmat A. M. AbdelJaber

1005ICT Object Oriented Programming Lecture Notes

Transcription:

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

Contents" " 16.1 Event-Driven Control" 16.2 Event Handling" 16.3 Three Examples" 16.4 Other Applications"

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

Examples" GUI applications: Model-View-Controller design Embedded applications: cell phones car engines airplanes

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.

Imperative and Event-Driven Paradigms Contrasted Figure 16.1

" Input to an event-driven program comes from autonomous event sources. Eg: human, robot sensors, engine sensors.

Properties" 1. An event-driven program need have no perceived stopping point. 2. The traditional read-eval-print loop, as in Lisp and Haskell, does not explicitly appear. 3. An application processes an input and exits.

Model-View-Controller (MVC)" Model: the object being implemented. Ex: game, calculator. Controller: input mechanisms. Ex: buttons, menus, combo boxes. View: output.

Ex: Tic-Tac-Toe Model" Whose turn is it? State of the board. Has someone won? Are there no empty squares?

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

Java Class AWTEvent and Its Subclasses* Figure 16.2

Java EventListener Class Interface and Its Subclasses* Figure 16.4

Widget Listener Interface JButton ActionListener actionperformed JComboBox ActionListener actionperformed JLabel JTextArea ActionListener actionperformed JTextField ActionListener actionperformed MouseListener... MouseMotionListener...

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.

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( );" }" }"

16.3.1 A Simple GUI Iinterface" combo : Nothing, Rectangle, Message echoarea : report events typing : enter messages

Initial Frame Design for a Graphical Drawing Tool Figure 16.6

private int lastx = 0; coordinates private int lasty = 0; private int clicknumber = 0; private JComboBox combo; // first click's x-y private String[ ] choices = {"Nothing", "Rectangle", "Message"}; private JTextArea echoarea; private JTextField typing;

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());"

// 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);" }"

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"); " }}"

First Step in an Interaction: The User Selects Rectangle from the Menu Figure 16.9

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;"

// 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));" } "

else if (combo.getselecteditem().equals("message")) " // for a message, display it" g.drawstring(typing.gettext(), x, y); " } // mouseclicked" }"

Effect of Selecting Rectangle Choice and Clicking the Mouse Twice Figure 16.12

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();" }"