Javafx-3d-shapes

提供:Dev Guides
移動先:案内検索

JavaFX-3Dシェイプ

前の章で、XY平面に2D形状を描く方法を見てきました。 これらの2D形状に加えて、JavaFXを使用して他のいくつかの3D形状も描画できます。

3D形状

一般に、3D形状は、XYZ平面に描画できる幾何学的図形です。 これらには、 Cylinder、Sphere および Box が含まれます。

上記の各3D形状はクラスによって表され、これらのすべてのクラスはパッケージ javafx.scene.shape に属します。 Shape3D という名前のクラスは、JavaFXのすべての3次元形状の基本クラスです。

3Dシェイプを作成する

3次元形状を作成するには、する必要があります-

  • 必要な3D形状のそれぞれのクラスをインスタンス化します。
  • 3D形状のプロパティを設定します。
  • 3D形状オブジェクトをグループに追加します。

各クラスのインスタンス化

3次元の形状を作成するには、まず、それぞれのクラスをインスタンス化する必要があります。 たとえば、3Dボックスを作成する場合、次のようにBoxという名前のクラスをインスタンス化する必要があります-

Box box = new Box();

シェイプのプロパティを設定する

クラスをインスタンス化した後、setterメソッドを使用して形状のプロパティを設定する必要があります。

たとえば、3Dボックスを描画するには、その幅、高さ、深さを渡す必要があります。 次のように、それぞれのセッターメソッドを使用してこれらの値を指定できます-

//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);

次の表に、JavaFXが提供するさまざまな3D形状のリストを示します。

S.No Shape & Description
1

Box

直方体は、長さ(深さ)、、および*高さ*を持つ3次元形状です。

JavaFXでは、3次元ボックスは Box という名前のクラスで表されます。 このクラスは、パッケージ javafx.scene.shape に属します。

このクラスをインスタンス化することにより、JavaFXでBoxノードを作成できます。

このクラスには、doubleデータ型の3つのプロパティがあります-

  • width -ボックスの幅。
  • height -ボックスの高さ。
  • depth -ボックスの深さ。
2

Cylinder

シリンダーは、曲面で接続された2つの平行な(ほとんど円形の)ベースを持つ閉じたソリッドです。

これは、2つのパラメーター、つまり、円形ベースの*半径*とシリンダーの*高さ*によって記述されます。

JavaFXでは、シリンダーは Cylinder という名前のクラスで表されます。 このクラスは、パッケージ javafx.scene.shape に属します。

このクラスをインスタンス化することにより、JavaFXでシリンダーノードを作成できます。 このクラスには、doubleデータ型の2つのプロパティがあります-

  • height -シリンダーの高さ。
  • 半径-シリンダーの半径。
3

Sphere

球体は、3D空間内の特定のポイントからすべて同じ距離rにあるポイントのセットとして定義されます。 この距離rは球の半径であり、与えられた点は球の中心です。

JavaFXでは、球体は Sphere という名前のクラスで表されます。 このクラスは、パッケージ javafx.scene.shape に属します。

このクラスをインスタンス化することにより、JavaFXで球体ノードを作成できます。

このクラスには、doubleデータ型の radius という名前のプロパティがあります。 球体の半径を表します。

3Dオブジェクトのプロパティ

すべての3次元オブジェクトについて、Cull Face、Drawing Mode、Materialなどのさまざまなプロパティを設定できます。

次のセクションでは、3Dオブジェクトのプロパティについて説明します。

カルフェイス

一般に、カリングとは、形状の不適切な方向の部分(ビュー領域には表示されない)を削除することです。

Cull Faceプロパティは CullFace タイプであり、3D形状のCull Faceを表します。 以下に示すように、メソッド* setCullFace()*を使用して、形状のCull Faceを設定できます-

box.setCullFace(CullFace.NONE);

形状のストロークタイプは-

  • なし-カリングは実行されません(CullFace.NONE)。
  • Front -すべての正面向きのポリゴンがカリングされます。 (CullFace.FRONT)。
  • Back -すべての裏面ポリゴンがカリングされます。 (StrokeType.BACK)。

デフォルトでは、3次元形状のカリング面は「戻る」です。

次のプログラムは、球体のさまざまなカリング面を示す例です。 このコードを SphereCullFace.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.stage.Stage;
import javafx.scene.shape.Sphere;

public class SphereCullFace extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing Sphere1
      Sphere sphere1 = new Sphere();

     //Setting the radius of the Sphere
      sphere1.setRadius(50.0);

     //Setting the position of the sphere
      sphere1.setTranslateX(100);
      sphere1.setTranslateY(150);

     //setting the cull face of the sphere to front
      sphere1.setCullFace(CullFace.FRONT);

     //Drawing Sphere2
      Sphere sphere2 = new Sphere();

     //Setting the radius of the Sphere
      sphere2.setRadius(50.0);

     //Setting the position of the sphere
      sphere2.setTranslateX(300);
      sphere2.setTranslateY(150);

     //Setting the cull face of the sphere to back
      sphere2.setCullFace(CullFace.BACK);

     //Drawing Sphere3
      Sphere sphere3 = new Sphere();

     //Setting the radius of the Sphere
      sphere3.setRadius(50.0);

     //Setting the position of the sphere
      sphere3.setTranslateX(500);
      sphere3.setTranslateY(150);

     //Setting the cull face of the sphere to none
      sphere2.setCullFace(CullFace.NONE);

     //Creating a Group object
      Group root = new Group(sphere1, sphere2, sphere3);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //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);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

