Box is a part of JavaFX. Box class defines a 3 dimensional box width, height and depth . The box is centered at origin.
Constructor of the class are:
- Box(): creates an empty instance of Box.
- Box(double w, double h, double d): creates an empty instance of Box with specified width, height and depth.
Commonly used methods
method | explanation |
---|---|
getDepth() | get the depth of the box. |
getWidth() | get the width of the box. |
getHeight() | get the height of the box |
setHeight(double v) | set the height of the box |
setWidth(double v) | set the width of the box |
setDepth(double v) | set the depth of the box |
The following programs will illustrate the use of Box class.
Java program to create a box and display it on the stage
This program creates a Box indicated by the name box( the height, width and depth is passed as arguments). The Box will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the box is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.
Java
// Java program to create a box and display it on the stage import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.shape.DrawMode; import javafx.scene.layout.*; import javafx.event.ActionEvent; import javafx.scene.shape.Box; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.scene.Group; public class box_0 extends Application { // launch the application public void start(Stage stage) { // set title for the stage stage.setTitle( "creating box" ); // create a box Box box = new Box( 200 .0f, 120 .0f, 150 .0f); // create a Group Group group = new Group(box); // translate the box to a position box.setTranslateX( 100 ); box.setTranslateY( 100 ); // create a scene Scene scene = new Scene(group, 500 , 300 ); // set the scene stage.setScene(scene); stage.show(); } public static void main(String args[]) { // launch the application launch(args); } } |
Output:
Java program to create a box and add a perspective camera to render the 3D object
This program creates a Box indicated by the name box( the height, width and depth is passed as arguments). The Box will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the box is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.A perspective camera will be created and added to the scene to render the box in 3D.
Java
// Java program to create a box and add a // perspective camera to render the 3D object import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.shape.DrawMode; import javafx.scene.layout.*; import javafx.event.ActionEvent; import javafx.scene.shape.Box; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; public class box_1 extends Application { // launch the application public void start(Stage stage) { // set title for the stage stage.setTitle( "creating box" ); // create a box Box box = new Box( 70 .0f, 70 .0f, 40 .0f); // create a Group Group group = new Group(box); // translate the box to a position box.setTranslateX( 100 ); box.setTranslateY( 100 ); // create a perspective camera PerspectiveCamera perspectivecamera = new PerspectiveCamera( false ); perspectivecamera.setTranslateX( 0 ); perspectivecamera.setTranslateY( 0 ); perspectivecamera.setTranslateZ( 0 ); // create a scene Scene scene = new Scene(group, 500 , 300 ); // set camera for scene scene.setCamera(perspectivecamera); // set the scene stage.setScene(scene); stage.show(); } public static void main(String args[]) { // launch the application launch(args); } } |
Output:
Note : The above programs might not run in an online IDE please use an offline compiler
Reference
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Box.html