Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
An exclusive variety of ellipses with both focal points in the same location is a circle. Its vertical radius is the same as its horizontal radius. JavaFX enables us to instantiate javafx.scene.shape.Circle class to build a Circle on the GUI platform. This blog describes JavaFX Circle in detail, including its constructors, methods, and programs for drawing Circle.
Without further ado, let's start by learning the constructors of the JavaFX Circle Class.
Constructors
The five JavaFX Circle Constructors constructors are listed below.
Circle (): A new instance of a blank circle will be produced.
Circle (double r): A circle instance with the specified radius will be created.
Circle (double x, double y, double r): The specified radius and coordinates will be used to build a circle instance.
Circle (double x, double y, double r, paint fill): An instance of a circle with the specified radius, fill, and coordinates will be produced.
Circle (double r, paint fill): A circle instance with the specified radius r and fill will be produced.
Let's have a look at the properties of the JavaFX Circle class.
Properties
This class has three properties:
centerX: The centerX value represents the Circle's x-coordinate.
centerY: The centerY value represents the Circle's y-coordinate.
radius: The Circle's circumference in pixels.
Let's have a look at the methods of JavaFX Circle.
Methods of JavaFX Circle
The different methods used for creating a JavaFX Circle are as follows:
Let's examine how to create a Circle using a program.
Program to create a Circle using Constructor
The following code snippet describes how to create a JavaFX circle using a constructor.
Code:
//importing packages
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.control.*;
import javafx.scene.Group;
public class Draw_circle extends Application {
public void start(Stage stage)
{
stage.setTitle("Draw Circle"); //Title
Circle circle = new Circle(80.0f, 80f, 30.f);//Creating Circle using constructors
Group group_1 = new Group(circle);// Creating Group
// Creating Scene
Scene scene = new Scene(group_1, 400, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
launch(args);
}
}
You can also try this code with Online Java Compiler
In the above program, a Circle is created using the coordinates of the center, and the radius is passed as arguments to the constructor. The Circle will be created within a scene that is hosted inside a stage. To give the stage a title, we use the setTitle() function. The Circle is then added to the newly established Group. The group is involved in the scene. The show() method is finally used to display the outcome.
Program to create a Circle using Methods of JavaFx Circle
The following code snippet describes how to create a JavaFX circle using methods.
Code:
//importing packages
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.control.*;
import javafx.scene.Group;
public class Draw_circle extends Application {
public void start(Stage stage)
{
stage.setTitle("Draw Circle"); //Title
Circle circle = new Circle();//Creating Circle
//Coordinates of Center of Circle
circle.setCenterX(80.0f);
circle.setCenterY(80.0f);
circle.setRadius(30.0f);//Radius of Circle
Group group_1 = new Group(circle);// Creating Group
// Creating Scene
Scene scene = new Scene(group_1, 400, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
launch(args);
}
}
You can also try this code with Online Java Compiler
In the above program, the Circle is created using the setCenterX(), setCenterY(), and setRadius() functions, defining the coordinates for the Circle's center and radius. The Circle will be created within a scene that is hosted inside a stage. To give the stage a title, we use the setTitle() function. The Circle is then added to the newly established Group. The group is involved in the scene. The show() method is finally used to display the outcome.
Program to create a Circle using Methods of JavaFx Circle and set its fill
The following code snippet describes how to create a JavaFX Circle and define its fill.
Code:
//importing packages
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.control.*;
import javafx.scene.Group;
public class Draw_circle extends Application {
public void start(Stage stage)
{
stage.setTitle("Draw Circle"); //Title
Circle circle = new Circle();//Creating Circle
//Coordinates of Center of Circle
circle.setCenterX(80.0f);
circle.setCenterY(80.0f);
circle.setRadius(30.0f);//Radius of Circle
circle.setFill(Color.GREEN);//Fill(Color) of the circle
Group group_1 = new Group(circle);// Creating Group
// Creating Scene
Scene scene = new Scene(group_1, 400, 200);
stage.setScene(scene);
stage.show();
}
public static void main(String args[])
{
launch(args);
}
}
You can also try this code with Online Java Compiler
In the above program, the Circle is created using the setCenterX(), setCenterY(), and setRadius() functions, defining the coordinates for the Circle's center and radius. The setFill() is used to set the color of the Circle. The Circle will be created within a scene that is hosted inside a stage. To give the stage a title, we use the setTitle() function. The Circle is then added to the newly established Group. The group is involved in the scene. The show() method is finally used to display the outcome.
Frequently Asked Questions
What is a scene in JavaFX?
A scene is a representation of a JavaFX application's actual contents. It includes all of a scene graph's elements.
Which method is used to define the center of the Circle?
The setCenterX() and setCenterY() are used to define the X-coordinate and Y-coordinate of the center of the Circle.
What is a stage in JavaFX?
In JavaFX, a Stage is a top-level container that hosts a Scene. It consists of visual components.
Conclusion
In this article, we have extensively discussed the details of the JavaFX Circle along with its constructors, methods, and different programs for drawing Circle.