Chapter 15 Event-Driven Programming and Animations Part 2

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Event Handling. In this class we will cover: Basics of event handling The AWT event hierarchy Semantic and low-level events in the AWT.
Event Handling Events and Listeners Timers and Animation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
1 Flash Actionscript Event Handling. 2 Event Handling Right now we know all about variables lets go back to our text input/output example: Suppose we.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Chapter 11 Exception Handling and Event Handling.
Event-Driven Programming
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
1 Event-Driven Programming Just like designing GUIs, you also will probably not write Java code like the program examples given in these notes. You will.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Programming with Visual C++: Concepts and Projects Chapter 2B: Reading, Processing and Displaying Data (Tutorial)
Lection №5 Modern integrated development environment.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 12 Event-Driven Programming.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
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,
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Section §15.1 Introduction  Suppose you want to develop a GUI application to calculate loan payments:  The user should be able type the pertinent.
Lecture 8:Event Driven Programming Michael Hsu CSULA.
GUI Programming using Java - Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
CompSci 230 S Programming Techniques
Java FX: Scene Builder.
Lecture 8:Event Driven Programming
Chapter 15 Event-Driven Programming and Animations
Control Circle Project Revisited
Applets.
Chapter 12 Event-Driven Programming
Java Events. Java Events Important definitions Overridden method Class vs. abstract class vs. interface Event Handling Definition Main components Windows.
Responding to Events Event Handling in Java
Event-driven programming
Lambda Expressions By Val Feldsher.
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
CS106A, Stanford University
CSE 331 Software Design and Implementation
Ellen Walker Hiram College
Functional Programming with Java
Event Driven Programming
Chapter 15 Event-Driven Programming and Animations
Defining Your Own Classes
Chapter 15 Event-Driven Programming and Animations
CSE 331 Software Design and Implementation
Chapter 12 Abstract Classes and Interfaces
1/10/2019 JavaFX Events COSC 330.
Event Driven Systems and Modeling
Chapter 16 Event-Driven Programming
Functional interface.
Events, Event Handlers, and Threads
Tonga Institute of Higher Education
Chapter 15 Event-Driven Programming and Animations Part 1
CSE 331 Software Design & Implementation
ITEC220 GUI Lecture – Part 2 References
Presentation transcript:

Chapter 15 Event-Driven Programming and Animations Part 2

Event Handling: Lambda Expressions Lambda expressions can be used to greatly simplify coding for event handling A Lambda expression can be viewed as an anonymous method with a concise syntax For example, the following code in (a) can be greatly simplified using a lambda expression in (b) in three lines

Lambda Expressions: Basic Syntax (type1 param1, type2 param2, ...) -> expression or (type1 param1, type2 param2, ...) -> { statements; } The data type for a parameter may be explicitly declared or implicitly inferred by the compiler. The parentheses can be omitted if there is only one parameter without an explicit data type. For example, (ActionEvent e) -> { // Code for handling event e } can be shortened as: e -> { // Code for handling event e }

EventHandler is a SAM Interface The compiler treats a lambda expression as an object created from an anonymous inner class. Therefore, the compiler understands that the object must be an instance of EventHandler<ActionEvent> Since the EventHandler interface defines handle() with a parameter of the ActionEvent type, the compiler knows that e is of ActionEvent type, and the statements in the lambda expression are all for the body of handle() EventHandler contains just one abstract method. Such interfaces are known as functional or Single Abstract Method (SAM) interfaces

Examples: Lambda Expressions AnonymousHandlerDemo discussed a little while back can be simplified using lambda expressions as in LambdaHandlerDemo LambdaHandlerDemo Run Another example: LoanCalculator Run

Loan Calculator Suppose you want to write a GUI program that lets the user enter a loan amount, annual interest rate, and number of years and click the Compute Payment button to obtain the monthly payment and total payment. How do you accomplish the task? You have to use event-driven programming to write the code to respond to the button-clicking event. LoanCalculator Run

The MouseEvent Class A MouseEvent is fired whenever a mouse button is pressed, released, clicked, moved, or dragged on a node or a scene The MouseEvent object captures the event, such as the number of clicks, the location, or which button was pressed MouseEventDemo Run

The KeyEvent Class A KeyEvent is fired whenever a key is pressed, released, or typed on a node or a scene The KeyEvent object describes the nature of the event (namely, that a key has been pressed, released, or typed) and the key value KeyEventDemo Run

The KeyCode Constants

Example: ControlCircle w. Mouse & Key ControlCircleWithMouseAndKey Run

Listeners for Observable Objects You can add a listener to process a value change in an observable object An instance of Observable is known as an observable object, which, for adding a listener, contains addListener(InvalidationListener listener) Once the value is changed in the property, a listener is notified. The listener class should implement the InvalidationListener interface, which uses invalidated(Observable o) to handle the property value change Every binding property is an instance of Observable ObservablePropertyDemo Run DisplayResizableClock Run

Animation Animation class and its concrete subclasses (especially PathTransition, FadeTransition and Timeline) provide the core functionality for all animations

PathTransition The PathTransition class animates the moves of a node along a path from one end to the other over a given time PathTransitionDemo Run FlagRisingAnimation Run

FadeTransition The FadeTransition class animates the change of the opacity in a node over a given time FadeTransitionDemo Run

Timeline PathTransition and FadeTransition define specialized animations The Timeline class can be used to program any animation using one or more KeyFrames Each KeyFrame is executed sequentially at a specified time interval TimelineDemo Run

Case Study: Clock Animation Use the static clock from the last chapter to create a clock animation Note: In all of our examples so far, events have been generated by the user. The events for this animation, however, are generated by system clock ClockAnimation Run

Case Study: Bouncing Ball BallPane BounceBallControl Run