Javafx 简明教程

JavaFX - Creating a Cylinder

圆柱体是一个封闭的实体,它有两个通过曲面连接的平行(大多为圆形)底面。为了便于可视化,你可以将 3D 圆柱体想象成一组 2D 圆,它们堆叠在一起达到一定的高度;因此,即使它用两个参数进行描述,它也是一个三维形状。

A cylinder is a closed solid that has two parallel (mostly circular) bases connected by a curved surface. To visualize, you can think of a 3D cylinder as a clutter of 2D circles that are stacked on top of each other to a certain height; hence, making it a three-dimensional shape even though it is described by two parameters.

圆柱体参数为 – 圆形底部的 radius 和圆柱体的 height ,如下所示:

The parameters of a cylinder are – the radius of its circular base and the height of the cylinder as shown in the following diagram −

cylinder

Cylinder in JavaFX

在 JavaFX 中,圆柱体由称为 Cylinder 的类表示。此类属于包 javafx.scene.shape 。通过实例化此类,可以在 JavaFX 中创建圆柱体节点。

In JavaFX, a cylinder is represented by a class named Cylinder. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a cylinder node in JavaFX.

此类有 2 个 double 数据类型的属性:

This class has 2 properties of the double datatype namely −

  1. height − The height of the Cylinder.

  2. radius − The radius of the Cylinder.

要绘制圆柱体,需要将值传递给这些属性,方法是将它们传递给此类的构造函数。这可以在实例化 Cylinder 类的同时按相同顺序完成;或者,通过使用其各个 setter 方法。

To draw a cylinder, you need to pass values to these properties by passing them to the constructor of this class. This can be done in the same order at the time of instantiation of the Cylinder class; Or, by using their respective setter methods.

Steps to Draw 3D Cylinder

要在 JavaFX 中绘制圆柱体 (3D),请按照以下步骤操作。

To Draw a Cylinder (3D) in JavaFX, follow the steps given below.

Step 1: Creating a Class

在 JavaFX 中创建圆柱体对象,通过实例化属于包 javafx.scene.shape 的类 Cylinder 来实现。可以在 start() 方法中实例化此类,如下所示:

Create a Cylinder object in JavaFX by instantiating the class named Cylinder, which belongs to a package javafx.scene.shape. You can instantiate this class in the start() method as follows −

public class ClassName extends Application {
   @Override
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the Cylinder class
      Cylinder cylinder = new Cylinder();
   }
}

Step 2: Setting Properties to the Cylinder

使用其各个 setter 设置圆柱体的 heightradius ,如下所示。

Set the height and radius of the Cylinder using their respective setter as shown below.

//Setting the properties of the Cylinder
cylinder.setHeight(300.0f);
cylinder.setRadius(100.0f);

Step 3: Creating a Group Object

现在,通过实例化属于包 javafx.scene 的类 Group 来创建组对象。然后,将上一步创建的圆柱体 (节点) 对象作为参数传递给 Group 类的构造函数。应按如下方式对其进行操作,以便将其添加到组中:

Now, create a group object by instantiating the class named Group, which belongs to the package javafx.scene. Then, pass the Cylinder (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(cylinder);

Step 4: Launching an Application

创建 3D 对象后,按以下步骤启动 JavaFX 应用程序:

Once the 3D object is created, launch the JavaFX application by following the steps below −

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

  2. Set the title to the stage using the setTitle() method of the Stage class.

  3. Add a scene object to the stage using the setScene() method of the class named Stage.

  4. Display the contents of the scene using the method named show().

  5. Lastly, the application is launched with the help of the launch() method within the Application class.

Example

以下程序展示了如何使用 JavaFX 生成圆柱体。将此代码保存在一个名为 CylinderExample.java 的文件中。

The following program shows how to generate a Cylinder using JavaFX. Save this code in a file with the name CylinderExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.stage.Stage;

public class CylinderExample extends Application {
   @Override
   public void start(Stage stage) {
      //Drawing a Cylinder
      Cylinder cylinder = new Cylinder();

      //Setting the properties of the Cylinder
      cylinder.setHeight(300.0f);
      cylinder.setRadius(100.0f);

      //Creating a Group object
      Group root = new Group(cylinder);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Drawing a cylinder");

      //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 CylinderExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls CylinderExample

执行后,上述程序会生成一个 JavaFX 窗口,其中显示一个圆柱体,如下所示。

On executing, the above program generates a JavaFX window displaying a Cylinder as shown below.

drawing 3dcylinder

Example

你还可以对 3D 形状应用转换。在此示例中,我们将尝试对 3D 圆柱体应用平移变换,并将其重新放置在应用程序中。将此代码保存在名为 TranslateCylinderExample.java 的文件中。

You can also apply transformations on the 3D shape. In this example, we are trying to apply translate transformation on the 3D cylinder and relocate it on the application. Save this code in the file named TranslateCylinderExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.scene.paint.Color;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class TranslateCylinderExample extends Application {
   @Override
   public void start(Stage stage) {
      //Drawing a Cylinder
      Cylinder cylinder = new Cylinder();

      //Setting the properties of the Cylinder
      cylinder.setHeight(150.0f);
      cylinder.setRadius(100.0f);

      Translate translate = new Translate();
      translate.setX(200);
      translate.setY(150);
      translate.setZ(25);

      cylinder.getTransforms().addAll(translate);

      //Creating a Group object
      Group root = new Group(cylinder);

      //Creating a scene object
      Scene scene = new Scene(root, 400, 300);

      scene.setFill(Color.web("#81c483"));

      //Setting title to the Stage
      stage.setTitle("Drawing a cylinder");

      //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 TranslateCylinderExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateCylinderExample

执行后,上述程序会生成一个 JavaFX 窗口,其中显示一个圆柱体,如下所示。

On executing, the above program generates a JavaFX window displaying a Cylinder as shown below.

translate cylinder