Cylinder is a part of JavaFX . Cylinder class is used to create a 3Dimensional cylinder of specified height and radius. The Cylinder is centered at origin.
The constructor for the class are
- Cylinder():Creates a new instance of Cylinder of radius of 1.0 and height of 2.0.
- Cylinder(double r, double h):Creates a new instance of Cylinder of a given radius and height.
- Cylinder(double r, double h, int div):Creates a new instance of Cylinder of a given radius and height and divisions
Commonly used methods
method | explanation |
---|---|
getHeight() | returns the height of the cylinder |
getRadius() | returns the radius of the base of cylinder |
setHeight(double v) | sets the height of the cylinder |
setRadius(double v) | sets the radius of the cylinder |
Java program to create a cylinder and add it to the stage
This program creates a Cylinder indicated by the name cylinder( the height, and radius is passed as arguments). The Cylinder will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the cylinder is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.
// Java program to create a cylinder // and add it to the stage import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Cylinder;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
public class cylinder_0 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle( "creating cylinder" );
// create a cylinder
Cylinder cylinder = new Cylinder( 20 .0f, 120 .0f);
// create a Group
Group group = new Group(cylinder);
// translate the cylinder to a position
cylinder.setTranslateX( 100 );
cylinder.setTranslateY( 100 );
// create a scene
Scene scene = new Scene(group, 500 , 300 );
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
} |
Output:
Java program to create a cylinder and add a perspective camera to it
This program creates a Cylinder indicated by the name cylinder( the height and radius is passed as arguments). The Cylinder will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the cylinder is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.A perspective camera will be created and added to the scene to render the cylinder in 3D.
// Java program to create a cylinder and add a perspective camera to it . import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Cylinder;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class cylinder_1 extends Application {
// launch the application
public void start(Stage stage)
{
// set title for the stage
stage.setTitle( "creating cylinder" );
// create a cylinder
Cylinder cylinder = new Cylinder( 20 .0f, 120 .0f);
// create a Group
Group group = new Group(cylinder);
// translate the cylinder to a position
cylinder.setTranslateX( 100 );
cylinder.setTranslateY( 100 );
// create a perspective camera
PerspectiveCamera camera = new PerspectiveCamera( false );
camera.setTranslateX( 0 );
camera.setTranslateY( 0 );
camera.setTranslateZ( 0 );
// create a scene
Scene scene = new Scene(group, 500 , 300 );
scene.setCamera(camera);
// set the scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
} |
output:
Note : The above programs might not run in an online IDE please use an offline compiler
Reference:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Cylinder.html