JavaFX | Circle with examples - GeeksforGeeks

25 Oct, 2019
Circle class is a part of the JavaFX library. Circle class creates a circle with a specified x and y position for the center of the circle, the specified radius for the circle and a specified fill.The radius and the center of the circles are measured in pixels.
Constructors of the class are:
Circle(): creates a empty instance of circleCircle(double r): creates a circle with a specified radiusCircle(double X, double Y, double r): creates a circle with given X and y coordinates of the center of the circle, and the radius.Circle(double r, paint f) creates a circle with a specified radius and fillCircle(double X, double Y, double r, Paint f): creates a circle with given X and y coordinates of the center of the circle, and the radius and also a specified fill .
Circle(): creates a empty instance of circle
Circle(double r): creates a circle with a specified radius
Circle(double X, double Y, double r): creates a circle with given X and y coordinates of the center of the circle, and the radius.
Circle(double r, paint f) creates a circle with a specified radius and fill
Circle(double X, double Y, double r, Paint f): creates a circle with given X and y coordinates of the center of the circle, and the radius and also a specified fill .
Commonly used methods:
Below programs illustrate the use of the Circle class:
Java program to create a circle by passing the coordinates of the center and radius as arguments in constructor: This program creates a Circle indicated by the name circle( the coordinates of the center and the radius is passed as arguments). The Circle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.// Java program to create circle by passing the// coordinates of the center and radius// as arguments in constructorimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_0 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle(150.0f, 150.0f, 80.f);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Java program to create a circle and using the functions setCenterX, setCenterY and setRadius to set the coordinates of center and radius: This program creates a Circle indicated by the name circle. The coordinates for the center and the radius of the circle is set using setCenterX(), setCenterY(), and setRadius function. The Circle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.// Java program to create a circle and using // the functions setCenterX, setCenterY and setRadius// to set the coordinates of center and radiusimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_1 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle();          // set the position of center of the  circle        circle.setCenterX(100.0f);        circle.setCenterY(100.0f);          // set Radius of the circle        circle.setRadius(50.0f);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Java program to create a circle with specified radius and coordinates of center and also specified fill: This program creates a Circle indicated by the name circle.The coordinates for the center and the radius of the circle is set using setCenterX(), setCenterY(), and setRadius function. .The function set Fill() is used to set the fill of the circle The Circle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.// Java program to create a circle with specified// radius and coordinates of center and also specified fillimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_2 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle();          // set the position of center of the  circle        circle.setCenterX(100.0f);        circle.setCenterY(100.0f);          // set Radius of the circle        circle.setRadius(50.0f);          // set the fill of the circle        circle.setFill(Color.BLUE);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Note : The above programs might not run in an online IDE please use an offline compiler.Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/shape/Circle.htmlMy Personal Notes
arrow_drop_upSave
Java program to create a circle by passing the coordinates of the center and radius as arguments in constructor: This program creates a Circle indicated by the name circle( the coordinates of the center and the radius is passed as arguments). The Circle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.// Java program to create circle by passing the// coordinates of the center and radius// as arguments in constructorimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_0 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle(150.0f, 150.0f, 80.f);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
// Java program to create circle by passing the// coordinates of the center and radius// as arguments in constructorimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_0 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle(150.0f, 150.0f, 80.f);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Java program to create a circle and using the functions setCenterX, setCenterY and setRadius to set the coordinates of center and radius: This program creates a Circle indicated by the name circle. The coordinates for the center and the radius of the circle is set using setCenterX(), setCenterY(), and setRadius function. The Circle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.// Java program to create a circle and using // the functions setCenterX, setCenterY and setRadius// to set the coordinates of center and radiusimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_1 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle();          // set the position of center of the  circle        circle.setCenterX(100.0f);        circle.setCenterY(100.0f);          // set Radius of the circle        circle.setRadius(50.0f);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:
// Java program to create a circle and using // the functions setCenterX, setCenterY and setRadius// to set the coordinates of center and radiusimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_1 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle();          // set the position of center of the  circle        circle.setCenterX(100.0f);        circle.setCenterY(100.0f);          // set Radius of the circle        circle.setRadius(50.0f);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Java program to create a circle with specified radius and coordinates of center and also specified fill: This program creates a Circle indicated by the name circle.The coordinates for the center and the radius of the circle is set using setCenterX(), setCenterY(), and setRadius function. .The function set Fill() is used to set the fill of the circle The Circle will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the circle is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.// Java program to create a circle with specified// radius and coordinates of center and also specified fillimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_2 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle();          // set the position of center of the  circle        circle.setCenterX(100.0f);        circle.setCenterY(100.0f);          // set Radius of the circle        circle.setRadius(50.0f);          // set the fill of the circle        circle.setFill(Color.BLUE);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}Output:Note : The above programs might not run in an online IDE please use an offline compiler.Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/shape/Circle.htmlMy Personal Notes
arrow_drop_upSave
// Java program to create a circle with specified// radius and coordinates of center and also specified fillimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.*;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.control.*;import javafx.stage.Stage;  import javafx.scene.Group;public class circle_2 extends Application {      // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating circle");          // create a circle        Circle circle = new Circle();          // set the position of center of the  circle        circle.setCenterX(100.0f);        circle.setCenterY(100.0f);          // set Radius of the circle        circle.setRadius(50.0f);          // set the fill of the circle        circle.setFill(Color.BLUE);          // create a Group        Group group = new Group(circle);          // create a scene        Scene scene = new Scene(group, 500, 300);          // set the scene        stage.setScene(scene);          stage.show();    }      public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:
Note : The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/shape/Circle.html
ManasChhabra2
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
Stack Class in Java
Stream In Java
Singleton Class in Java
Set in Java