Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.

Slides:



Advertisements
Similar presentations
Chapter 16 Graphical User Interfaces
Advertisements

Chapter 8 Improving the User Interface
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Design, Debugging & Introduction.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
CS 0004 –Lecture 1 Wednesday, Jan 5 th, 2011 Roxana Gheorghiu.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
Welcome to CIS 083 ! Events CIS 068.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Peter Andreae Python for Level 3 CS4HS see website: ecs.vuw.ac.nz/Main/PythonForSchools.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102.
Concurrent Programming and Threads Threads Blocking a User Interface.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input TextFields,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Event-driven Input COMP.
Fields, Constructors COMP 102 # T2
Introduction to Event-Driven Programming
Chapter Topics 15.1 Graphical User Interfaces
Event loops 16-Jun-18.
Lesson 1: Buttons and Events – 12/18
Quick intro to Event Driven Programming
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Event Driven Programming
Event loops.
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Event loops 8-Apr-19.
Tonga Institute of Higher Education
Event loops.
ACM programming contest
Event loops.
Event loops 19-Aug-19.
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Event-driven Input COMP 102 # T1

© Peter Andreae COMP :2 Menu GUI’s Buttons Listeners PuppetMaster program Admin: No lecture Friday. Labs this week as usual, but continuing Assig 6. Test #2: Mon12 May 5-6pm, (6-7pm for PHYS114 students)

© Peter Andreae COMP :3 Nicer User Interaction Designing good User Interfaces is an art! Text based interaction has strengths and limitations; eg + very flexible + good for expert users – slow for new/infrequent users and slow typists. – have to know the options and possiblities – hard to deal with graphical elements – error/typo prone Graphical User Interfaces have strengths and limitations: – limitations on options – only what is shown on screen – slow for expert users (unless very well designed) + good for new/infrequent users + options are explicit and obvious + excellent for graphical elements – more complicated to construct Each kind is appropriate for different tasks

© Peter Andreae COMP :4 GUI’s and Event driven input In a GUI, the interaction is controlled by the user, not by the program User initiates "events" Program responds

© Peter Andreae COMP :5 PuppetMaster How does Java respond to buttons etc? Smile Frown Right Walk Speak Distance Left

© Peter Andreae COMP :6 Event Driven Input Frown Left The JVM (Java Virtual Machine) = the "clerk" Listeners: listening to Smile button: PuppetMaster-3 listening to Frown button: PuppetMaster-3 : listening to Distance slider: PuppetMaster-3 listening to Speak textField: PuppetMaster-3 PuppetMaster-3 What happens when user clicks Frown button: JVM notices the event, looks up the listener tells PuppetMaster-3 calls buttonPerformed("frown") on PuppetMaster-3 watching PuppetMaster Constructor: - creates the object - sets up buttons and records "Listeners" - makes figure - then stops Speak Distance Right Smile Walk The JVM then watches the interface

© Peter Andreae COMP :7 Setting up event-driven input Setting up the GUI: Add buttons, sliders, and textfields to the UI Specify which object is going to "listen" to them. Specify how it will respond Setting up the listener object: declare that it is a kind of object that can respond to buttons define a “buttonPerformed” method that specifies how to respond. declare that it is a kind of object that can respond to sliders define a “sliderPerformed” method that specifies how to respond. declare that it is a kind of object that can respond to textFields define a “textFieldPerformed” method that specifies how to respond.

