在前面的章節中,我們已經了解了如何在 JavaFX 應用程序中的 XY 平面上繪制 2D 形狀。除了這些 2D 形狀之外,我們還可以使用 JavaFX 繪制其他幾個 3D 形狀。
通常,3D 形狀是可以在 XYZ 平面上繪制的幾何圖形。它們由兩個或多個維度定義,通常是 length, width and depth。JavaFX 支持的 3D 形狀包括 Cylinder、Sphere 和 Box。
上面提到的每個 3D 形狀都由一個類表示,所有這些類都屬于 javafx.scene.shape 包。名為 Shape3D 的類是 JavaFX 中所有 3 維形狀的基類。
創建 3D 形狀
要創建 3-Dimensional 形狀,需要
實例化相應的類
要創建一個 3-Dimensional 形狀,首先你需要實例化它各自的類。例如,如果要創建一個 3D 框,則需要實例化名為 Box 的類,如下所示
Box box = new Box();
設置形狀的屬性
實例化類后,需要使用 setter 方法設置形狀的屬性。
例如,要繪制 3D 框,需要傳遞其 Width、Height、Depth。您可以使用各自的 setter 方法指定這些值,如下所示
//Setting the properties of the Box
box.setWidth(200.0);
box.setHeight(400.0);
box.setDepth(200.0);
將 Shape 對象添加到組中
最后,需要通過將形狀的對象作為構造函數的參數傳遞來將其添加到組中,如下所示。?
//Creating a Group object
Group root = new Group(box);
S.No | 形狀和描述 |
1 | Box 長方體是具有length?(depth),?width, and a?height. 在 JavaFX 中,三維框由名為 Box 的類表示。此類屬于 javafx.scene.shape 包。 通過實例化此類,可以在 JavaFX 中創建一個 Box 節點。 此類具有 double 數據類型的 3 個屬性 width ? 框的寬度 height ? 框的高度 depth - 框的深度 |
2 | Cylinder 圓柱體是一種封閉的實體,具有兩個平行(大部分為圓形)的底面,由曲面連接。 它由兩個參數描述,即其圓形底面的半徑和圓柱體的高度。 在 JavaFX 中,圓柱體由名為 Cylinder 的類表示。此類屬于 javafx.scene.shape 包。 通過實例化此類,您可以在 JavaFX 中創建一個圓柱體節點。此類具有 double 數據類型的 2 個屬性 height?? 圓柱體的高度 radius - 圓柱體的半徑 |
3 | Sphere 球體定義為與 3D 空間中的給定點的距離相同的點集 r。這個距離 r 是球體的半徑,給定的點是球體的中心。 在 JavaFX 中,球體由名為 Sphere 的類表示。此類屬于 javafx.scene.shape 包。 通過實例化此類,可以在 JavaFX 中創建一個球體節點。 此類具有名為 radius 的 double 數據類型的屬性。它表示 Sphere 的半徑。 |
3D 對象的屬性
對于所有 3 維對象,可以在 JavaFX 中設置各種屬性。它們在下面列出?
?
JavaFX - 創建一個 Box
長方體是三維立體形狀。長方體由 6 個矩形組成,這些矩形以直角放置。使用方形面的長方體是立方體,如果面是矩形,而不是立方體,則它看起來像一個鞋盒。
長方體是具有 length (depth)、width 和 height 的三維形狀。在 JavaFX 中,這種類型的三維形狀被尋址為 Box;因為它可以是長方體或立方體,具體取決于形狀的測量值。
在 JavaFX 中,三維框由名為 Box 的類表示。此類屬于 javafx.scene.shape 包。通過實例化此類,可以在 JavaFX 中創建一個 Box 節點。?
繪制 3D 框的步驟
第 1 步:創建 Box
可以通過實例化名為 BOX 的類在 JavaFX 中創建 Box,該類屬于包 javafx.scene.shape 。您可以在 Application 類的 start() 方法中實例化此類,如下所示
public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception {//Creating an object of the class Box Box box = new Box(); }
}
第 2 步:將屬性設置為框
使用 3D 框各自的 setter 方法設置 3D 框的屬性 Width、Height 和 Depth ,如以下代碼塊所示
//Setting the properties of the Box
box.setWidth(200.0);
box.setHeight(400.0);
box.setDepth(200.0);
第 3 步:創建 Group 對象?
在 start() 方法中,通過實例化名為 Group 的類來創建 group 對象,該類屬于包 javafx.scene 。將上一步中創建的 Box(節點)對象作為參數傳遞給 Group 類的構造函數。為了將其添加到組中,應執行此作,如下所示
Group root = new Group(box);
第 4 步:啟動應用程序
例1
下面是一個使用 JavaFX 生成 3D 框的程序。將此代碼保存在名為 BoxExample.java 的文件中。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.stage.Stage; public class BoxExample extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box = new Box(); //Setting the properties of the Box box.setWidth(200.0); box.setHeight(400.0); box.setDepth(200.0); //Creating a Group object Group root = new Group(box); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle("Drawing a Box"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); }public static void main(String args[]){ launch(args); }
}
例2?
在前面的示例中,我們沒有指定要從中繪制框的開始和結束坐標。但是,使用 animation 類的 translateX 和 translateY 屬性,我們可以在 JavaFX 應用程序上重新定位該框。讓我們看一下下面的示例,并將其保存在名為 TranslateBoxExample.java 的文件中。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.paint.Color;
import javafx.scene.transform.Translate;
import javafx.stage.Stage; public class TranslateBoxExample extends Application { @Override public void start(Stage stage) { //Drawing a Box Box box = new Box(); //Setting the properties of the Box box.setWidth(200.0); box.setHeight(200.0); box.setDepth(200.0);Translate translate = new Translate(); translate.setX(200); translate.setY(150); translate.setZ(25); box.getTransforms().addAll(translate);//Creating a Group object Group root = new Group(box); //Creating a scene object Scene scene = new Scene(root, 400, 300);scene.setFill(Color.web("#81c483")); //Setting title to the Stage stage.setTitle("Translate a Box"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); }public static void main(String args[]){ launch(args); }
}
JavaFX - 創建圓柱體
圓柱體是一種封閉的實體,具有兩個平行(大部分為圓形)的底面,由曲面連接。為了可視化,您可以將 3D 圓柱體視為一堆雜亂的 2D 圓圈,這些圓圈彼此堆疊到一定高度;因此,即使它由兩個參數描述,也使其成為三維形狀。
繪制 3D 圓柱體的步驟
第 1 步:創建類
通過實例化名為 Cylinder 的類,在 JavaFX 中創建一個 Cylinder 對象,該類屬于包 javafx.scene.shape 。您=可以在 start() 方法中實例化此類,如下所示??
public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception {//Creating an object of the Cylinder class Cylinder cylinder = new Cylinder(); }
}
第 2 步:為 Cylinder 設置屬性
//Setting the properties of the Cylinder
cylinder.setHeight(300.0f);
cylinder.setRadius(100.0f);
?第 3 步:創建 Group 對象?
Group root = new Group(cylinder);
?第 4 步:啟動應用程序
例1
下面的程序演示如何使用 JavaFX 生成 Cylinder。將此代碼保存在名為 CylinderExample.java 的文件中。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.stage.Stage;public class CylinderExample extends Application { @Override public void start(Stage stage) { //Drawing a Cylinder Cylinder cylinder = new Cylinder(); //Setting the properties of the Cylinder cylinder.setHeight(300.0f); cylinder.setRadius(100.0f); //Creating a Group object Group root = new Group(cylinder); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting title to the Stage stage.setTitle("Drawing a cylinder"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); }
}
?例2
還可以對 3D 形狀應用轉換。在此示例中,我們嘗試在 3D 圓柱體上應用平移轉換,并在應用程序上重新定位它。將此代碼保存在名為 TranslateCylinderExample.java 的文件中。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.scene.paint.Color;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;public class TranslateCylinderExample extends Application { @Override public void start(Stage stage) { //Drawing a Cylinder Cylinder cylinder = new Cylinder(); //Setting the properties of the Cylinder cylinder.setHeight(150.0f); cylinder.setRadius(100.0f);Translate translate = new Translate(); translate.setX(200); translate.setY(150); translate.setZ(25); cylinder.getTransforms().addAll(translate); //Creating a Group object Group root = new Group(cylinder); //Creating a scene object Scene scene = new Scene(root, 400, 300); scene.setFill(Color.web("#81c483"));//Setting title to the Stage stage.setTitle("Drawing a cylinder"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); }
}
JavaFX - 創建球體
繪制 3D 球體的步驟
步驟 1:創建球體
public class ClassName extends Application { @Override public void start(Stage primaryStage) throws Exception {//Creating an object of the class Sphere Sphere sphere = new Sphere(); }
}
第 2 步:為球體設置屬性
//Setting the radius of the Sphere
sphere.setRadius(300.0);
?第 3 步:創建 Group 對象?
Group root = new Group(sphere);
?第 4 步:啟動應用程序
例1
以下程序演示如何使用 JavaFX 生成 Sphere。將此代碼保存在名為 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); }
}
例2?
?在下面的程序中,我們通過為 JavaFX 應用程序的場景著色來在 JavaFX 中應用一些 CSS。將此代碼保存在名為 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); }
}