Table of contents
1.
Introduction
2.
JavaFX 3D Object Properties
3.
Cull Faces
4.
Example to show the 3D object properties
4.1.
Output
5.
3D Object Material
5.1.
Properties
6.
Drawing Modes
7.
JavaFX Camera
8.
JavaFX Subscene
8.1.
Program to display the cylinder as a 2D object
8.1.1.
Output
9.
Frequently Asked Questions
9.1.
What is JavaFX?
9.2.
How do I include a picture in a JavaFX scene?
9.3.
What does a JavaFX stage mean?
10.
Conclusion
Last Updated: Mar 27, 2024

3D Object Properties

Author Ashish Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will discuss JavaFX 3D Object Properties, its material, drawing modes, camera, and sub-scenes. Ultimately, we will look into a coded example of JavaFX 3D Object Properties. We will start with a brief introduction to JavaFX 3D Object Properties and move further into the topic for a detailed description.

difference between 2d object and 3d object

Without further ado, let's start by learning JavaFX 3D Object Properties

JavaFX 3D Object Properties

We may modify the different attributes of 3D objects using JavaFX and alter JavaFX 3D Object Properties. Each property in JavaFX has a particular class defined for it. Geometrical forms that can independently be displayed on the X, Y, and Z planes are 3D shapes in JavaFX. 

The X, Y, and Z planes express length, height, and width, respectively. Cube, Cuboid, Cylinder, Cone, Sphere, etc., are examples of common 3D shapes. The shape3D class includes 3D shapes, and the Shape class consists of all shapes (2D and 3D). The foundation class for all shapes is Shape3D. 

The following are some of the JavaFX 3D Object Properties.

Cull Faces

The act of deleting the portions of 3D objects that are not visible in the view region is known as culling. 3D shapes in JavaFX have a property of type CullFace. The 3D shape class object should use the instance method shown below to set this property to an acceptable value.

The CullFace class defines the following three types of strokes:

None: There is no culling.

Front: All front-facing polygons are eliminated.

Back: All polygons that face the back are removed. 

Example to show the 3D object properties

 

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) {

// title to the application
outputStageObject.setTitle("Sphere Demo");

//  creating the sphere object
Sphere sphereObject = new Sphere(50);

// creating the group object
Group groupObject = new Group();

//calling the sphere object
groupObject.getChildren().add(sphereObject);

//creating the camera object
Camera perspectiveCamera = new PerspectiveCamera();

//defining scene object for addition of group object
Scene sceneObject = new Scene(groupObject, 300, 300);
sceneObject.setFill(Color.GREEN);

//setting up the camera 
sceneObject.setCamera(perspectiveCamera);

//adding translation property for sphere object for both X and Y axis
sphereObject.translateXProperty().set(300 / 2);
sphereObject.translateYProperty().set(300 / 2); 
 
//event handler for key pressing
outputStageObject.addEventHandler(KeyEvent.KEY_PRESSED, eventHandler -> {
switch (eventHandler.getCode()) {

//it increases the size of sphere if we press button A
case A:
sphereObject.translateZProperty().set(sphereObject.getTranslateZ() + 200);
break;

//it decreases the size of sphere if we press B
case B:
sphereObject.translateZProperty().set(sphereObject.getTranslateZ() - 200);
break;
}
});
outputStageObject.setScene(sceneObject)

//displaying the output on the screen
outputStageObject.show();
}

       // calling main
       public static void main(String[] args) {

        launch(args);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

sphere demo

3D Object Material

A 3D shape's surface can be covered with various materials thanks to JavaFX. All the classes that deal with the material of a 3D shape are provided by the class Material in the package javafx.scene.paint. The subclass of the class Material is the class PhongMaterial. All that is necessary to produce the desired material is to instantiate this class.

The properties of this class are provided by this class as a variety of materials, each of which can be set to a specific value using the setter methods. However, the following technique can be used to apply the material to a form.

shape.setMaterial(<material-type>)  

Properties

Drawing Modes

To draw three-dimensional shapes, JavaFX offers several drawing options. This attribute belongs to the DrawMode class. The class defines the following DrawMode kinds.

🔥 Fill: In this mode, the 3D shape is drawn and filled. DrawMode is a representation of it. Fill

🔥 Line: In this mode, only lines are used to draw the 3D shape. DrawMode is a representation of it. Line

To set a draw mode for the 3D shape, use the setter method that is listed below.

Shape3D.setDrawMode(<DrawMode value>)  

JavaFX Camera

The camera is represented as an object in JavaFX so that it can be moved about a 3D layout and alter the scene's view as needed. the javafx.scene class. Camera stands for the camera. It differs from a 2D space, though, where moving the camera around the screen is not necessary.

The X-coordinate, Y-coordinate, and Z-coordinate in the JavaFX camera coordinate system all point to the right, downward, and away from the viewer, respectively.

The following lines of code must be used to create the camera and add it to the scene.

  1. Camera camera = new Camera();  
  2. camera.setTranslateX(150);  
  3. camera.setTranslateY(150);   
  4. camera.setTranslateZ(120);  
  5. scene.setCamera(camera);  

JavaFX Subscene

A container that can store a scene node for which the camera angle needs to be specified is known as a subscene node. It is employed to divide scenes. If we need to set a separate camera for a specific area of the scene, we can do it by using the subscene node. One of the following situations allows for the use of subscene.

If the UI controls we are utilising in the scene require a static camera (overlay).

Y-up for 3D objects and Y-down for 2D user interface

Create a subscene by using the following syntax as an underlay for the background.

SubScene <reference> = new SubScene(Parent root, double width, double height, boolean depthbuffer, boolean antiAliasing)  

Program to display the cylinder as a 2D object

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 {
        outputStageObject.setTitle("Cylinder 3D Demo");
      
        // defining cylinder object
        Cylinder cylinderObject = new Cylinder();
        
        // setting properties for cylinder object
        cylinderObject.setRadius(55);
        cylinderObject.setHeight(150);
        cylinderObject.setTranslateX(150);
        cylinderObject.setTranslateY(120);
        
        PerspectiveCamera perspectveCamera = new PerspectiveCamera();
        perspectveCamera.setTranslateX(12);
        perspectveCamera.setTranslateY(12);
        perspectveCamera.setTranslateZ(36);
        
        Group groupObject = new Group();
        groupObject.getChildren().addAll(cylinderObject);
        
        // scene object for adjusting group object
        Scene scene = new Scene(groupObject, 200, 150, Color.DARKORCHID);
        scene.setCamera(perspectveCamera);
        outputStageObject.setScene(scene);
        
        // showing the output on screen
        outputStageObject.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

cylinder 3d demo

Frequently Asked Questions

What is JavaFX?

Developers can construct rich client applications that work reliably across a range of platforms by designing, building, testing, debugging, and deploying them using JavaFX, a collection of graphics and media packages.

How do I include a picture in a JavaFX scene?

A FileInputStream should represent the image you want to load. You bypass the input stream object you just generated and send it as a constructor parameter, instantiate the Image class instead. Create an instance of the ImageView class. By providing the image object as a parameter to the setImage() method, you can set the image to it.

What does a JavaFX stage mean?

A Scene, made up of visual components, is hosted by a Stage in JavaFX. The Stage class in the JavaFX represents a stage in a JavaFX application. The start(Stage s) method of the Application class receives the platform-created primary stage as a parameter.

Conclusion

In this article, we have extensively discussed the JavaFX 3D Object Properties along with JavaFX 3D Object Properties materials, drawing modes, camera, sub-scenes, and coded examples.

After reading about Introduction to 3D object properties, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. If you want to check out articles related to Java refer to these links, JavaFXJavaFX arcBubble chart using JavaFX, and java interview questions.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass