×

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 Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 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 ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

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 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 Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.