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 for this purpose are present in the javafx.scene.shape.Sphere class.
Properties and corresponding methods to create Sphere:
1. radius
This property of the Sphere class allows us to define the radius of Sphere and the setRadius() method helps to set it.
Constructors of Sphere class in JavaFX:
1. Sphere()
This is the simple constructor of the Sphere class. It creates a sphere with a default 1.0 radius.
2. Sphere(double radius_value)
This is the parameterized constructor of the Sphere class. It creates a sphere with a specified radius.
3. Sphere(double radius_value, int division_value)
This is the parameterized constructor of the Sphere class. It creates a sphere with a specified radius and divisions.
JavaFX 3D Shape – Simple Sphere 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.shape.CullFace;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class SphereUI extends Application {
@Override
public void start( Stage primaryStage ) throws Exception
{
Sphere newsphere = new Sphere();
newsphere.setRadius(100);
newsphere.setTranslateX(200);
newsphere.setTranslateY(150);
newsphere.setCullFace( CullFace.BACK );
PerspectiveCamera newcamera = new PerspectiveCamera();
newcamera.setTranslateX(-50);
newcamera.setTranslateY(0);
newcamera.setTranslateZ(0);
Group box = new Group();
box.getChildren().addAll (newsphere);
box.getChildren().addAll();
Scene scene = new Scene( box, 600, 400, Color.RED);
scene.setCamera (newcamera);
primaryStage.setScene ( scene );
primaryStage.setTitle(" JavaFX 3D shapes - Sphere Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the Sphere 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.Sphere, javafx.scene.paint.*, javafx.scene.PerspectiveCamera, javafx.scene.shape.CullFace.
Then we have created one class named SphereUI 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 Sphere, a Group object is created which is then passed to the Scene class object.
The Sphere is created using the setters method to set the radius of the Sphere. 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 - Sphere Example”. Also, it displays a Sphere with a defined radius.
JavaFX 3D Shape – Colored Sphere 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.CullFace;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class SphereUI extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Sphere newsphere = new Sphere();
newsphere.setRadius(100);
newsphere.setTranslateX(200);
newsphere.setTranslateY(150);
newsphere.setCullFace(CullFace.BACK);
PerspectiveCamera newcamera = new PerspectiveCamera();
newcamera.setTranslateX(-50);
newcamera.setTranslateY(0);
newcamera.setTranslateZ(0);
PhongMaterial blueStuff = new PhongMaterial();
blueStuff.setDiffuseColor(Color.BLUE);
newsphere.setMaterial(blueStuff);
Group box = new Group();
box.getChildren().addAll(newsphere);
box.getChildren().addAll();
Scene scene = new Scene(box,600,400);
scene.setCamera(newcamera);
primaryStage.setScene(scene);
primaryStage.setTitle(" JavaFX 3D shapes - Colored Sphere Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the colored Sphere 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.Sphere, javafx.scene.paint.*, javafx.scene.PerspectiveCamera, javafx.scene.paint.Color, javafx.scene.paint.PhongMaterial, javafx.scene.shape.CullFace .
Then we have created one class named SphereUI 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 Sphere on the simple container, a Group object is created which is then passed to the Scene class object.
The Sphere is created using the setters method to set the radius of the Sphere. 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 Sphere. The setCullFace() method is used to set shades for Sphere.
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 Sphere Example”. Also, it displays a Colored Sphere on the simple container with a defined radius.