javac SphereCullFace.java
java SphereCullFace

実行すると、上記のプログラムは、それぞれ次のように、カルフェイス値 FRONT、BACK および NONE を持つ3つの球体を表示するJavaFXウィンドウを生成します-

Cull Faces

描画モード

プロパティは DrawMode 型であり、現在の3D形状の描画に使用される描画モードを表します。 あなたは次のようにメソッドsetDrawMode()を使用して3D形状を描くために描画モードを選択することができます-

box.setDrawMode(DrawMode.FILL);

JavaFXでは、2つの描画モードを選択して3D形状を描画できます。

  • Fill -このモードは、2Dシェイプを描画して塗りつぶします(DrawMode.FILL)。
  • Line -このモードは、線を使用して3D形状を描画します(DrawMode.LINE)。

デフォルトでは、3次元形状の描画モードは塗りつぶしです。

次のプログラムは、3Dボックスのさまざまな描画モードを示す例です。 このコードを BoxDrawMode.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.stage.Stage;

public class BoxDrawMode extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing a Box
      Box box1 = new Box();

     //Setting the properties of the Box
      box1.setWidth(100.0);
      box1.setHeight(100.0);
      box1.setDepth(100.0);

     //Setting the position of the box
      box1.setTranslateX(200);
      box1.setTranslateY(150);
      box1.setTranslateZ(0);

     //Setting the drawing mode of the box
      box1.setDrawMode(DrawMode.LINE);

     //Drawing a Box
      Box box2 = new Box();

     //Setting the properties of the Box
      box2.setWidth(100.0);
      box2.setHeight(100.0);
      box2.setDepth(100.0);

     //Setting the position of the box
      box2.setTranslateX(450);//450
      box2.setTranslateY(150);//150
      box2.setTranslateZ(300);

     //Setting the drawing mode of the box
      box2.setDrawMode(DrawMode.FILL);

     //Creating a Group object
      Group root = new Group(box1, box2);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

     //Setting camera
      PerspectiveCamera camera = new PerspectiveCamera(false);
      camera.setTranslateX(0);
      camera.setTranslateY(0);
      camera.setTranslateZ(0);
      scene.setCamera(camera);

     //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);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

javac BoxDrawMode.java
java BoxDrawMode

実行時に、上記のプログラムは、次のように、それぞれ描画モード値LINEおよびFILLの2つのボックスを表示するJavaFXウィンドウを生成します-

描画モード

素材

cull Faceプロパティのタイプは Material であり、3D形状のマテリアルの表面を選択するために使用されます。 次のようにメソッド* setCullFace()*を使用して、3D形状のマテリアルを設定できます-

cylinder.setMaterial(material);

このメソッドについて前述したように、タイプMaterialのオブジェクトを渡す必要があります。 パッケージ javafx.scene.paintPhongMaterial クラスはこのクラスのサブクラスであり、Phongシェーディングマテリアルを表す7つのプロパティを提供します。 これらのプロパティのセッターメソッドを使用して、これらすべてのタイプのマテリアルを3Dシェイプの表面に適用できます。

以下は、JavaFXで利用可能な素材の種類です-

  • bumpMap -これは、RGBイメージとして保存された法線マップを表します。
  • diffuseMap -これは拡散マップを表します。
  • selfIlluminationMap -これは、このPhongMaterialの自己照明マップを表します。
  • specularMap -これは、このPhongMaterialのスペキュラーマップを表します。
  • diffuseColor -これは、このPhongMaterialの拡散色を表します。
  • specularColor -これは、このPhongMaterialの鏡面反射色を表します。
  • specularPower -これは、このPhongMaterialの鏡面屈折力を表します。

デフォルトでは、3次元シェイプのマテリアルは、ライトグレーの拡散色を持つPhongMaterialです。

以下は、シリンダー上のさまざまなマテリアルを表示する例です。 このコードを CylinderMaterials.java という名前のファイルに保存します。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.stage.Stage;

