JavaFX – Creating a Cylinder


JavaFX – Creating a Cylinder


”;


A cylinder is a closed solid that has two parallel (mostly circular) bases connected by a curved surface. To visualize, you can think of a 3D cylinder as a clutter of 2D circles that are stacked on top of each other to a certain height; hence, making it a three-dimensional shape even though it is described by two parameters.

The parameters of a cylinder are – the radius of its circular base and the height of the cylinder as shown in the following diagram −

Cylinder

Cylinder in JavaFX

In JavaFX, a cylinder is represented by a class named Cylinder. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a cylinder node in JavaFX.

This class has 2 properties of the double datatype namely −

  • height − The height of the Cylinder.

  • radius − The radius of the Cylinder.

To draw a cylinder, you need to pass values to these properties by passing them to the constructor of this class. This can be done in the same order at the time of instantiation of the Cylinder class; Or, by using their respective setter methods.

Steps to Draw 3D Cylinder

To Draw a Cylinder (3D) in JavaFX, follow the steps given below.

Step 1: Creating a Class

Create a Cylinder object in JavaFX by instantiating the class named Cylinder, which belongs to a package javafx.scene.shape. You can instantiate this class in the start() method as follows −

public class ClassName extends Application {  
   @Override     
   public void start(Stage primaryStage) throws Exception {
      //Creating an object of the Cylinder class       
      Cylinder cylinder = new Cylinder();   
   }    
}

Step 2: Setting Properties to the Cylinder

Set the height and radius of the Cylinder using their respective setter as shown below.

//Setting the properties of the Cylinder 
cylinder.setHeight(300.0f); 
cylinder.setRadius(100.0f); 

Step 3: Creating a Group Object

Now, create a group object by instantiating the class named Group, which belongs to the package javafx.scene. Then, pass the Cylinder (node) object created in the previous step as a parameter to the constructor of the Group class. This should be done in order to add it to the group as follows −

Group root = new Group(cylinder);

Step 4: Launching an Application

Once the 3D object is created, launch the JavaFX application by following the steps below −

  • Instantiate the class named Scene by passing the Group object as a parameter value to its constructor. You can also pass dimensions of the application screen as optional parameters to the constructor.

  • Set the title to the stage using the setTitle() method of the Stage class.

  • Add a scene object to the stage using the setScene() method of the class named Stage.

  • Display the contents of the scene using the method named show().

  • Lastly, the application is launched with the help of the launch() method within the Application class.

Example

The following program shows how to generate a Cylinder using JavaFX. Save this code in a file with the name 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); 
   } 
}

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls CylinderExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls CylinderExample

Output

On executing, the above program generates a JavaFX window displaying a Cylinder as shown below.

Drawing 3dCylinder

Example

You can also apply transformations on the 3D shape. In this example, we are trying to apply translate transformation on the 3D cylinder and relocate it on the application. Save this code in the file named 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); 
   } 
}

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateCylinderExample.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls TranslateCylinderExample

Output

On executing, the above program generates a JavaFX window displaying a Cylinder as shown below.

Drawing 3dCylinder

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *