130 likes | 151 Views
Enhance the Circle Control example by allowing users to adjust circle radius with mouse and keys. Add listeners for observable objects to process value changes. Utilize Animation and PathTransition classes for smooth animations.
E N D
Control Circle Project Revisited • Refer to CircleControlWithMouseAndKey • Eclipse Project that • Adds more control to the CircleControl example • Allowing end user to • Increase/decrease the circle radius by clicking left/right buttons • Increase/decrease the circle radius by pressing the U and D keys • getButton() • Detects which mouse button is pressed • MouseButton.PRIMARY, MouseButton.SECONDARY, …
Listeners for Observable Objects • A listener can be added • To process a value change in an observable object • An instance of Observable class • Is known as an observable object that contains the • addListener(InvalidationListener) for adding a listener • The listener must implement • the InvalidationListenerinterface • To override the invalidated(Observable o) method • To handle value change • Once the value in Observable changes • The listener is notified through the invalidated method
Animation Class • JavaFX provides the Animation class • With the core functionality for all animations
Animation Class (Continued) • TheCycleCountproperty • Indicates the number of cycles for the animation • The constant Timeline.INDEFINITEindicates an infinite number • The rate property • Defines the speed of the animation • The status property • Indicates the status of the animation: • Animation.Status.PAUSED • Animation.Status.RUNNING • Animation.Status.STOPPED
PathTransition Class • This class • Animates the moves of a node along a path • From one end to the other over a given time
PathTransition Class (Cont’d) • The Duration class • Defines a duration of time • Constants INDEFINITE, ONE, ZERO are defined • Offers • add, subtract, multiply, and divide methods • To perform arithmetic operations • toHours(), toMinutes(), toSeconds(), and toMillis() • To return number of hours, minutes, seconds, and ms in duration • Defines • PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT
PathTransition Class (Cont’d) • Refer to PathTransitionDemo Eclipse project • It moves a rectangle along the outline of a circle • An animation can be • Started by invoking the play() method • Paused by invoking the pause() method
PathTransition Class (Cont’d) • Refer to FlagRisingApp Eclipse project • It animates a flag rising • ImageView moves along a line • That may or may not be placed in the scene
FadeTransition Class • This class • Animates the changes of the opacity in a node • over a given time
FadeTransition Class (Cont’d) • Refer to FadeTransitionDemo Eclipse project • It applies a fade transition to the filled color in an ellipse • The ellipse’s • centerX, centerY, radiusX, and radiusY properties • Are bound to the pane’s size
Timeline Class • Timeline class • Can be used to program any animation using • One or more KeyFrames • Each KeyFrame • Is executed sequentially at a specified time interval • Can be constructed as follows: • The onFinished handler • Is called when the duration of the key frame is elapsed.
Timeline Class (Continued) • Refer to TimeLineDemo Eclipse project • Displays a flashing text. • The text is on and off alternating to animate flashing • A handler is used • To change the text to empty if it is not empty or • To “programming is fun” if the text is empty • The animation is set to run indefinitely
Timeline Class (Continued) • Refer to BouncingBallApp Eclipse project • Animates a bouncing ball • The mouse-pressed and mouse-released handlers • Are implemented to pause and resume the animation • When the UP arrow key is pressed • The ball’s movement is increased