© Peter Andreae COMP :8 PuppetMaster: Design Structure of the PuppetMaster class: public class PuppetMaster … { // fields to store values between events/method calls private …. // Constructor public PuppetMaster(){ // set up the buttons, slider, textField // initialise fields } // methods to respond to the buttons, slider, textField public void … }

© Peter Andreae COMP :9 Smile Frown Speak Left Right Walk Distance 1100 PuppetMaster: setting up Buttons etc public class PuppetMaster … { // fields // constructor public PuppetMaster(){ UI.addButton( "Smile", this); UI.addButton( "Frown", this); UI.addButton( "Left", this); UI.addButton( "Right", this); UI.addButton( "Walk", this); UI.addTextField( "Speak", this); UI.addSlider( "Distance", 1, 100, 20, this); … } // methods to respond } Name of the slider min initial value max Object that will be listening Object that will be listening Name of button Name of textField Object that will be listening

© Peter Andreae COMP :10 Smile Frown Speak Left Right Walk Distance 1100 PuppetMaster: setting up Buttons etc public class PuppetMaster … { // fields // constructor public PuppetMaster(){ UI.addButton( "Smile", this); UI.addButton( "Frown", this); UI.addButton( "Left", this); UI.addButton( "Right", this); UI.addButton( "Walk", this); UI.addTextField( "Speak", this); UI.addSlider( "Distance", 1, 100, 20, this); … } // methods to respond } Object must be the right kind of Listener Object must be the right kind of Listener Object must be the right kind of Listener Object must be the right kind of Listener Object must be the right kind of Listener Object must be the right kind of Listener Object must be the right kind of Listener UIButtonListener UITextFieldListener UISliderListener Being just a PuppetMaster is not enough!

© Peter Andreae COMP :11 PuppetMaster: responding to buttons public class PuppetMaster implements UIButtonListener, …{ // fields // constructor public PuppetMaster(){ UI.addButton("Smile", this); UI.addButton("Frown", this); ⋮ } /** Respond to button presses */ public void buttonPerformed(String buttonName){ if (buttonName.equals("Smile")){ // what to do when the Smile button is clicked (tell the cartoon figure to smile) } else if (buttonName.equals("Frown")){ // what to do when the Frown button is clicked } else …. A promise that the class will have all the actions required of an UIButtonListener The method every UIButtonListener must have A PuppetMaster object is also a UIButtonListener

© Peter Andreae COMP :12 PuppetMaster: Fields Actions on the CartoonFigure happen in response to different events ⇒ will be in different method calls ⇒ need to store figure in a field, not a local variable. puplic class PuppetMaster implements UIButtonListener, … { private CartoonFigure figure ; // the figure being controlled : //Constructor to set up the GUI and initialise the figure public PuppetMaster(){ UI.addButton("Smile", this); // this object will respond UI.addButton("Frown", this); : this.figure = new CartoonFigure(200, 100, "blue"); } // respond to the buttons etc public void buttonPerformed(String buttonName){ :

© Peter Andreae COMP :13 PuppetMaster: putting it together public class PuppetMaster implements UIButtonListener, …{ private CartoonFigure figure ; private double walkDist = 20 ; public PuppetMaster(){ UI.addButton("Smile", this); // this object will respond UI.addButton("Frown", this); : this.figure = new CartoonFigure(200, 100, "blue"); } /** Respond to button presses */ public void buttonPerformed(String buttonName){ if (buttonName.equals("Smile")) { this.figure.smile() } else if (buttonName.equals("Frown")){ this.figure.frown() } : else if (buttonName.equals("Walk")){ this.figure.Walk( ? ) } : else …. Need to remember the distance from when the user last change the distance slider this.walkDist );} Typical design: fields to store values from one event, for use by another event Typical design: fields to store values from one event, for use by another event

© Peter Andreae COMP :14 Responding to buttons Button-49 name: “ Frown ” listener: PuppetMaster-3 worksheet: buttonPerformed: if (button.equals(…. else if (button.equals(… : button: "Frown" this: PuppetMaster-3 PuppetMaster-3 walkDist: 20 figure: CartoonFigure-11 Button-48 name: “ Smile ” listener: PuppetMaster-3 Smile Frown Speak Distance Walk Button-50 name: “ Walk ” listener: PuppetMaster-3 CartoonFigure-11 emotion: "smile" figX: 100 figY: 200direction: "right" imgBaseName: "blue"

© Peter Andreae COMP :15 GUI: Mouse input Just like buttons, except don’t have to put anything on screen Each press, release, click on the graphics pane will be an event Must tell UI which object is listening to mouse events UI.setMouseListener(this); Must declare that the class is a UIMouseListener Must define method to say how to respond the the mouse: public void mousePerformed(String action, double x, double y) { if (action.equals("pressed") ) { // what to do if mouse button is pressed } else if (action.equals("released") ) { // what to do if mouse button is released } else if (action.equals("clicked") ) { // what to do if mouse button is clicked } } Where action occurred

© Peter Andreae COMP :16 Using the mouse. Want to let user specify input with the mouse, eg: drawing lines Typical pattern: On "pressed", just remember the position On "released", do something with remembered position and new position 1 2 (100,80) (260,90)

© Peter Andreae COMP :17 Mouse Input // let user draw lines on graphics pane with the mouse. public class LineDrawer implements UIMouseListener{ private double startX, startY; public LineDrawer(){ UI.setMouseListener(this); } public void mousePerformed(String action, double x, double y) { if (action.equals("pressed") ) { this.startX = x; this.startY = y; } else if (action.equals("released") ) { UI.drawLine(this.startX, this.startY, x, y); }

© Peter Andreae COMP :18 Selecting Colors: JColorChooser public class LineDrawer implements UIMouseListener, UIButtonListener{ private double startX, startY; private Color currentColor = Color.black; public LineDrawer (){ UI.setMouseListener(this); UI.addButton("Color", this); } public void mousePerformed(String action, double x, double y) { if (action.equals("pressed") ) { this.startX = x; this.startY = y; } else if (action.equals("released") ) { UI.drawLine(this.startX, this.startY, x, y); } public void buttonPerformed(String button){ if (action.equals(“Color") ) { this.currentColor = JColorChooser.showDialog(null, "Choose Color", this.currentColor); UI.setColor(this.currentColor); }