Updated March 31, 2023
Introduction to JavaFX 3D
The 3D shapes in JavaFX are geometrical figures that can be represented on the X, Y and Z planes simultaneously. X plane represents length or depth, Y plane represents the height and the Z plane represents width. General 3D shapes are Cube, Cuboid, Cylinder, Cone, Sphere, etc. 3D shapes are included in Shape3D class and all shapes are included in the Shape class (2D and 3D shapes). Shape3D is the base class of all shapes. Shape class is available within the scene. Shape package. We can make use of it by importing this package.
Create a 3-Dimensional Shape
- Create the object for the required 3D shape.
- Add all the required properties.
- Add the 3D shape object reference to any JavaFX element like Group, VBox, HBox, Pane, etc.
Frequently Used Shapes
Below are the Frequently used shapes and their description
1. Cuboid
- The cuboid is a three-dimensional shape with a length (depth), width, and height.
- A cuboid has 3D shapes with length, height, and width. This is available in Box Box class is within javafx.scene.shape package.
2. Cylinder
- The cylinder is a closed solid object that has 2 parallel circle bases on top and bottom.
- A cylinder has height and radius properties. Each circle has the same radius. A cylinder is available inside Cylinder
- This Cylinder class is available within the scene. shape package.
3. Sphere
- The Sphere is a circle like structure, which have a set of points that are kept all are in the equal distance with “r” radius.
- The sphere exists inside the Sphere
- This Sphere class is available within the scene. shape package.
Constructors
- Box(): It creates the Box object with a new keyword.
- Cylinder(): It creates the Cylinder object with a new keyword.
- Sphere(): It creates the Sphere object with a new keyword.
Importance Cull Face Property
Culling means remove the improperly aligned parts of the 3D or 2D object shapes. By applying this cull property we can make these parts not visible in the shape view area. We can set cull face property by setCullFace(CullFace.Type)
This method consisting of 3 arguments:
- NONE: This is the default property. It does not have any cull face.
- FRONT: All the front face polygon parts are removed.
- BACK: All the back facing polygon parts are removed.
Frequently Used Methods
- setRadius(double r): It sets the radius.
- setTranslateX(int x): It sets the horizontal location of this view relative to the left position.
- setTranslateY(int y): It sets the vertical location of this view relative to the right position.
- setTranslateZ(int z): It sets either horizontal or vertical location of this view relative either left to the right position.
- setWidth(double w): It sets the width.
- setHeight(double h): It sets the height.
- setDepth(doubled): It sets the depth.
- setMaterial(): It is used to set the rendering of the shape.
How do 3D Shapes work in JavaFX?
1. Accessing JavaFX features user-defined class must extend Application. Creating Cuboid or Sphere or Cylinder etc class is 1st This classes can be created by using the new keyword.
Syntax #1
Box cuboidRef=new Box();
Or
Sphere sphereRef=new Sphere();
Or
Cylinder cylinderRef=new Cylinder();
2. Creating a Group element is the 2nd It is used to add the canvas object.
Syntax #2
Group groupRef = new Group(cuboidRef or sphereRef or cylinderRef );
3. 3rd step is creating Scene for apply show method on to it.
Syntax #3
Scene screen = new Scene(groupRef, length, width);
4. Adding Scene reference screen to the Stage object reference is 4th Adding output screen to Stage. We will get this stage object reference from the start predefined JavaFX method.
Syntax #4
stage.setScene(screen);
5. 5th step is showing output to the end-user by applying the show () method on the scene object.
Syntax #5
stage.show();
Examples to Implement JavaFX 3D
Below are the examples:
Example #1
Sphere with Green background-color
Code:
package com.shapes;
import javafx.application.Application;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class SphereDemo extends Application {
@Override
public void start(Stage outputStageObject) {
//setting the title to the application
outputStageObject.setTitle("Sphere Demo");
//creating sphere object
Sphere sphereObject = new Sphere(50);
//creating group object
Group groupObject = new Group();
groupObject.getChildren().add(sphereObject);
//creating camera object
Camera perspectiveCamera = new PerspectiveCamera();
//creating scene object for adding group object
Scene sceneObject = new Scene(groupObject, 500, 500);
sceneObject.setFill(Color.GREEN);
sceneObject.setCamera(perspectiveCamera);
//setting translate property for sphere object
sphereObject.translateXProperty().set(500 / 2);
sphereObject.translateYProperty().set(500 / 2);
//Key pressed event handler
outputStageObject.addEventHandler(KeyEvent.KEY_PRESSED, eventHandler -> {
switch (eventHandler.getCode()) {
//increase the sphere size if we press A
case A:
sphereObject.translateZProperty().set(sphereObject.getTranslateZ() + 300);
break;
//decrease the sphere size if we press B
case B:
sphereObject.translateZProperty().set(sphereObject.getTranslateZ() - 300);
break;
}
});
outputStageObject.setScene(sceneObject);
//displaying output
outputStageObject.show();
}
public static void main(String[] args) {
// JVM calls start method automatically
launch(args);
}
}
Output:
Example #2
User-Defined Triangle
Code:
package com.shapes;
import javafx.animation.Animation;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class UserDefinedShapes extends Application {
public MeshView createMeshView() {
// Points for triangle
float[] allPoints = { 50, 0, 0, 45, 10, 0, 55, 10, 0 };
// coordinates for triangle
float[] coordinatesValues = { 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f };
// face values of triangle
int[] faceValues = { 0, 0, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2 };
// Creating triangle mesh demo
TriangleMesh mesh = new TriangleMesh();
mesh.getPoints().addAll(allPoints);
mesh.getTexCoords().addAll(coordinatesValues);
mesh.getFaces().addAll(faceValues);
// Creating mesh view
MeshView meshView = new MeshView();
meshView.setMesh(mesh);
return meshView;
}
@Override
public void start(Stage stageOutObject) {
// Set the Title of the Stage
stageOutObject.setTitle("Triangle Movable 3D Demo");
// creating mesh view object
MeshView createMeshView = this.createMeshView();
createMeshView.setTranslateX(100);
createMeshView.setTranslateY(150);
createMeshView.setTranslateZ(500);
// scaling the mesh view to visible larger then usual size
createMeshView.setScaleX(11.0);
createMeshView.setScaleY(11.0);
createMeshView.setScaleZ(11.0);
// Create camera object to view 3d shapes clearly
PerspectiveCamera cameraPerspective = new PerspectiveCamera(false);
cameraPerspective.setTranslateX(100);
cameraPerspective.setTranslateY(-150);
cameraPerspective.setTranslateZ(500);
// create rotation object for rotating the triangle for certain duration
RotateTransition rotateShape = new RotateTransition(Duration.seconds(3), cameraPerspective);
rotateShape.setCycleCount(Animation.INDEFINITE);
rotateShape.setFromAngle(-40);
rotateShape.setToAngle(40);
rotateShape.setAutoReverse(true);
rotateShape.setAxis(Rotate.Y_AXIS);
rotateShape.play();
// Creating a point light object for setting color to the shape
PointLight colorLight1 = new PointLight();
colorLight1.setColor(Color.CHARTREUSE);
colorLight1.setTranslateX(350);
colorLight1.setTranslateY(250);
colorLight1.setTranslateZ(500);
// Creating a point light object for setting color to the shape
PointLight colorLight2 = new PointLight();
colorLight2.setColor(Color.GREEN);
colorLight2.setTranslateX(300);
colorLight2.setTranslateY(250);
colorLight2.setTranslateZ(550);
// Create group object for adding elements
Group groupObject = new Group(createMeshView, colorLight1, colorLight2);
// Rotate the triangle with 90 degrees
groupObject.setRotationAxis(Rotate.Y_AXIS);
groupObject.setRotate(90);
// creating scene object for adding group object
Scene scene = new Scene(groupObject, 450, 400, true);
scene.setCamera(cameraPerspective);
stageOutObject.setScene(scene);
// Display the output
stageOutObject.show();
}
public static void main(String[] args) {
// JVM calls start method
Application.launch(args);
}
}
Output:
Example #3
Cylinder
Code:
package com.shapes;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Cylinder;
import javafx.stage.Stage;
public class CylinderDemo extends Application {
@Override
public void start(Stage outputStageObject) throws Exception {
// setting title for cylinder application
outputStageObject.setTitle("Cylinder 3D Demo");
// Creating cylinder object
Cylinder cylinderObject = new Cylinder();
cylinderObject.setRadius(65);
cylinderObject.setHeight(200);
cylinderObject.setTranslateX(200);
cylinderObject.setTranslateY(140);
// creating camera object for adding 3d shapes
PerspectiveCamera perspectveCamera = new PerspectiveCamera();
perspectveCamera.setTranslateX(10);
perspectveCamera.setTranslateY(10);
perspectveCamera.setTranslateZ(30);
// setting group and stage
Group groupObject = new Group();
groupObject.getChildren().addAll(cylinderObject);
// creating scene object for adding group object
Scene scene = new Scene(groupObject, 400, 300, Color.DARKORCHID);
scene.setCamera(perspectveCamera);
outputStageObject.setScene(scene);
// display the output
outputStageObject.show();
}
public static void main(String[] args) {
// JVM calls strat method automatically
launch(args);
}
}
Output:
Conclusion
JavaFX 3D shapes are existed within javafx.scene.Shape class. Cylinder, Cuboid, Sphere, 3d triangle, etc. have come into 3-dimensional shapes. These 3D shapes are used in Space applications, civil engineering applications, aviation applications, etc.
Recommended Articles
This is a guide to JavaFX 3D. Here we discuss 3-dimensional shape, constructors, how JavaFX 3D work, and examples to implement for better understanding. You can also go through our other related articles to learn more –