Javafx 简明教程
JavaFX - Creating a Box
长方体是三维立体形状。长方体由 6 个矩形组成,这些矩形垂直放置。如果长方体使用方形面,则它是立方体;如果面是矩形,与立方体不同,则它看起来像鞋盒。
A cuboid is a three-dimensional solid shape. Cuboids are composed of 6 rectangles, which are placed at right angles. A cuboid that uses square faces is a cube, if the faces are rectangles, other than cubes, it looks like a shoe box.
长方体是具有 length (深度)、 width 和 height 的三维形状,如下面的图表所示 −
A cuboid is a three-dimensional shape with a length (depth), width, and a height as shown in the following diagram −

在 JavaFX 中,这种类型的三维形状称为 Box;因为它可以是长方体或立方体,具体取决于形状的尺寸。
In JavaFX, this type of three-dimensional shape is addressed as a Box; as it can either be a cuboid or cube, depending on the measurements of the shape.
Box in JavaFX
在 JavaFX 中,三维框由一个名为 Box 的类表示。此类属于 javafx.scene.shape 包。
In JavaFX, a 3-dimensional box is represented by a class named Box. This class belongs to the package javafx.scene.shape.
通过实例化此类,您可以在 JavaFX 中创建一个 Box 节点。
By instantiating this class, you can create a Box node in JavaFX.
此类具有 3 个 double 数据类型的属性,即 −
This class has 3 properties of the double datatype, which are −
-
width − The width of the box.
-
height − The height of the box.
-
depth − The depth of the box.
要绘制三次曲线,您需要通过将值传递给此类的构造函数来传递这些属性。在实例化时必须按照相同的顺序执行此操作;或者,使用它们的各自 setter 方法。
To draw a cubic curve, you need to pass values to these properties by passing them to the constructor of this class. This has to be done in the same order at the time of instantiation; or, by using their respective setter methods.
Steps to Draw 3D Box
若要在 JavaFX 中绘制 3D 盒子,请执行以下步骤。
To Draw a 3D box in JavaFX, follow the steps given below.
Step 1: Creating a Box
可以通过实例化名为 BOX 、属于包 javafx.scene.shape 的类来在 JavaFX 中创建盒子。可以在 Application 类的 start() 方法中按如下方式实例化此类。
You can create a Box in JavaFX by instantiating the class named BOX, which belongs to a package javafx.scene.shape. You can instantiate this class within the start() method of the Application class as follows.
public class ClassName extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//Creating an object of the class Box
Box box = new Box();
}
}
Step 2: Setting Properties to the Box
使用各自的 setter 方法设置 3D 盒子的属性 Width, Height 和 Depth ,如以下代码块所示。
Set the properties of the 3D box, Width, Height and Depth, using their respective setter methods as shown in the following code block.
//Setting the properties of the Box
box.setWidth(200.0);
box.setHeight(400.0);
box.setDepth(200.0);
Step 3: Creating a Group Object
在 start() 方法中,通过实例化名为 Group 、属于包 javafx.scene 的类来创建组对象。
In the start() method, create a group object by instantiating the class named Group, which belongs to the package javafx.scene.
将前一步中创建的盒子(节点)对象作为参数传递给 Group 类构造函数。应执行此操作以将其添加到组,如下所示:
Pass the Box (node) object, created in the previous step, as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows −
Group root = new Group(box);
Step 4: Launching an Application
创建 3D 对象后,按以下步骤启动 JavaFX 应用程序:
Once the 3D object is created, launch the JavaFX application by following the steps below −
-
Instantiate the class named Scene by passing the Group object as a parameter value to its constructor. You can also pass dimensions of the application screen as optional parameters to the constructor.
-
Set the title to the stage using the setTitle() method of the Stage class.
-
Add a scene object to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method within the Application class.
Example
以下是使用 JavaFX 生成的 3D 盒子程序。将此代码保存在名为 BoxExample.java 的文件中。
Following is a program which generates a 3D box using JavaFX. Save this code in a file with the name BoxExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.stage.Stage;
public class BoxExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Box
Box box = new Box();
//Setting the properties of the Box
box.setWidth(200.0);
box.setHeight(400.0);
box.setDepth(200.0);
//Creating a Group object
Group root = new Group(box);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a Box");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls BoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls BoxExample
执行后,上述程序将生成一个 JavaFX 窗口,显示一个如下图所示的 3D 盒子:
On executing, the above program generates a JavaFX window displaying a 3D Box as shown below −

Example
在前面的示例中,我们没有指定用以绘制盒子的起始和结束坐标。但是,使用动画类的 translateX 和 translateY 属性,可以重新定位 JavaFX 应用程序中的盒子。我们来看下面的一个示例,并将其保存在名为 TranslateBoxExample.java 的文件中。
In the previous example, we did not specify the start and end coordinates to draw the box from. However, using translateX and translateY properties of animation class, we can relocate the box on the JavaFX application. Let us look at an example below and save it in a file named TranslateBoxExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.paint.Color;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
public class TranslateBoxExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Box
Box box = new Box();
//Setting the properties of the Box
box.setWidth(200.0);
box.setHeight(200.0);
box.setDepth(200.0);
Translate translate = new Translate();
translate.setX(200);
translate.setY(150);
translate.setZ(25);
box.getTransforms().addAll(translate);
//Creating a Group object
Group root = new Group(box);
//Creating a scene object
Scene scene = new Scene(root, 400, 300);
scene.setFill(Color.web("#81c483"));
//Setting title to the Stage
stage.setTitle("Translate a Box");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateBoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateBoxExample
执行后,上述程序将生成一个 JavaFX 窗口,显示一个如下图所示的 3D 盒子。我们着色场景以区分盒子的平移位置。
On executing, the above program generates a JavaFX window displaying a 3D Box as shown below. We coloured the scene in order to differentiate the translated location of the box.
