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 – 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.