×

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for this purpose are present in the javafx.scene.shape.Box class.

Properties and corresponding methods to create Box:

1. height

This property of the Box class allows us to define the height of Box and the setHeight() method helps to set it.

2. width

This property of the Box class allows us to define the width of Box and the setWidth) the method helps to set it.

3. depth

This property of the Box class allows us to define the depth of Box and the setDepth() method helps to set it.

Constructors of Box class in JavaFX:

1. Box()

This is the simple constructor of the Box class. It requires setters to set the value of height, width, and depth.

2. Box(double width_value, double height_value, double depth_value)

This is the parameterized constructor of the Box class and it allows us to pass values of width, height, and depth in its constructor.

JavaFX 3D Shape – Simple Box on colored container

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.*;
import javafx.scene.shape.Box;  


public class BoxUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
        Box box1 = new Box();  
        box1.setLayoutX(50);
        box1.setLayoutY(50);


        box1.setHeight(200);  
        box1.setWidth(200);  
        box1.setDepth(400);  
        box1.setTranslateX(200);  
        box1.setTranslateY(200);  
        box1.setTranslateZ(200);  


        PerspectiveCamera camera = new PerspectiveCamera();  
        camera.setTranslateX(100);  
        camera.setTranslateY(100);  
        camera.setTranslateZ(50);  
          


         Group box = new Group();  
         box.getChildren().addAll ( box1 );


         box.getChildren().addAll();      
         Scene scene = new Scene( box, 600, 400, Color.RED);  
         scene.setCamera(camera);
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 3D shapes - Box Example");  
           primaryStage.show();  
        }


    public static void main(String[] args)
  {
        Application.launch(args);
    }
}

Output:

In order to create the Box in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Box, javafx.scene.paint.*, javafx.scene.PerspectiveCamera.

Then we have created one class named BoxUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Box, a Group object is created which is then passed to the Scene class object.

The Box is created using the setters method to set the height, width, and depth of the Box. Also, the PerspectiveCamera object is created to view the 3D shape of the container.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 3D shapes - Box Example”. Also, it displays a Box with defined height, width, and depth.

JavaFX 3D Shape -Box

JavaFX 3D Shape – Colored Box on simple container

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial; 
import javafx.scene.shape.Box;  


public class BoxUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
        Box box1 = new Box();  
        box1.setLayoutX(50);
        box1.setLayoutY(50);


        box1.setHeight(200);  
        box1.setWidth(200);  
        box1.setDepth(400);  
        box1.setTranslateX(200);  
        box1.setTranslateY(200);  
        box1.setTranslateZ(200);  
        PhongMaterial blueStuff = new PhongMaterial();
        blueStuff.setDiffuseColor(Color.BLUE);
        box1.setMaterial(blueStuff);


        PerspectiveCamera camera = new PerspectiveCamera();  
        camera.setTranslateX(100);  
        camera.setTranslateY(100);  
        camera.setTranslateZ(50);  
          
         Group box = new Group();  
         box.getChildren().addAll(box1);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         scene.setCamera(camera);
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 3D shapes - Colored Box Example");  
           primaryStage.show();  
        }


    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the colored Box on simple container in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Box, javafx.scene.paint.*, javafx.scene.PerspectiveCamera, javafx.scene.paint.Color, javafx.scene.paint.PhongMaterial.

Then we have created one class named BoxUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a colored Box, a Group object is created which is then passed to the Scene class object.

The Box is created using the setters method to set the height, width, and depth of the Box. Also, the PerspectiveCamera object is created to view the 3D shape of the container. The setMaterial() method is used to set the color shape for the Box.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 3D shapes – Colored Box Example”. Also, it displays a Colored Box with defined height, width, and depth.

JavaFX 3D Shape -Box

Related Topics

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.