Open In App

JavaFX | Ellipse with examples

Last Updated : 31 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Ellipse class is a part of the JavaFX library. The ellipse class creates an ellipse on providing the center and X and Y radius. Ellipse class extends Shape class.

Constructors of the class are:

  1. Ellipse(): creates an empty instance of ellipse
  2. Ellipse(double X, double Y): creates a ellipse with the given x and y radius
  3. Ellipse(double x, double y, double X, double Y): creates a ellipse with given center and radius

Commonly used methods:

method explanation
getCenterX() returns the X coordinate of the center of ellipse
getCenterY() returns the Y coordinate of the center of ellipse
getRadiusX() returns the value of X Radius(along major axis)
getRadiusY() returns the value of Y Radius(along minor axis)
setCenterX(double v) sets the X coordinate of the center of ellipse
setCenterY(double v) sets the Y coordinate of the center of ellipse
setRadiusX(double v) returns the value of X Radius(along major axis)
setRadiusY(double v) returns the value of Y Radius(along minor axis)
setFill(Color c) sets the fill of the ellipse

Below programs will illustrate the use of ellipse class:

  1. Java program to create ellipse by passing the coordinates of the center and radius as arguments in constructor:

    This program creates a Ellipse indicated by the name ellipse( the coordinates of the center and the radius is passed as arguments). The Ellipse 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 ellipse is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.




    // Java program to create ellipse by passing the
    // coordinates of the center and radius as arguments in constructor
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    public class ellipse_0 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
            // set title for the stage
            stage.setTitle("creating ellipse");
      
            // create a ellipse
            Ellipse ellipse = new Ellipse(200.0f, 120.0f, 150.0f, 80.f);
      
            // create a Group
            Group group = new Group(ellipse);
      
            // 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:

  2. Java program to create ellipse by passing the coordinates of the center and radius using functions setCenterX(), setCenterY() etc.:

    This program creates a Ellipse indicated by the name ellipse.The coordinates of the center and the radius will be set using functions setCenterX(), setCenterY(), setRadiusX(), and setRadiusY() functions. The Ellipse 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 ellipse is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.




    // Java program to create ellipse by passing the
    // coordinates of the center and radius using
    // functions setCenterX(), setCenterY() etc.
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    public class ellipse_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
            // set title for the stage
            stage.setTitle("creating ellipse");
      
            // create a ellipse
            Ellipse ellipse = new Ellipse();
      
            // set center
            ellipse.setCenterX(150.0f);
            ellipse.setCenterY(120.0f);
      
            // set radius
            ellipse.setRadiusX(130.0f);
            ellipse.setRadiusY(100.0f);
      
            // create a Group
            Group group = new Group(ellipse);
      
            // 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:

  3. Java program to create ellipse by passing the coordinates of the center and radius using functions setCenterX(), setCenterY(), and set a fill using setFill() function:

    This program creates a Ellipse indicated by the name ellipse.The coordinates of the center and the radius will be set using functions setCenterX(), setCenterY(), setRadiusX(), and setRadiusY() functions.The function setFill() will be used to set the fill of the ellipse. The Ellipse 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 ellipse is attached.The group is attached to the scene. Finally, the show() method is called to display the final results.




    // Java program to create ellipse by passing the
    // coordinates of the center and radius using
    // functions setCenterX(), setCenterY(), and
    // set a fill using setFill() function
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.control.*;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    public class ellipse_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
            // set title for the stage
            stage.setTitle("creating ellipse");
      
            // create a ellipse
            Ellipse ellipse = new Ellipse();
      
            // set center
            ellipse.setCenterX(150.0f);
            ellipse.setCenterY(120.0f);
      
            // set radius
            ellipse.setRadiusX(130.0f);
            ellipse.setRadiusY(100.0f);
      
            // set fill
            ellipse.setFill(Color.BLUE);
      
            // create a Group
            Group group = new Group(ellipse);
      
            // 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:

  4. Note : The above programs might not run in an online IDE please use an offline compiler.
    Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Ellipse.html



    Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

Similar Reads