JavaFX Circle
In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for this purpose are present in the javafx.scene.shape.Circle class.
Properties and corresponding methods to create Circle:
1. centerX
This property of the Circle class allows us to define the center point of Circle which is X co-ordinate and the setCenterX() method helps to set it.
2. centerY
This property of the Circle class allows us to define the center point of Circle which is Y co-ordinate and the setCenterY() method helps to set it.
3. radius
This property of the Circle class allows us to define the width of the Circle and the setRadius() method helps to set it.
JavaFX 2D Shape – Circle using Setters
Example:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
public class CircleUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
Group box = new Group();
Circle circle = new Circle();
circle.setCenterX(300);
circle.setCenterY(200);
circle.setRadius(150);
box.getChildren().addAll(circle);
Scene scene = new Scene(box,600,400);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX 2D shapes - Circle Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the Circle in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Circle.
Then we have created one class named CircleUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Circle, a Group object is created which is then passed to the Scene class object.
The circle is created using the setters method to set centerX, centerY, and radius of the circle.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Circle Example”. Also, it displays a circle with defined centerX, centerY, and radius.
JavaFX 2D Shape – Circle using Constructor
Example:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
public class CircleUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
Group box = new Group();
Circle circle = new Circle(200, 200, 120);
box.getChildren().addAll(circle);
Scene scene = new Scene(box,600,400);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX 2D shapes - Circle using constructor Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the Circle using constructor in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Circle.
Then we have created one class named CircleUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold Circle created with a constructor, a Group object is created which is then passed to the Scene class object.
The circle is created using the constructor with the required values passed in it.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 2D shapes – Circle using constructor Example”. Also, it displays circle as defined by constructor values.
JavaFX 2D Shape – Circle with color filled in it
Example:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class CircleUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception
{
Group box = new Group();
Circle circle = new Circle();
circle.setCenterX(300);
circle.setCenterY(200);
circle.setRadius(150);
circle.setFill(Color.BLUE);
box.getChildren().addAll(circle);
Scene scene = new Scene(box,600,400);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX 2D shapes - Circle Example");
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Output:
In order to create the Circle with color filled in it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Circle, javafx.scene.paint.Color.
Then we have created one class named CircleUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a color-filled Circle, a Group object is created which is then passed to the Scene class object.
The circle is created using the setters method to set centerX, centerY, and radius of the circle, and the setFill() method is used to set the color of the circle.
The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Circle Example". Also, it displays a circle with defined centerX, centerY, and radius with blue color filled in it.