Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 15 Event-Driven Programming and Animations

Similar presentations


Presentation on theme: "Chapter 15 Event-Driven Programming and Animations"— Presentation transcript:

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

2 Inner Class Listeners (RECALL) A listener class is designed specifically to create a listener object for a GUI component (e.g., a button). (RECALL) It will not be shared by other applications. (NEW) So, it is appropriate to define the listener class inside the frame class as an inner class. An Inner class is a member of another class. Advantages: In some applications, you can use an inner class to make programs simple and concise. An inner class can reference the data and methods defined in the outer class in which it nests, so you do not need to pass the reference of the outer class to the constructor of the inner class. Dr. Clincy - Lecture

3 Example - Inner Classes
( 1 ) This is a case of two separate classes ( 2 ) This is a case of Class A being an inner class to Class Test ( 3 ) This is another case of Class InnerClass being an inner class to Class OuterClass Dr. Clincy - Lecture

4 Recall: 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 KeyFrame for handler Set cycle count indefinite Starting the animation Pausing the animation Resuming the animation Setting the stage Dr. Clincy - Lecture

5 Anonymous Inner Classes
An Inner class listeners can be shortened using an anonymous inner classes. An anonymous inner class is an inner class without a name. It combines declaring an inner class and creating an instance of the class in one step. An anonymous inner class is declared as follows: new SuperClassName/InterfaceName() { // Implement or override methods in superclass or interface // Other methods if necessary } Original approach with two steps One step approach Dr. Clincy - Lecture

6 Simplifying Event Handing Using Lambda Expressions
Lambda expression is a new feature in Java 8. Lambda expressions can be used to further simplify the code for event handling. Dr. Clincy - Lecture

7 The MouseEvent Class A MouseEvent is fired whenever a mouse button is pressed, released, clicked, moved or dragged on a node or a scene. Four constants are used, PRIMARY for left, SECONDARY for right, MIDDLE for middle and NONE for none. You can use the getButton () method to detect which button is pressed – getButton () == MouseButton.SECONDARY tests if right button was pressed. Dr. Clincy - Lecture

8 The KeyEvent Class A KeyEvent is fired whenever a keyis pressed, released or typed on a node or a scene. Dr. Clincy - Lecture

9 The KeyCode Constants Dr. Clincy - Lecture


Download ppt "Chapter 15 Event-Driven Programming and Animations"

Similar presentations


Ads by Google