Open In App

JavaFX | Sphere with examples

Last Updated : 22 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Sphere is a part of JavaFX. Sphere class is used to create a 3 dimensional sphere with a specified radius . The sphere is centered at origin. 
Sphere class inherits Shape3D class. 
Constructor for the class are 
 

  1. Sphere(): creates a new sphere with radius 1.0
  2. Sphere(double r): creates a new sphere with a given radius
  3. Sphere(double r, int div): creates a new sphere with a given radius and number of divisions


Commonly used methods 
 

methodexplanation
getDivisions()returns the number of divisions of the sphere
getRadius()returns the radius of sphere
setRadius(double r)sets the radius of the sphere to specified value


The following programs will illustrate the use of Sphere class 
 


Java program to create sphere by passing the radius as arguments in constructor 
 


This program creates a Sphere indicated by the name sphere( radius is passed as arguments). The Sphere 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 sphere is attached.The group is attached to the scene. Finally, the show() method is called to display the final results. 
 

Java
// Java program to create sphere by passing the radius
// as arguments in constructor
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.Sphere;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
public class sphere_0 extends Application {

    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating sphere");

        // create a sphere
        Sphere sphere = new Sphere(80.0f);

        // create a Group
        Group group = new Group(sphere);

        // translate the sphere to a position
        sphere.setTranslateX(100);
        sphere.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 Sphere and add a perspective camera to render the 3D object 
 


This program creates a Sphere indicated by the name sphere( radius is passed as arguments). The Sphere 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 sphere 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
// Java program to create a Sphere and add a perspective camera to render the 3D object
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.Sphere;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
public class sphere_1 extends Application {

    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating sphere");

        // create a sphere
        Sphere sphere = new Sphere(80.0f);

        // create a Group
        Group group = new Group(sphere);

        // translate the sphere to a position
        sphere.setTranslateX(100);
        sphere.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/Sphere.html
 


Next Article
Article Tags :
Practice Tags :

Similar Reads