×

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 Bloom

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

4 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

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 Translate Transition

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

3 minutes read.

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

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 Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

4 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 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 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.