Implementation
So, we have seen the functionality of the sphere class. Now, let's see the actual implementation.
The below program creates a sphere with a radius of 50.0. After that, a group object is created to which our sphere is attached. Then the position of the sphere is changed using setTranslationX() and setTranslationsY() methods. Then we set the title for our application. After that, a new scene is created to which our group object is attached then, this scene is attached to the stage. Finally, we call the show method available on the stage to show the output.
-
Scene: This class acts as the container for all the scene graphs in our application.
-
Group: It is a container component which doesn't give its children any particular layout guidelines. Nodes that are child components are all positioned at 0.
- Stage: The most basic JavaFX container is the JavaFX Stage class.
Program
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
import javafx.scene.*;
public class Sphere_Example extends Application {
public void start(Stage stage) {
// Creating a sphere
Sphere sphere = new Sphere(50.0f);
// Creating a group
Group group = new Group(sphere);
// setting the position of sphere
sphere.setTranslateX(250);
sphere.setTranslateY(250);
// setting the title for our application
stage.setTitle("Sphere Example");
// creating a scene
Scene scene = new Scene(group, 500, 500);
// setting scene
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
// launch the application
launch(args);
}
}

You can also try this code with Online C++ Compiler
Run Code
Execution
To execute the above program. You can follow the given steps:
1. Open up the terminal in the same folder where you have saved your source code.
2. Enter the below code and hit enter.
javac --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
After you hit enter a new file will automatically be generated namely filename.class
3. Now, enter the below command and hit enter to execute the above program.
java --module-path path/to/your/javaFx/modules --add-modules javafx.fxml,javafx.controls filename.java
4. You have successfully executed the JavaFX program.

Output

Frequently Asked Questions
What are the three components of JavaFX?
Stage, Scene, and Nodes are the three main components of a JavaFX application.
What is a stage in JavaFX?
A stage in a JavaFX application is represented by the Stage class in the javafx.stage package. A Scene, which is made up of visual components, is hosted by a Stage in JavaFX. The start(Stage s)
method of the Application class receives the platform-created primary stage as a parameter.
What is a group JavaFX?
A group is a component that serves as a container for the children without applying any special structuring. Every child node or component, in this case, will remain at position 0,0.
Conclusion
In this article, we have extensively discussed the Sphere Class present inside the JavaFX Framework. We have seen different constructors to initialize the sphere object. Also, we have seen different methods available inside the class along with their implementation.
If you think this blog has helped you enhance your knowledge about JavaFx Sphere and if you would like to learn more, check out our articles JavaFX, JavaFX Arc, JavaFX Parallel Transition, JavaFX Fade Transition, JavaFX Sequential Transition, and many more on our Website.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Happy Learning!