Java Reference
In-Depth Information
Call the following
animate()
method after the
stage
is shown:
private void animate() {
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(translateX, -320)),
new KeyFrame(Duration.seconds(2), new KeyValue(translateX, 320)));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
When the First3DExample program is run with the preceding animation, you see the sphere move from the left
side of the window to the right side of the window, confirming that the
x
coordinate increases from left to right. In the
same manner, you can run the program with the
sphere
's
translateY
property animated from -240 to 240, and see
the sphere move from the top of the window to the bottom of the window, confirming that the
y
coordinate increases
from top to bottom. In both animations, the highlight of the sphere changes as the sphere moves across the screen in
a way that is consistent with the light being at the center of the window. In theory, you can try to run the program with
the
sphere
's
translateZ
being animated from -500 to 500. However, because the default camera is a parallel camera,
even though the sphere is indeed moving from negative
z
coordinate to positive
z
coordinate, the sphere remains the
same size. You can only discern the movement of the sphere by the way its highlight changes. Another phenomenon
that can be observed when the
translateZ
of the
sphere
is animated is that the sphere is not visible for the two
extremes of the
z
coordinate range.
Finally, turn your attention to the color of the sphere. In our program, we did not specify the material of the
sphere. What you see is the result of the JavaFX runtime system giving the sphere a default material.
In the next few sections, we will teach you all about the 3D objects, cameras, lights, and materials that are
available in the JavaFX 3D graphics API.
Understanding JavaFX 3D Objects
The JavaFX 3D graphics API provides several
Node
s that represent 3D objects. These classes reside in the
javafx.
scene.shape
package, along with their 2D siblings. There is a common base class
Shape3D
, and four concrete
subclasses:
Sphere
,
Box
,
Cylinder
, and
MeshView
. The
Sphere
,
Box
, and
Cylinder
are predefined 3D shapes, also
called 3D primitives. The
MeshView
is a user-defined shape.
Understanding the
Shape3D
Base Class
The
Shape3D
class is a subclass of
Node
. It includes the following public methods:
•
void setMaterial(Material)
•
Material getMaterial()
•
ObjectProperty<Material> materialProperty()
•
void setDrawMode(DrawMode)
•
DrawMode getDrawMode()
•
ObjectProperty<DrawMode> drawModeProperty()
•
void setCullFace(CullFace)
•
CullFace getCullFace()
•
ObjectProperty<CullFace> cullFaceProperty()
Search WWH ::
Custom Search