Explain the properties of 3D object in JavaFX?


Following are the various properties of 3D objects −

  • Cull Face − In general, culling is the removal of improperly oriented parts of a shape (which are not visible in the view area).

    You can set the value to the cull face property of a 3d object using the setCullFace() method (Shape class).

    JavaFX supports three kinds of cull face types represented by three constants of the Enum named CullFace namely, NONE, FRONT, BACK.

  • Draw Mode − This property defines/specifies the mode used to draw the 3D shape.

    You can set the value to the draw mode property of a 3d object using the setDrawMode() method (Shape class).

    JavaFX supports two kinds of draw modes represented by constants of the Enum named DrawMode, FILL and LINE.

  • Material − This property specifies the type of material the 3D object should be covered with. You can set value to this property using the setMaterial() method.

Example

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.stage.Stage;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class JavaFX3DShapesProperties extends Application {
   public void start(Stage stage) {
      //Drawing a Box
      Box cube = new Box(100, 120, 100);
      cube.setTranslateX(30.0);
      cube.setTranslateY(180.0);
      cube.setTranslateZ(150.0);
      //Setting the property "cull face"
      cube.setCullFace(CullFace.FRONT);
      //Drawing a Cylinder
      Cylinder cylinder = new Cylinder(50, 150);
      //Setting the properties of the cylinder
      cylinder.setTranslateX(250.0);
      cylinder.setTranslateY(180.0);
      cylinder.setTranslateZ(150.0);
      //Setting the cull face "material"
      PhongMaterial material = new PhongMaterial();
      material.setDiffuseColor(Color.DARKRED);
      cylinder.setMaterial(material);
      //Drawing a Sphere
      Sphere sphere = new Sphere(75);
      sphere.setTranslateX(480.0);
      sphere.setTranslateY(180.0);
      sphere.setTranslateZ(150.0);
      //Setting the property "draw mode"
      sphere.setDrawMode(DrawMode.LINE);
      //Setting the perspective camera
      PerspectiveCamera cam = new PerspectiveCamera();
      cam.setTranslateX(-50);
      cam.setTranslateY(50);
      cam.setTranslateZ(0);
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 15);
      Text label1 = new Text("CullFace: front");
      label1.setFont(font);
      label1.setX(10);
      label1.setY(270);
      Text label2 = new Text("Material: colored");
      label2.setFont(font);
      label2.setX(180);
      label2.setY(270);
      Text label3 = new Text("Draw Mode: line");
      label3.setFont(font);
      label3.setX(370);
      label3.setY(270);
      //Setting the Scene
      Group root = new Group(cube, cylinder, sphere, label1, label2, label3);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      scene.setCamera(cam);
      stage.setTitle("JavaFX 3D Shape properties");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output


Updated on: 16-May-2020

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements