Sphere類別用以建立球體,其建構函式如下,以radius參數設定球體的半徑,此外divisions參數設定球體的分割等分,以產生不同類型的立體,請參考後續範例:
public Sphere()
public Sphere(double radius)
public Sphere(double radius, int divisions)
並提供以下方法:
- getRadius():取得球體的半徑。
- setRadius():設定球體的半徑。
- getDivisions():取得球體的分割等分。
// 建立Sphere物件 Sphere sphere = new Sphere(100); // 以Wireframe方式呈現 sphere.setDrawMode(DrawMode.LINE); // 不省略 sphere.setCullFace(CullFace.NONE); // 平移Sphere物件 sphere.setLayoutX(150); sphere.setLayoutY(150); // 旋轉Sphere物件 sphere.getTransforms().add(new Rotate(20,Rotate.X_AXIS)); sphere.getTransforms().add(new Rotate(10,Rotate.Z_AXIS)); sphere.getTransforms().add(new Rotate(30,Rotate.Y_AXIS)); Group root = new Group(); root.getChildren().add(sphere); ...【執行結果】
// 建立Sphere物件 Sphere sphere = new Sphere(100, 100); // 以Wireframe方式呈現 sphere.setDrawMode(DrawMode.LINE); // 省略Back Face sphere.setCullFace(CullFace.BACK); ...【執行結果】
// 建立Sphere物件 Sphere sphere = new Sphere(100, 100); // 以Wireframe方式呈現 sphere.setDrawMode(DrawMode.LINE); // 省略Front Face sphere.setCullFace(CullFace.FRONT); ...【執行結果】
// 建立Sphere物件 Sphere sphere = new Sphere(100, 100); // 以填滿方式呈現 sphere.setDrawMode(DrawMode.FILL); // 省略Back Face sphere.setCullFace(CullFace.BACK); ...【執行結果】
以下範例示範setMaterial()方法設定貼圖:
// 建立Sphere物件
Sphere sphere = new Sphere(100);
// 以填滿方式呈現
sphere.setDrawMode(DrawMode.FILL);
Image image = new Image(
getClass().getResourceAsStream("images/earthmap.jpg"));
// 設定材質
PhongMaterial material = new PhongMaterial();
// 設定貼圖
material.setDiffuseMap(image);
// 設定物件表面的材質
sphere.setMaterial(material);
Group root = new Group();
root.getChildren().add(box);
...
【執行結果】divisions參數用以設定球體的分割等分,以下是以Sphere建構函式設定球體的分割等分:
// 建立Sphere物件
Sphere sphere = new Sphere(100, 8);
Image image = new Image(
getClass().getResourceAsStream("images/Javafx.png"));
// 設定材質
PhongMaterial material = new PhongMaterial();
// 設定貼圖
material.setDiffuseMap(image);
...
【執行結果】【參考資料】
[1] 黃嘉輝,深入研究JavaFX 2。
[2] 黃嘉輝,JavaFX遊戲程式設計。
[3] Java Official Web Site:http://www.oracle.com/technetwork/java/index.html
[4] JavaFX:http://www.oracle.com/technetwork/java/javafx
[5] JavaFX 8.0 API Specification.
[6] Java Platform, Standard Edition 8 API Specification.
[7] JDK 8 Certified System Configurations
[8] H. M. Cundy, A. P. Rollett, Mathematical Models, Tarquin Publications, 1981.
[9] G. Sellers, R. S. Wright, N. Haemel, OpenGL SuperBible: Comprehensive Tutorial and Reference (6th Edition), Addison-Wesley Professional, 2013.
© Chia-Hui Huang






沒有留言:
張貼留言