22 JavaFX Graphics and
Multimedia
Objectives
In this chapter you’ll:
Use JavaFX graphics and
multimedia capabilities to
Display two-dimensional
shape nodes of types Line,
Rectangle, Circle,
Ellipse, Arc, Path,
Polyline and Polygon.
Transition and Timeline
animations.
Use an AnimationTimer to
create frame-by-frame
animations.
Self-Review Exercises 2
Self-Review Exercises
22.1 Fill in the blanks in each of the following statements:
a) Selectors that begin with are known as ID selectors—they are applied to ob-
jects with the specified ID.
b) XML comments begin with and end with –> and can span multiple lines.
c) The x– and y-coordinate values are measured by default from the corner of
the layout, with x-coordinates increasing left to right and y-coordinates increasing top
to bottom.
d) The CSS property indicates how lines should be terminated.
e) A(n) defines colors that transition gradually from one color to the next.
f) A(n) can be applied to any UI element to reposition or reorient the graphic.
g) To perform a task for a given state of a MediaPlayer, you specify an object that imple-
ments interface (package java.lang).
h) A FadeTransition changes a Node’s .
i) Each Transition animation uses a(n) to calculate new property values
throughout the animation’s duration.
j) Package javafx.scene.canvas contains two classes: Canvas is a subclass of Node in which
you can draw graphics and performs the drawing operations on a Canvas.
k) objects represent color stops in a gradient.
l) Colors and images you see on three-dimensional shapes are created by applying
to the shapes.
m) JavaFX class is used to define materials.
22.2 State whether each of the following is true or false. If false, explain why.
a) Each CSS rule begins with a CSS collector which specifies the JavaFX objects that will
be styled according to the rule.
b) An XML start tag consists of square brackets ([ and ]) containing the element’s name
followed by zero or more attributes.
c) It’s possible to load CSS files dynamically and add them to a Scene’s collection of style
sheets, which you can access via the getStyleSheets method.
d) For each property you set in Scene Builder, there is a corresponding attribute in FXML.
3Chapter 22 JavaFX Graphics and Multimedia
e) You specify a CSS type selector by using the JavaFX class name.
f) A Polyline draws a series of connected lines defined by a set of points and connects the
last point to the first point.
g) PathElement subclass MoveTo moves to a specific position without drawing anything.
h) For video, JavaFX supports MPEG-4 (also called MP4) and Flash Video formats.
i) Playing multiple videos requires a separate MediaPlayer for each Media object.
j) Transition animations change a Node’s property values from one value to another in a
specified amount of time. Only one property of a Node can be animated.
k) Setting an animation’s cycle count to Timeline.INFINITE performs an animation until
its stop method is called or the program terminates.
l) For smooth animation, JavaFX tries to display animation frames at 30 frames per sec-
ond.
m) You cannot define animations that operate at the same overall speed, regardless of the
frame rate on a given device.
n) Class AnimationTimer is a concrete class.
o) If you call setLineWidth to specify the GraphicsContext’s line thickness, all subsequent
GraphicsContext method calls that draw lines or shape borders will use this setting.
p) A three-dimensional object’s depth is measured along the z-axis which runs parallel to
your screen.
q) PhongMaterial method setSpecularColor sets the color of a bright spot that makes a
3D shape appear shiny. PhongMaterial method setSpecularPower determines the in-
tensity of that spot.
Exercises
NOTE: Solutions to the programming exercises are located in the ch22solutions folder.
Each exercise has its own folder named ex22_## where ## is a two-digit number representing
the exercise number. For example, Exercise 22.3’s solution is located in the folder ex22_03.
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.