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