通常, 可以将盒子定义为具有所有面为矩形的三维形状。盒子通常是长方体, 具有高度, 深度和宽度三个维度。在JavaFX中, 框由类javafx.scene.shape.Box表示。我们只需要实例化此类即可创建该框。
下图显示了立方体(盒子)的高度, 宽度和深度。
物产
该类包含下表中描述的各种属性。
属性 | 描述 | 设置方法 |
---|---|---|
depth | 这是一个双精度类型的属性。它代表Box的Z维。 | setDepth(double value) |
height | 这是一个双精度类型的属性。它表示Box的Y维。 | setHeight(double value) |
width | 这是一个双精度类型的属性。它代表盒子的X维度。 | setWidth(double value) |
建设者
该类包含以下描述的两个构造函数。
public Box():使用默认参数创建Box类的实例。
public Box(double width, double height, double depth):创建具有指定尺寸的Box类的实例
例
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.stage.Stage;
public class BoxExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Creating Boxes
Box box1 = new Box();
Box box2 = new Box();
//Setting properties for second box
box2.setTranslateX(450);
box2.setTranslateY(300);
box2.setTranslateZ(100);
box2.setHeight(150);
box2.setWidth(50);
box2.setDepth(400);
//Setting properties for first box
box1.setHeight(100);
box1.setWidth(100);
box1.setDepth(400);
box1.setTranslateX(200);
box1.setTranslateY(200);
box1.setTranslateZ(200);
//Setting the perspective camera
PerspectiveCamera camera = new PerspectiveCamera();
camera.setTranslateX(100);
camera.setTranslateY(100);
camera.setTranslateZ(50);
//Configuring Group, Scene and Stage
Group root = new Group();
root.getChildren().addAll(box1, box2);
Scene scene = new Scene(root, 450, 350, Color.LIMEGREEN);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.setTitle("Box Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
评论前必须登录!
注册