JavaFX |带示例的球体
Sphere 是 JavaFX 的一部分。 Sphere 类用于创建具有指定半径的 3 维球体。球体以原点为中心。
Sphere 类继承 Shape3D 类。
该类的构造函数是
- Sphere() : 创建一个半径为 1.0 的新球体
- Sphere(double r) : 创建一个具有给定半径的新球体
- Sphere(double r, int div) : 创建一个具有给定半径和分割数的新球体
常用方法
method | explanation |
---|---|
getDivisions() | returns the number of divisions of the sphere |
getRadius() | returns the radius of sphere |
setRadius(double r) | sets the radius of the sphere to specified value |
下面的程序将说明Sphere类的使用
Java程序通过在构造函数中将半径作为参数传递来创建球体
该程序创建一个名为 sphere 的 Sphere(半径作为参数传递)。球体将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加球体。组附加到场景。最后调用 show() 方法显示最终结果。
Java
// Java program to create sphere by passing the radius
// as arguments in constructor
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.Sphere;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
public class sphere_0 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating sphere");
// create a sphere
Sphere sphere = new Sphere(80.0f);
// create a Group
Group group = new Group(sphere);
// translate the sphere to a position
sphere.setTranslateX(100);
sphere.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
// Java program to create a Sphere 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.Sphere;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class sphere_1 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating sphere");
// create a sphere
Sphere sphere = new Sphere(80.0f);
// create a Group
Group group = new Group(sphere);
// translate the sphere to a position
sphere.setTranslateX(100);
sphere.setTranslateY(100);
// create a perspective camera
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(0);
camera.setTranslateY(0);
camera.setTranslateZ(0);
// create a scene
Scene scene = new Scene(group, 500, 300);
scene.setCamera(camera);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
输出:
用于创建 Sphere 并添加透视相机以渲染 3D 对象的Java程序
该程序创建一个名为 sphere 的 Sphere(半径作为参数传递)。球体将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加球体。组附加到场景。最后,调用 show() 方法显示最终结果。将创建透视相机并将其添加到场景中,以 3D 渲染圆柱体。
Java
// Java program to create a Sphere 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.Sphere;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class sphere_1 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle("creating sphere");
// create a sphere
Sphere sphere = new Sphere(80.0f);
// create a Group
Group group = new Group(sphere);
// translate the sphere to a position
sphere.setTranslateX(100);
sphere.setTranslateY(100);
// create a perspective camera
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(0);
camera.setTranslateY(0);
camera.setTranslateZ(0);
// create a scene
Scene scene = new Scene(group, 500, 300);
scene.setCamera(camera);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器
参考:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Sphere.html