public class CylinderMaterials extends Application {
   @Override
   public void start(Stage stage) {
     //Drawing Cylinder1
      Cylinder cylinder1 = new Cylinder();

     //Setting the properties of the Cylinder
      cylinder1.setHeight(130.0f);
      cylinder1.setRadius(30.0f);

     //Setting the position of the Cylinder
      cylinder1.setTranslateX(100);
      cylinder1.setTranslateY(75);

     //Preparing the phong material of type bump map
      PhongMaterial material1 = new PhongMaterial();
      material1.setBumpMap(new Image
         ("http://www.finddevguides.com/images/tplogo.gif"));

     //Setting the bump map material to Cylinder1
      cylinder1.setMaterial(material1);

     //Drawing Cylinder2
      Cylinder cylinder2 = new Cylinder();

     //Setting the properties of the Cylinder
      cylinder2.setHeight(130.0f);
      cylinder2.setRadius(30.0f);

     //Setting the position of the Cylinder
      cylinder2.setTranslateX(200);
      cylinder2.setTranslateY(75);

     //Preparing the phong material of type diffuse map
      PhongMaterial material2 = new PhongMaterial();
      material2.setDiffuseMap(new Image
         ("http://www.finddevguides.com/images/tp-logo.gif"));

     //Setting the diffuse map material to Cylinder2
      cylinder2.setMaterial(material2);

     //Drawing Cylinder3
      Cylinder cylinder3 = new Cylinder();

     //Setting the properties of the Cylinder
      cylinder3.setHeight(130.0f);
      cylinder3.setRadius(30.0f);

     //Setting the position of the Cylinder
      cylinder3.setTranslateX(300);
      cylinder3.setTranslateY(75);

     //Preparing the phong material of type Self Illumination Map
      PhongMaterial material3 = new PhongMaterial();
      material3.setSelfIlluminationMap(new Image
         ("http://www.finddevguides.com/images/tp-logo.gif"));

     //Setting the Self Illumination Map material to Cylinder3
      cylinder3.setMaterial(material3);

     //Drawing Cylinder4
      Cylinder cylinder4 = new Cylinder();

     //Setting the properties of the Cylinder
      cylinder4.setHeight(130.0f);
      cylinder4.setRadius(30.0f);

     //Setting the position of the Cylinder
      cylinder4.setTranslateX(400);
      cylinder4.setTranslateY(75);

     //Preparing the phong material of type Specular Map
      PhongMaterial material4 = new PhongMaterial();
      material4.setSpecularMap(new Image
         ("http://www.finddevguides.com/images/tp-logo.gif"));

     //Setting the Specular Map material to Cylinder4
      cylinder4.setMaterial(material4);

     //Drawing Cylinder5
      Cylinder cylinder5 = new Cylinder();

     //Setting the properties of the Cylinder
      cylinder5.setHeight(130.0f);
      cylinder5.setRadius(30.0f);

     //Setting the position of the Cylinder
      cylinder5.setTranslateX(100);
      cylinder5.setTranslateY(300);

     //Preparing the phong material of type diffuse color
      PhongMaterial material5 = new PhongMaterial();
      material5.setDiffuseColor(Color.BLANCHEDALMOND);

     //Setting the diffuse color material to Cylinder5
      cylinder5.setMaterial(material5);

     //Drawing Cylinder6
      Cylinder cylinder6 = new Cylinder();

     //Setting the properties of the Cylinder
      cylinder6.setHeight(130.0f);
      cylinder6.setRadius(30.0f);

     //Setting the position of the Cylinder
      cylinder6.setTranslateX(200);
      cylinder6.setTranslateY(300);

     //Preparing the phong material of type specular color
      PhongMaterial material6 = new PhongMaterial();

     //setting the specular color map to the material
      material6.setSpecularColor(Color.BLANCHEDALMOND);

     //Setting the specular color material to Cylinder6
      cylinder6.setMaterial(material6);

     //Drawing Cylinder7
      Cylinder cylinder7 = new Cylinder();

     //Setting the properties of the Cylinder
      cylinder7.setHeight(130.0f);
      cylinder7.setRadius(30.0f);

     //Setting the position of the Cylinder
      cylinder7.setTranslateX(300);
      cylinder7.setTranslateY(300);

     //Preparing the phong material of type Specular Power
      PhongMaterial material7 = new PhongMaterial();
      material7.setSpecularPower(0.1);

     //Setting the Specular Power material to the Cylinder
      cylinder7.setMaterial(material7);

     //Creating a Group object
      Group root = new Group(cylinder1 ,cylinder2, cylinder3,
      cylinder4, cylinder5, cylinder6, cylinder7);

     //Creating a scene object
      Scene scene = new Scene(root, 600, 400);

     //Setting camera
      PerspectiveCamera camera = new PerspectiveCamera(false);
      camera.setTranslateX(0);
      camera.setTranslateY(0);
      camera.setTranslateZ(-10);
      scene.setCamera(camera);

     //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);
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します。

Javac CylinderMaterials.java
java CylinderMaterials

上記のプログラムは、実行時に、次のスクリーンショットに示すように、マテリアル、バンプマップ、拡散マップ、自己照明マップ、スペキュラーマップ、拡散カラー、スペキュラーカラー、(BLANCHEDALMOND)スペキュラーパワーを持つ7つのシリンダーを表示するJavaFXウィンドウを生成します−

シリンダー素材