Programming Languages 2nd edition Tucker and Noonan

Similar documents
Using A Frame for Output

CS 335 Lecture 06 Java Programming GUI and Swing

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012


Programming with Java GUI components

GUI Event-Driven Programming

Swing. A Quick Tutorial on Programming Swing Applications

Assignment # 2: Design Patterns and GUIs

Extending Desktop Applications to the Web

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

JiST Graphical User Interface Event Viewer. Mark Fong

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

GUI Components: Part 2

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

Event processing in Java: what happens when you click?

Homework/Program #5 Solutions

core 2 Handling Mouse and Keyboard Events

MiniDraw Introducing a framework... and a few patterns

Mouse Event Handling (cont.)

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Graphical User Interfaces

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Advanced Network Programming Lab using Java. Angelos Stavrou

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

How to Convert an Application into an Applet.

Model-View-Controller (MVC) Design Pattern

During the process of creating ColorSwitch, you will learn how to do these tasks:

Artificial Intelligence. Class: 3 rd

Providing Information (Accessors)

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

How Scala Improved Our Java

Automated Data Collection for Usability Evaluation in Early Stages of Application Development

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Event-Driven Programming

Lab 9. Spam, Spam, Spam. Handout 11 CSCI 134: Spring, To gain experience with Strings. Objective

Java is commonly used for deploying applications across a network. Compiled Java code

Skills and Topics for TeenCoder: Java Programming

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

1 Hour, Closed Notes, Browser open to Java API docs is OK

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

Developing GUI Applications: Architectural Patterns Revisited

Schueler-Organisiertes Lernen am Beispiel von Grafischen Benutzer-Schnittstellen in Java Tag der Offenen Tür - GTS 2008

Konzepte objektorientierter Programmierung

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Essentials of the Java(TM) Programming Language, Part 1

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

Tutorial Reference Manual. Java WireFusion 4.1

The Basic Java Applet and JApplet

Animations in DrRacket

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Essentials of the Java Programming Language

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

GUI and Web Programming

Remote Method Invocation

Karsten Lentzsch JGoodies SWING WITH STYLE

How To Write A Program For The Web In Java (Java)

Medientechnik. Übung MVC

Windows PowerShell Essentials

TATJA: A Test Automation Tool for Java Applets

Java Mouse and Keyboard Methods

Demo: Controlling.NET Windows Forms from a Java Application. Version 7.3

The following four software tools are needed to learn to program in Java:

public class demo1swing extends JFrame implements ActionListener{

Creating a Java application using Perfect Developer and the Java Develo...

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

Chapter 15 Using Forms in Writer

CHAPTER 14 Understanding an App s Architecture

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

AP Computer Science Static Methods, Strings, User Input

Export. Exporting Client Addresses

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

ICOM 4015: Advanced Programming

Twin A Design Pattern for Modeling Multiple Inheritance

Informatik II. // ActionListener hinzufügen btnconvert.addactionlistener(this); super.setdefaultcloseoperation(jframe.

Java Classes. GEEN163 Introduction to Computer Programming

LAB4 Making Classes and Objects

BHARATHIAR UNIVERSITY COIMBATORE SCHOOL OF DISTANCE EDUCATION

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

11. Applets, normal window applications, packaging and sharing your work

Mapping to the Windows Presentation Framework

Writer Guide. Chapter 15 Using Forms in Writer

Lab 7 Keyboard Event Handling Mouse Event Handling

CS108, Stanford Handout #33. Sockets

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM COURSE TITLE: ADVANCE JAVA PROGRAMMING (COURSE CODE: )

UI software architectures & Modeling interaction

Syllabus for CS 134 Java Programming

How to Develop Accessible Linux Applications

Generating Automated Test Scripts for AltioLive using QF Test

The Snake Game Java Case Study

Dev Articles 05/25/07 11:07:33

16.1 DataFlavor DataFlavor Methods. Variables

Randy Hyde s Win32 Assembly Language Tutorials (Featuring HOWL) #4: Radio Buttons

The first program: Little Crab

Excel Companion. (Profit Embedded PHD) User's Guide

Callbacks. Callbacks Copyright 2007 by Ken Slonneger 1

Hypercosm. Studio.

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 16-2 1

16.1 Event-Driven Control 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. An event is created by an external action such as a user interaction through a GUI The event handler is a segment of code that is called in response to an event 16-3 Examples GUI applications: Model-View-Controller design Embedded applications: cell phones car engines airplanes 16-4 2

Imperative and Event-Driven Paradigms Contrasted Figure 16.1 16-5 Input to an event-driven program comes from autonomous event sources. Eg: human, robot sensors, engine sensors. 16-6 3

Properties 1. An event-driven program has no perceived stopping point. 2. The traditional read-eval-print loop does not explicitly appear. A typical program processes an input and exits. 16-7 Model-View-Controller (MVC) Model: the object being implemented. Ex: game, calculator. Controller: input mechanisms. Ex: buttons, menus, combo boxes. View: output. 16-8 4

Model-View-Controller (MVC) Ex: Tic-Tac-Toe Model Whose turn is it? State of the board. Has someone won? Are there no empty squares? 16-10 5

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) 16-11 Events in Java (continued) An event generator tells a listener of an event by sending a message An interface is used to make event-handling methods conform to a standard protocol A class that implements a listener must implement an interface for the listener Concepts of Programming Languages, Copyright 2006 9th ed., The by McGraw-Hill Robert W. Companies, Sebesta. Addison Inc. Wesley, 2010. 16-12 6

Events in Java (continued) One class of events is ItemEvent, which is associated with the event of clicking a checkbox, a radio button, or a list item The ItemListener interface prescribes a method, itemstatechanged, which is a handler for ItemEvent events The listener is created with additemlistener Concepts of Programming Languages, Copyright 2006 9th ed., The by McGraw-Hill Robert W. Companies, Sebesta. Addison Inc. Wesley, 2010. 16-13 Java Class AWTEvent and Its Subclasses* Figure 16.2 16-14 7

16-15 Java EventListener Class Interface and Its Subclasses* Figure 16.4 16-16 8

Widget Listener Interface JButton ActionListener actionperformed JComboBox ActionListener actionperformed JLabel JTextArea ActionListener actionperformed JTextField ActionListener actionperformed MouseListener... MouseMotionListener... 16-17 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. 16-18 9

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-19 16.3.1 A Simple GUI Interface combo : Nothing, Rectangle, Message echoarea : report events typing : enter messages 16-20 10

Initial Frame Design for a Graphical Drawing Tool Figure 16.6 16-21 private int lastx = 0; private int lasty = 0; private int clicknumber = 0; private JComboBox combo; private String[ ] choices = {"Nothing", "Rectangle", "Message"}; private JTextArea echoarea; private JTextField typing; 16-22 11

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()); 16-23 // 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); } 16-24 12

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"); }} 16-25 First Step in an Interaction: The User Selects Rectangle from the Menu Figure 16.9 16-26 13

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; 16-27 // 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)); } 16-28 14

else if (combo.getselecteditem().equals("message")) // for a message, display it g.drawstring(typing.gettext(), x, y); } // mouseclicked } 16-29 Effect of Selecting Rectangle Choice and Clicking the Mouse Twice Figure 16.12 16-30 15

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(); 16-31 16