Sphere
Sphere類別用以建立球體,其建構函式如下:
public Sphere()
public Sphere(double radius)
public Sphere(double radius, int divisions)
其中參數:
radius:設定球體的半徑。divisions:設定球體的分割等分,以產生不同類型的立方柱體。
其方法包括:
getRadius():取得球體的半徑。setRadius():設定球體的半徑。getDivisions():取得球體的分割等分。
請注意並沒有setDivisions()方法,因此欲設定球體的分割等分,僅能使用Sphere類別的建構函式宣告。
請參考以下範例。
程式說明
範例示範以Sphere類別建立球體,並分別以setCullFace()與setDrawMode()方法設定不省略描繪任何一面與以線框方式繪製球體:
// 建立球體
Sphere sphere;
sphere = new Sphere();
// 設定球體的半徑
sphere.setRadius(100);
// 設定不省略描繪任何一面
sphere.setCullFace(CullFace.NONE);
// 設定以線框方式繪製
sphere.setDrawMode(DrawMode.LINE);
// 平移Sphere物件
sphere.setLayoutX(150);
sphere.setLayoutY(150);
// 旋轉Sphere物件
sphere.getTransforms().add(new Rotate(30, Rotate.X_AXIS));
sphere.getTransforms().add(new Rotate(30, Rotate.Y_AXIS));
sphere.getTransforms().add(new Rotate(30, Rotate.Z_AXIS));
...
執行結果

請參考以下範例。
程式說明
範例示範以setCullFace()方法設定省略描繪Back Face:
// 建立球體
Sphere sphere;
sphere = new Sphere();
// 設定球體的半徑
sphere.setRadius(100);
// 設定省略描繪Back Face
sphere.setCullFace(CullFace.BACK);
// 設定以線框方式繪製
sphere.setDrawMode(DrawMode.LINE);
...
執行結果

請參考以下範例。
程式說明
範例示範以setCullFace()方法設定省略描繪Front Face:
// 建立球體
Sphere sphere;
sphere = new Sphere();
// 設定球體的半徑
sphere.setRadius(100);
// 設定省略描繪Front Face
sphere.setCullFace(CullFace.FRONT);
// 設定以線框方式繪製
sphere.setDrawMode(DrawMode.LINE);
...
執行結果

請參考以下範例。
程式說明
範例示範以setDrawMode()方法設定以填滿的方式繪製球體:
// 建立球體
Sphere sphere;
sphere = new Sphere();
// 設定球體的半徑
sphere.setRadius(100);
// 設定省略描繪Back Face
sphere.setCullFace(CullFace.BACK);
// 設定以填滿的方式繪製球體
sphere.setDrawMode(DrawMode.FILL);
...
執行結果

請參考以下範例。
程式說明
範例示範以Shape3D抽象類別的setMaterial()方法設定物體表面的材質,其中以PhongMaterial類別的setDiffuseColor()與setSpecularColor()方法分別設定漫射光與反射光的顏色。此外並以Sphere類別的建構函式設定球體的分割等分:
// 建立球體
Sphere sphere;
// 設定divisions參數
sphere = new Sphere(100, 8);
// 設定省略描繪Back Face
sphere.setCullFace(CullFace.BACK);
// 設定以填滿的方式繪製球體
sphere.setDrawMode(DrawMode.FILL);
// 設定Phong Material
PhongMaterial material = new PhongMaterial();
// 設定漫射光顏色
material.setDiffuseColor(Color.RED);
// 設定反射光顏色
material.setSpecularColor(Color.WHITE);
// 設定物體表面的材質
sphere.setMaterial(material);
...
執行結果

請參考以下範例。
程式說明
範例示範以Shape3D抽象類別的setMaterial()方法設定物體表面的材質,其中以PhongMaterial類別的setDiffuseMap()方法設定漫射貼圖:
// 建立球體
Sphere sphere;
sphere = new Sphere(100);
// 設定省略描繪Back Face
sphere.setCullFace(CullFace.BACK);
// 設定以填滿的方式繪製球體
sphere.setDrawMode(DrawMode.FILL);
Image image = new Image(
getClass().getResourceAsStream("images/JavaFX.png"));
// 設定Phong Material
PhongMaterial material = new PhongMaterial();
// 設定漫射貼圖
material.setDiffuseMap(image);
// 設定物體表面的材質
sphere.setMaterial(material);
...
此外並以RotateTransition類別處理旋轉效果,分別以下列方法處理旋轉效果的相關設定:
setNode():設定執行旋轉效果的物體。setAxis():設定旋轉的座標軸。setCycleCount():設定旋轉的執行次數。setDuration():設定旋轉效果的間隔時間。setFromAngle():設定旋轉的開始角度。setToAngle():設定旋轉的結束角度。
當完成上述設定之後,則以RotateTransition類別play()方法執行,以產生如同地球自轉的效果:
// 設定旋轉效果
RotateTransition rotatetransition = new RotateTransition();
// 設定執行旋轉效果的物件
rotatetransition.setNode(group);
// 設定旋轉的座標軸
rotatetransition.setAxis(Rotate.Y_AXIS);
// 設定旋轉的執行次數
rotatetransition.setCycleCount(RotateTransition.INDEFINITE);
// 設定旋轉效果的間隔時間
rotatetransition.setDuration(Duration.seconds(20));
// 設定旋轉的開始角度
rotatetransition.setFromAngle(360);
// 設定旋轉的結束角度
rotatetransition.setToAngle(0);
// 執行旋轉效果
rotatetransition.play();
...
執行結果
