Updated March 23, 2023
Introduction to JavaFX Circle
JavaFX package consists of different shapes such as line, rectangle, ellipse circle, etc. that can be applied in several applications. Among this, a circle is created with the help of the Circle class. The x,y coordinate where the center of the circle, radius, and fill will be passed to the circle during initialization.
The JavaFX Circle is instantiated from the class javafx.scene.shape.Circle. The syntax, constructors, properties, methods, and example of JavaFX Circle will be explained in the below sections.
Syntax:
Circle circle = new Circle(x, y, r);
Here, x and y will be the coordinate of the center of the circle whereas r will be the radius.
Constructors
Following are the five constructors for JavaFX Circle Constructors.
- Circle (): An empty circle instance will be created.
- Circle (double r): A circle instance will be created with the given radius.
- Circle (double x, double y, double r): A circle instance will be created with the given coordinate and radius.
- Circle (double x, double y, double r, paint fill): A circle instance will be created with the given coordinate, fill and radius.
- Circle (double r, paint fill): A circle instance will be created with the given radius r and fill.
Properties
Here is the properties of JavaFX circle which are explained below:
- radius(): Radius will be defined (in pixels) for the Circle.
- centerX(): A horizontal position will be defined (in pixels) for the Circle center.
- centerY(): Vertical position will be defined (in pixels) for the Circle center.
Methods
Now, let us see some of the commonly used methods in JavaFX Circle.
- getRadius(): Radius property’s value will be returned.
- setRadius(double value): Radius property’s value will be set.
- getCenterY(): CenterY property’s value will be returned.
- setCenterX(double value): CenterX property’s value will be set.
- getCenterX(): CenterX property’s value will be set.
- setCenterY(double value): CenterY property’s value will be set.
- toString(): String representation will be returned for the object of Circle.
How to Create a JavaFX Circle?
JavaFX Circle can be created using the below steps.
Step 1: Create a sample class that extends from the application base class.
public class JavaFXCircleExample extends Application {
}
Inside the class, define the start() method that instructs the steps for displaying the result.
public void start(Stage s) {
}
Step 2: Create a circle. Instantiate the class javafx.scene.shape.Circle. Following is the syntax that is used for Circle.
//create object of Circle
Circle c = new Circle();
Step 3: Set properties such as x, y, and radius of the circle. X, y and r can be either passed directly to the constructor or divide into different steps. If different functions are used, pass the value for x, y, and r as depicted below.
c.setCenterX(350.0f);
c.setCenterY(143.0f);
c.setRadius(50.0f);
If the constructor is used to pass the values, then the syntax will be as shown below.
// creation of circle by passing x, y, r as parameters
Circle c = new Circle(125.0f, 125.0f, 90.f);
Step 4: Create a group object. Once the circle is created, create a group and pass the object of a circle to it.
//create a group g
Group g = new Group(c);
Step 5: Similarly, create a scene and pass the object of the group to it.
Scene sc = new Scene(g, 300, 200)
Step 6: Set the title for the stage created. The title of the stage can be created immediately after the stage creation or in this step.
//set title
s.setTitle("JavaFX Circle Example");
Step 7: Add the scene created to the stage. After the above steps, the method setScene() is used in order to add the scene.
s.setScene(sc);
Step 8: Display the results and launch the application. Display the result by calling the function show() at the end.
s.show();
Once all these steps are done, launch the application with the help of launch(args).
Examples to Implement JavaFX Circle
Now let us see some of the JavaFX programs that implement JavaFX Circle.
Example #1
Java Program to create a circle with the parameterized constructor.
Code:
//Java program for circle creation with parameterized constructor
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.Group;
//class that extends application base class
public class JavaFXCircleExample extends Application {
// application starts at this point
public void start(Stage s)
{
// set title
s.setTitle("Circle Example");
// creation of circle by passing x, y, r as parameters
Circle c = new Circle(125.0f, 125.0f, 90.f);
// creation of Group
Group gp = new Group(c);
// creation of scene
Scene sc = new Scene(gp, 550, 350);
// set the scene
s.setScene(sc);
//display the results
s.show();
}
//main method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
On executing the code, a circle will be created with 90 as the radius. The x,y coordinates for the center and radius is passed as parameters of the constructor of circle.
Example #2
Java program to create a circle with a non-parameterized constructor.
Code:
//Java program for circle creation with non-parameterised constructor
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.Group;
//class that extends application base class
public class JavaFXCircleExample extends Application {
// application starts at this point
public void start(Stage s)
{
// set title
s.setTitle("Circle Example");
// creation of circle
Circle c = new Circle();
// set the X coordinate of center of the circle
c.setCenterX(125.0f);
c.setCenterY(125.0f);
// set the radius of circle
c.setRadius(90.0f);
// creation of Group
Group gp = new Group(c);
// creation of scene
Scene sc = new Scene(gp, 550, 350);
// set the scene
s.setScene(sc);
//display the results
s.show();
}
//main method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
In this program also, a circle is displayed on executing the code. The only difference is that x,y coordinates for the center and radius is set using the methods setCenterX(), setCenterY(), and Radius(). Even if the way of passing these values are different, the output is still the same.
Recommended Articles
This is a guide to JavaFX Circle. Here we discuss how to create a JavaFX Circle along with its properties, methods, constructors, and examples with code implementation. You may also look at the following articles to learn more-