What are various 3D shapes provided by JavaFX?


In general, a 3D shape is a geometrical figure that can be drawn on the XYZ plane. These include a Cylinder, Sphere, and a Box.

The javafx.scene.shape.Shape3D package provides various classes each of them represents/defines a 3d geometrical object or, an operation on them. The class named Shape3D is the base class of all the 3-Dimensional shapes in JavaFX.

Following are various geometrical shapes that you can draw using JavaFX −

  • Cylinder − A cylinder is a closed solid that has two parallel (mostly circular) bases connected by a curved surface.

  • Sphere − A sphere is defined as the set of points that are all at the same distance r from a given point in a 3D space. This distance r is the radius of the sphere and the given point is the center of the sphere.

  • Box − A cuboid is a three-dimensional shape with a length (depth), width, and height.

To create a required shape, you need to −

  • Instantiate the respective class.

  • Set its properties.

  • Add the created object to Group.

Example

Following JavaFX Example demonstrates the creation of all the available 3D shapes −

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.Cylinder;
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 JavaFX3DShapes extends Application {
   public void start(Stage stage) {
      //Drawing a Box
      Box cube = new Box(100, 120, 100);
      //Setting color and translating the box
      PhongMaterial material = new PhongMaterial();
      material.setDiffuseColor(Color.DARKRED);
      cube.setMaterial(material);
      cube.setTranslateX(30.0);
      cube.setTranslateY(180.0);
      cube.setTranslateZ(150.0);
      //Drawing a Cylinder
      Cylinder cylinder = new Cylinder(50, 150);
      //Setting the properties of the cylinder
      cylinder.setMaterial(material);
      cylinder.setTranslateX(250.0);
      cylinder.setTranslateY(180.0);
      cylinder.setTranslateZ(150.0);
      //Drawing a Sphere
      Sphere sphere = new Sphere(75);
      //Setting the properties of the sphere
      sphere.setMaterial(material);
      sphere.setTranslateX(480.0);
      sphere.setTranslateY(180.0);
      sphere.setTranslateZ(150.0);
      //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("Box");
      label1.setFont(font);
      label1.setX(50);
      label1.setY(270);
      Text label2 = new Text("Cylinder");
      label2.setFont(font);
      label2.setX(210);
      label2.setY(270);
      Text label3 = new Text("Sphere");
      label3.setFont(font);
      label3.setX(400);
      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("Drawing 3D Shapes");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 16-May-2020

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements