Chapter 15 EventDriven Programming and Animations Dr Clincy

  • Slides: 16
Download presentation
Chapter 15 Event-Driven Programming and Animations Dr. Clincy - Lecture 1

Chapter 15 Event-Driven Programming and Animations Dr. Clincy - Lecture 1

Recall: Basic Structure of Java. FX We have already examined the creating the GUI

Recall: Basic Structure of Java. FX We have already examined the creating the GUI frontend and placing (or mapping) the nodes or buttons on the pane or scene Stage object is automatically created by JVM - window. Scene object is the container for the content – frame in the window. Node object (or button) is a visual component used to invoke some type of action Lets now explore making the nodes or buttons active – Event Driven Programming Panes can be used to help layout the nodes within a scene. Dr. Clincy - Lecture 2

Handling GUI Events Source object (e. g. , button, polygon, image, etc. ) Event

Handling GUI Events Source object (e. g. , button, polygon, image, etc. ) Event object (e. g. , mouse click, mouse pointer over object, type characters into a textfield, etc ) Listener object contains a method for processing the event Not all objects can be handlers for some action event. To be a handler of an event, there are two requirements: • The object must be an instance of the interface called Event. Handler. NOTE: the Event. Handler • • interface defines the common behavior for all handlers The handler, which is the object generated from the Event. Handler interface, MUST be “registered” with the event source object using the method source. set. On. Action(handler) The full format for interface Event. Handler is: Event. Handler<T extends Event> where T is a generic type that extends the subtype Event. The Event. Handler interface contains the handle(Action. Event) method for processing action events. NOTE: Your handler class must override the handle(Action. Event) method to respond to the 3 event. Dr. Clincy - Lecture

Example: Event-Driven Programming Given the two buttons in the frame, when the OK button

Example: Event-Driven Programming Given the two buttons in the frame, when the OK button is clicked, the message, “OK button clicked” should be displayed to the console. When the Cancel button is clicked, the message “Cancel button clicked” should be displayed to the console. Dr. Clincy - Lecture 4

Example: Event-Driven Programming First, let’s create the handler classes for the OK and Cancel

Example: Event-Driven Programming First, let’s create the handler classes for the OK and Cancel buttons Creating the OK handler class by implementing the Event. Handler interface Overriding the Event. Handler’s handle method Handling the event is simply printing the statement “OK button clicked” to the console Creating the Cancel handler class by implementing the Event. Handler interface Overriding the Event. Handler’s handle method Handling the event is simply printing the statement “Cancel button clicked” to the console Now that we have created the handler classes for each button, let’s now use those classes in the Main Dr. Clincy - Lecture 5

Example: Event-Driven Programming Import the following for the events and handlers Creating and setting

Example: Event-Driven Programming Import the following for the events and handlers Creating and setting up pane Creating the OK button Creating the Cancel button Creating the OK button’s handler from the class created (recall the listener must be an instance of a listener interface) Registering the newly created handler with the OK button (recall a listener must be registered with a source object) Creating the Cancel button’s handler from the class created Registering the newly created handler with the Cancel button Mapping the OK and Cancel buttons to the pane Setting the stage Now the OK and Cancel buttons can “listen” for mouse click events as the program runs – and react accordingly. Dr. Clincy - Lecture 6

Event Classes Source object (e. g. , button, polygon, image, etc. ) Event object

Event Classes Source object (e. g. , button, polygon, image, etc. ) Event object (e. g. , mouse click, mouse pointer over object, type characters into a textfield, etc ) Java’s root class for an event object is java. util. Event. Object Java. FX’s root class for an event is javafx. event. Event There are three types of events Dr. Clincy - Lecture Listener object contains a method for processing the event 7

Some Examples: Selected User Actions and Handlers Dr. Clincy - Lecture 8

Some Examples: Selected User Actions and Handlers Dr. Clincy - Lecture 8

Animation Class Java. FX provides the Animation class with the core functionality for all

Animation Class Java. FX provides the Animation class with the core functionality for all animations. Below are some of the core functionalities for Java. FX A Boolean that answers will the animations. animation reverse its direction o the next cycle Also, there are many concrete subclasses of the Animation class – your book covers the Path. Transition, Fade. Transition and Timeline classes. Dr. Clincy - Lecture 9

Path. Transition Class The Path. Transition class animates the moves of a node along

Path. Transition Class The Path. Transition class animates the moves of a node along a path from one end to the other over a given time • • The Duration class defines a duration time. Yon can use new Duration(double millis) to create an instance of Duration The add, subtract, multiple and divide methods are used to perform arithmetic operations You can use to. Hours(), to. Minutes(), to. Seconds() and to. Millis() to return timein different measures You can use compare. To to compare two durations Dr. Clincy - Lecture 10

Example: Path. Transition Write a program that animates a rectangle moving along a circle.

Example: Path. Transition Write a program that animates a rectangle moving along a circle. Import the following for animation Creating a rectangle Creating a circle Adding the circle and rectangle to the pane Creating a Path. Transition Setting the transition duration Setting the path of the transition Setting the node of the transition Setting the orientation of the node during the transition Setting the cycle count to indefinite Setting Auto. Reverse to be true Starting the animation Pausing the animation Resuming the animation Setting the stage Dr. Clincy - Lecture 11

Fade. Transition Class The Fade. Transition class animates the change of the opacity (transparency)

Fade. Transition Class The Fade. Transition class animates the change of the opacity (transparency) in a node over a given time. Dr. Clincy - Lecture 12

Example: Fade. Transition Write a program that applies a fade transition to a filled

Example: Fade. Transition Write a program that applies a fade transition to a filled color in a ellipse. Import the following for animation Creating a ellipse Setting ellipse’s fill color Setting ellipse’s outline Binding the ellipse to the pane Add ellipse to pane Create Fade. Transition Setting starting transparency level Setting ending transparency level Setting cycle count Setting Auto. Reverse to be true Starting the animation Pausing the animation Resuming the animation Setting the stage Dr. Clincy - Lecture 13

Timeline Class q q Path. Transition and Fade. Transition classes define specialized animations. The

Timeline Class q q Path. Transition and Fade. Transition classes define specialized animations. The Timeline class can be used to program any animation using one or more Key. Frames. Each Key. Frame is executed sequentially at a specified time interval. Timeline inherits from Animation. Dr. Clincy - Lecture 14

Example: Timeline Class Write a program that flashes text on and off Import the

Example: Timeline Class Write a program that flashes text on and off Import the following for animation and text Create a stack pane Create the text Add the text to the pane Create handler for changing text (this causes the on and off) Set text empty Set text Create a timeline Create a Key. Frame for handler Set cycle count indefinite Starting the animation Pausing the animation Resuming the animation Setting the stage Dr. Clincy - Lecture 15

Cover Project 2 Dr. Clincy - Lecture 16

Cover Project 2 Dr. Clincy - Lecture 16