Box是JavaFX的一部分。 Box类定义3D框的width,height和depth。盒子以原点为中心。
该类的构造函数是:
Box():创建Box的空实例。
Box(double w, double h, double d):创建具有指定宽度,高度和深度的Box的空实例。
常用方法
方法
说明
getDepth()
得到盒子的深度。
getWidth()
得到盒子的宽度。
getHeight()
得到盒子的高度
setHeight(double v)
设置盒子的高度
setWidth(double v)
设置盒子的宽度
setDepth(double v)
设置盒子的深度
以下程序将说明Box类的用法。
Java程序创建一个盒子并将其显示在舞台上
该程序创建一个由名称框指示的Box(高度,宽度和深度作为参数传递)。盒子将在场景中创建,而场景又将被放置在舞台中。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加框。该组将附加到场景。最后,调用show()方法以显示最终结果。
// 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);
}
}
输出:
Java程序创建一个框并添加透视相机以渲染3D对象
该程序创建一个由名称框指示的Box(高度,宽度和深度作为参数传递)。盒子将在场景中创建,而场景又将被放置在舞台中。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加框。该组将附加到场景。最后,将调用show()方法以显示最终结果。将创建一个透视摄像机,并将其添加到场景中以3D渲染框。
// 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);
}
}