Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
JavaFX 3D shapes are geometrical figures that can be represented on the X, Y, and Z planes at the same time. The length is represented by the X plane, the height by the Y plane, and the width by the Z plane. Cube, Cuboid, Cylinder, Cone, Sphere, and other general 3D shapes Shape3D class includes 3D shapes, and the Shape class includes all shapes (2D and 3D shapes). Shape3D is the foundation class for all shapes. Within the scene, the Shape class is available. Package shape. We can put it to use by importing this package.
Create a Three-Dimensional Shape
Create the required 3D shape object.
Add all of the necessary properties.
Add a 3D shape object reference to any JavaFX element, such as Group, VBox, HBox, Pane, etc.
3D shape types in JavaFX
In JavaFX, 3D shapes are classified into two types:
3D shapes that are predefined JavaFX includes predefined 3D shape classes such as Cylinder, Sphere, and Box. We simply need to instantiate these classes to display these shapes on the screen. The classes contain various properties and methods that must be used to generate the appropriate shapes.
3D shapes defined by the user The class javafx.scene. JavaFX provides shape.TriangleMesh is a subclass of the abstract class javafx.scene.shape.Mesh. This class allows users to define their points, texture coordinates, and faces as class properties.
Shapes That Are Frequently Used
The frequently used shapes and their descriptions are listed below.
The cuboid A cuboid is a three-dimensional shape with three dimensions: length (depth), width, and height. A cuboid has three dimensions: length, height, and width. This is available in the Box Box class, which is part of the javafx.scene.shape package.
The cylinder The cylinder is a closed solid object with top and bottom parallel circle bases. Height and radius are properties of a cylinder. The radius of each circle is the same. Inside, there is a cylinder. CylinderThis Cylinder class is present in the scene. package shape
The sphere The Sphere is a circle-like structure with a collection of points; all kept at the same distance with an "r" radius. The sphere can be found within the Sphere. This Sphere class is present in 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 of Cull Face Property
Culling refers to removing improperly aligned parts of 3D or 2D object shapes. Using the cull property, we can hide these parts in the shape view area. Set the cull face property with setCullFace (CullFace.Type)
This method takes three arguments:
None: This is the default setting. There is no cull face on it.
Front: All front face polygon parts have been removed.
BACK: All back-facing polygon parts have been removed.
Methods That Are Frequently Used
setRadius (double r): This command determines the radius.
set TranslateX(int x): This function determines the horizontal position of this view in relation to the left position.
setTranslateY(int y): This function determines the vertical position of this view in relation to the correct position.
setTranslateZ(int z): This function sets this view's horizontal or vertical position relative to the left or right position.
setwidth (double w): This specifies the width.
setheight (double h): This specifies the height.
setDepth(doubled): This determines the depth.
setMaterial() is used to specify how the shape will be rendered.
How do 3D Shapes function in JavaFX?
To access JavaFX features, the user-defined class must extend the Application. The first class is Cuboid, Sphere, Cylinder, and so on. The new keyword is used to create these classes. Box cuboidRef = new Box(); Or Sphere sphereRef = new Sphere(); Or Cylinder cylinderRef=new Cylinder();
The second step is to create a Group element. It's used to insert the canvas object. Group groupRef = new Group(cuboidRef or sphereRef or cylinderRef);
In The third step, create a Scene and apply the show method to it. Scene screen = new Scene(groupRef, length, width);
It is possible to add a Scene reference screen to the Stage object reference. Fourth, add an output screen to the stage. This stage object reference will be obtained from the start predefined JavaFX method. stage.setScene(screen);
The fifth step is to display the output to the end-user using the show () method on the scene object. stage.show();
Examples to Implement JavaFX 3D
Example 1
Sphere with Green background-color
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);
}
}
Example 2
User-Defined Triangle
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);
}
}
Example 3
Cylinder
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);
}
}
FAQs
What is JavaFX transformation?
Transformation implies transforming some graphics into something else by applying various rules. Translation, scaling up or down, rotation, shearing, and other transformations are available in JavaFX.
A user can use JavaFX to perform transformations on nodes such as rotation, scaling, and translation. All of these transformations are represented by several classes in the package javafx.scene.transform.
What are JavaFX UI Controls?
The elements that are exposed to the user for intercommunication or information exchange are the UI controls. The structure of the UI elements displayed on the screen is represented by a layout. The response is the UI component's reaction to an event that occurs on it.
However, the package javafx.scene.control includes all of the necessary classes for UI elements such as buttons, labels, and so on. Each class describes a specific UI control and explains a few styling methods.
How many different kinds of charts are there in JavaFX?
Charts in JavaFX are classified into the following types:
Pie Chart - The areas of a circle are used to define several proportions of the total information in a Pie Chart. The class javafx.scene.chart in JavaFX. PieChart is used to work with pie charts.
XYChart - The data in XYChart is drawn on the XY (horizontal and vertical) axes. The X-axis represents one type of value, while the Y-axis represents the other. To display the appropriate information, a mapping is performed between the values plotted on X and Y charts. To work with the XYChart in JavaFX, use the class javafx.scene.chart.XYChart.
Conclusion
In this article, we have extensively discussed the concepts of JavaFX 3D shapes. We started by introducing the JavaFX 3D shapes, creating a 3-D shape, types of JavaFX 3D shapes, frequently used JavaFX 3D shapes, constructors of JavaFX 3D shapes, and frequently used methods of JavaFX 3D shapes, how JavaFX 3D shapes function, then concluded with JavaFX 3D shapes implementation.
Peeps out there who want to learn more can refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Do upvote our blog to help other ninjas grow.