Javafx 简明教程

JavaFX - Creating a Sphere

球体是三维空间中的一个完美的圆形几何对象,也就是一个完全圆形的球的表面。

A sphere is a perfectly round geometrical object in a three-dimensional space that is the surface of a completely round shaped ball.

球体定义为到 3D 空间中给定点距离均为 r 的点的集合。此距离 r 是球体的 radius ,给定点是球体的中心。

A sphere is defined as the set of points that are all at the same distance r from a given point in a 3D space. This distance r is the radius of the sphere and the given point is the centre of the sphere.

3d sphere

Sphere in JavaFX

在 JavaFX 中,球体由名为 Sphere 的类表示。此类属于 javafx.scene.shape 包。通过实例化此类,您可以在 JavaFX 中创建球体节点。

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

此类具有名为 radius*of double datatype. It represents the radius of a Sphere. To draw a Sphere, you need to set values to this property by passing it to the constructor of this class at the time of instantiation; Or, by using a setter method named *setRadius() 的属性。

This class has a property named radius*of double datatype. It represents the radius of a Sphere. To draw a Sphere, you need to set values to this property by passing it to the constructor of this class at the time of instantiation; Or, by using a setter method named *setRadius().

Steps to Draw 3D Sphere

请遵循以下步骤,在 JavaFX 中绘制球体(3D)。

Follow the steps given below to Draw a Sphere (3D) in JavaFX.

Step 1: Creating a Sphere

通过实例化属于 javafx.scene.shape 包的名为 Sphere 的类,在 JavaFX 中创建球体。您可以在 start() 方法中实例化此类,如下所示。

Create a Sphere in JavaFX by instantiating the class named Sphere, 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 class Sphere
      Sphere sphere = new Sphere();
   }
}

Step 2: Setting Properties to the Sphere

如下所示,使用名为 setRadius() 的方法设置球体的半径。

Set the radius of the Sphere using the method named setRadius() as shown below.

//Setting the radius of the Sphere
sphere.setRadius(300.0);

Step 3: Creating a Group Object

如下所示,通过将 sphere 对象作为参数传递给它的构造函数对 Group 类进行实例化 -

Instantiate the Group class by passing sphere object as a parameter to its constructor, as shown below −

Group root = new Group(sphere);

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 生成球体的操作方法如下所示。将此代码保存在名为 SphereExample.java 的文件中。

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

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

public class SphereExample extends Application {
   @Override
   public void start(Stage stage) {
      //Drawing a Sphere
      Sphere sphere = new Sphere();

      //Setting the properties of the Sphere
      sphere.setRadius(50.0);

      sphere.setTranslateX(200);
      sphere.setTranslateY(150);

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

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

      //Setting title to the Stage
      stage.setTitle("Drawing a Sphere - draw fill");

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

执行上述程序后,将在 JavaFX 窗口中生成一个球体,如下所示。

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

drawing 3dsphere

Example

在以下程序中,我们通过为 JavaFX 应用程序的场景着色,在 JavaFX 中应用了一些 CSS。将此代码保存在名为 CSSSphereExample.java 的文件中。

In the following program, we are applying some CSS in JavaFX by colouring the scene of JavaFX application. Save this code in a file with the name CSSSphereExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere;

public class CSSSphereExample extends Application {
   @Override
   public void start(Stage stage) {
      //Drawing a Sphere
      Sphere sphere = new Sphere();

      //Setting the properties of the Sphere
      sphere.setRadius(50.0);

      sphere.setTranslateX(100);
      sphere.setTranslateY(150);

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

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

	  scene.setFill(Color.ORANGE);

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

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

执行上述程序后,将在 JavaFX 窗口中生成一个球体,如下所示。

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

translate sphere