Table of contents
1.
Introduction
2.
Sphere Class
2.1.
Constructors
2.2.
Available Methods
3.
Implementation
3.1.
Program
4.
Frequently Asked Questions
4.1.
What are the three components of JavaFX?
4.2.
What is a stage in JavaFX?
4.3.
What is a group JavaFX?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX Sphere

Author Harsh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaFX frameworks have a variety of classes that can be used to create 3D shapes. The sphere is one of them. A Sphere is a three-dimensional object. The primary distinction between a circle and a sphere is that the sphere is described by its three axes, namely the x, y, and z axes.

In this article, we will discuss the sphere class present inside the JavaFX framework. We will also look at some of the commonly used methods present in the class. Lastly, we will implement the program in which we will create a 3d sphere with a radius.

javafx logo

Sphere Class

Sphere is a part of the JavaFX framework and is present inside the javafx.scene.shape package. Sphere class is used to create a 3d sphere inside our javafx application.

Now let’s see the different constructors that are available in sphere class.

Constructors

There are 3 different types of constructors available in sphere class.

  1. Sphere(): Creates a new sphere with radius 1.0.
     
  2. Sphere(double radius): Creates a sphere with the given radius.}
     
  3. Sphere(double radius, int divisions): Creates a new sphere with the specified radius and number of divisions.
     

Available Methods

Available methods table

 

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.

command to run javafx program

Output

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 JavaFXJavaFX ArcJavaFX Parallel TransitionJavaFX Fade TransitionJavaFX Sequential Transition, and many more on our Website.
 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem 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 problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass