×

JavaFX Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight body. All methods needed for this purpose are present in the javafx.scene.shape.Cylinder class.

Properties and corresponding methods to create a Cylinder:

1. height

This property of the Cylinder class allows us to define the height of the Cylinder and the setHeight() method helps to set it.

2. radius

This property of the Cylinder class allows us to define the radius of the Cylinder and the setRadius() method helps to set it.

Constructors of Cylinder class in JavaFX:

1. Cylinder()

This is the simple constructor of the Cylinder class. It requires setters to set the value of height and radius.

2. Cylinder(double radius_value, double height_value)

This is the parameterized constructor of the Cylinder class and it allows us to pass values of height and radius in its constructor.

3. Cylinder( double radius_value, double height_value, int division_value)

This is the parameterized constructor of the Cylinder class and it allows us to pass values of height, divisions, and radius in its constructor.

JavaFX 3D Shape – Simple Cylinder on colored container

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.scene.shape.Cylinder;
import javafx.stage.Stage;


public class CylinderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      
    Cylinder newcyn = new Cylinder();  
         
    newcyn.setRadius(80);  
    newcyn.setHeight(200);  
    newcyn.setTranslateX(200);  
    newcyn.setTranslateY(150);  
     
        PerspectiveCamera newcamera = new PerspectiveCamera();  
        newcamera.setTranslateX(-50);  
        newcamera.setTranslateY(0);  
        newcamera.setTranslateZ(0); 
          


         Group box = new Group();  
         box.getChildren().addAll(newcyn);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400,Color.RED);  
         scene.setCamera(newcamera);
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 3D shapes - Cylinder Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Cylinder in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Cylinder, javafx.scene.paint.*, javafx.scene.PerspectiveCamera.

Then we have created one class named CylinderUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Cylinder, a Group object is created which is then passed to the Scene class object.

The Cylinder is created using the setters method to set the height and radius of the Cylinder. Also, the PerspectiveCamera object is created to view the 3D shape of the container.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 3D shapes - Cylinder Example”. Also, it displays a Cylinder with a defined height and radius.

JavaFX 3D Shape -Cylinder

JavaFX 3D Shape – Colored Cylinder on simple container

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.scene.shape.Cylinder;
import javafx.stage.Stage;


public class CylinderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      
    Cylinder newcyn = new Cylinder();  
         
    newcyn.setRadius(80);  
    newcyn.setHeight(200);  
    newcyn.setTranslateX(200);  
    newcyn.setTranslateY(150);  
     
        PerspectiveCamera newcamera = new PerspectiveCamera();  
        newcamera.setTranslateX(-50);  
        newcamera.setTranslateY(0);  
        newcamera.setTranslateZ(0); 
        PhongMaterial blueStuff = new PhongMaterial();
        blueStuff.setDiffuseColor(Color.BLUE);
        newcyn.setMaterial(blueStuff);


         Group box = new Group();  
         box.getChildren().addAll(newcyn);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         scene.setCamera(newcamera);
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX 3D shapes - Colored Cylinder Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the colored Cylinder on simple container in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Cylinder, javafx.scene.paint.*, javafx.scene.PerspectiveCamera, javafx.scene.paint.Color, javafx.scene.paint.PhongMaterial.

Then we have created one class named CylinderUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a colored Cylinder, a Group object is created which is then passed to the Scene class object.

The Cylinder is created using the setters method to set the height and radius of the Cylinder. Also, the PerspectiveCamera object is created to view the 3D shape of the container. The setMaterial() method is used to set the color shape for the Cylinder.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 3D shapes – Colored Cylinder Example”. Also, it displays a Colored Cylinder with a defined height and radius.

JavaFX 3D Shape -Cylinder

Related Topics

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX Light Spot

In the JavaFX application, in order to apply various Light Spot effect from a single source, we use this class. It has the ability to apply light from all the...

4 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Path Transition

In the JavaFX application, in order to generate the path transition, we have to apply path transition on any particular shape. It has the ability to traverse the given shape...

3 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 minutes read.

JavaFX Blend

In the JavaFX application, in order to apply various blend effect, we use Blend class. It has the ability to blend the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.