📅  最后修改于: 2020-10-14 05:41:36             🧑  作者: Mango
通常,球体可以定义为圆形3D对象,其表面上的每个点均与其中心等距。球体可以看作是在3维平面中创建的圆,其中每个坐标都包含一个额外的维Z。
现实世界中Sphere的示例是地球仪,球等。在JavaFX中,Sphere由类javafx.scene.shape.Sphere表示。我们只需要实例化此类即可创建球体。下图显示了一个球体。
下表描述了类的属性及其设置方法。
Property | Description | Setter Methods |
---|---|---|
radius | It is a double type property. It represents the radius of the sphere. | SetRadius(double radius) |
有三个构造函数
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.CullFace;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class SphereExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//creating the sphere
Sphere s = new Sphere();
//setting the properties for the sphere object
s.setRadius(100);
s.setTranslateX(200);
s.setTranslateY(150);
s.setCullFace(CullFace.BACK);
//setting camera
PerspectiveCamera camera = new PerspectiveCamera();
camera.setTranslateX(-50);
camera.setTranslateY(0);
camera.setTranslateZ(0);
//setting group and stage
Group root = new Group();
root.getChildren().addAll(s);
Scene scene = new Scene(root,500,300,Color.LIMEGREEN);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.setTitle("Sphere Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}