×

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique shape. All methods needed for this purpose are present in the javafx.scene.shape.Ellipse class. Each time Ellipse is drawn using 2 points called the focus. Any random point on Eclipse has the same sum of the distance to the focus point.

Each Ellipse has,

1. Center:

This is the middle point of the segment joining two points on the Ellipse. The major and minor axis intersects at this point of the Ellipse.

2. Major axis:

In the Ellipse, the major axis is the longest diameter of that Ellipse.

3. Minor axis:

In the Ellipse, the minor axis is the shortest diameter of that Ellipse.

Constructors of Ellipse class:

Ellipse():

This constructor is used to create an empty constructor.

Ellipse(double x, double y):

This constructor is used to create Ellipse with a given x and y radius.

Ellipse(double x, double y, double radiusX, double radiusY):

This constructor is used to create Ellipse with given x and y co-ordinates for center and radius width and height.

Methods of Ellipse class:

  getCenterX()This method of Ellipse is used to get the X coordinate of the center of the ellipse.  
  getCenterY()This method of Ellipse is used to get the Y coordinate of the center of the ellipse.  
  getRadiusX()This method of Ellipse is used to get the width of the radius of the ellipse.  
  getRadiusY()This method of Ellipse is used to get the height of the radius of the ellipse.
  setCenterX(double x)This method of Ellipse is used to set the X coordinate of the center of the ellipse.  
  setCenterY(double y)This method of Ellipse is used to set the Y coordinate of the center of the ellipse.
  setRadiusX(double x)This method of Ellipse is used to set the width of the radius of the ellipse.
  setRadiusY(double y)This method of Ellipse is used to set the height of the radius of the ellipse.
  setFill(Color newcolor)This method of Ellipse is used to set the color to be filled in the ellipse.

Properties and corresponding methods to create Ellipse:

1. centerX

This property of the Ellipse class allows us to define the center point of Ellipse which is X co-ordinate and the setCenterX() method helps to set it.

2. centerY

This property of the Ellipse class allows us to define the center point of Ellipse which is Y co-ordinate and the setCenterY() method helps to set it.

3. radiusX

This property of the Ellipse class allows us to define the width of the Ellipse and the setRadiusX() method helps to set it.

4. radiusY

This property of the Ellipse class allows us to define the height of the Ellipse and the setRadiusY() method helps to set it.

Steps to draw Ellipse in JavaFX:

1. Import all the required Libraries:

In order to create Ellipse in JavaFX application, we have to import all the required libraries such as, javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.paint.Color, javafx.scene.shape.Ellipse.

2. Create one class extending the Application class:

We have to create one class with any name extending the Application class. Also, we have to override the start method to provide implementation details. This method is used to instantiate an object of Stage as primaryStage.

3. Create Ellipse Empty constructor:

Then in the start() method in order to create Ellipse, we have to create an empty constructor as,

Ellipse newEllipse=new Ellipse();

4. Set various properties of Ellipse:

After creating empty Ellipse constructor, we can set centerX, centerY, radiusX and radiusY properties of Ellipse using setCenterX(), setCenterY(), setRadiusX() and setRadiusY() methods.

5. Create Group Object:

For creating a container to hold an Ellipse, a Group object is created which is then passed to the Scene class object.

6. Create Scene Object:

Scene class object is created and the size of container with elements to include are passed in the constructor.

7. Prepare the stage:

Scene class object is passed in setScene() method called with primaryStage object. The tile of the stage is set using the setTitle(“ Title ”) method and the show() method is called to display output.

8. Launch the application:

In order to run the application, the launch( args ) method is called in the main() method which will launch an application and execute logic inside the start() method.

JavaFX 2D Shape -Ellipse using setters

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.Ellipse;


  
public class EllipseUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Ellipse newellipse = new Ellipse();
      
    newellipse.setCenterX(180);
    newellipse.setCenterY(150);
      newellipse.setRadiusX(120);
    newellipse.setRadiusY(100);
   
         Group box = new Group();  
         box.getChildren().addAll(newellipse);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX 2D shapes - Ellipse Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Ellipse 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.paint.Color, javafx.scene.shape.Ellipse.

Then we have created one class named EllipseUI 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 an Ellipse, a Group object is created which is then passed to the Scene class object.

Ellipse is created with the help of empty constructor and X, Y, Width and Height is set using setters such as setCenterX(), setCenterY(), setRadiusX() and setRadiusY().

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 called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Ellipse Example”. Also, it displays Ellipse with defined x, y coordinates, and width, height.

JavaFX 2D Shape -Ellipse

JavaFX 2D Shape -Ellipse with Constructor

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.Ellipse;


  
public class EllipseUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
        Ellipse newellipse = new Ellipse(240, 160, 120, 120);


         Group box = new Group();  
         box.getChildren().addAll(newellipse);


         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX 2D shapes - Ellipse Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Ellipse with values in 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.paint.Color, javafx.scene.shape.Ellipse.

Then we have created one class named EllipseUI 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 an Ellipse with a constructor, a Group object is created which is then passed to the Scene class object.

Ellipse is created with the help of the constructor having centerX, centerY, radiusX, and radiusY passed as an argument.

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 - Ellipse Example”. Also, it displays Ellipse as mentioned by the user in the constructor.

JavaFX 2D Shape -Ellipse

JavaFX 2D Shape -Ellipse with setFill() method

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.Ellipse;


  
public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Ellipse newellipse = new Ellipse();
      
    newellipse.setCenterX(180);
    newellipse.setCenterY(150);
      newellipse.setRadiusX(120);
    newellipse.setRadiusY(100);
    newellipse.setFill(Color.RED);

         Group box = new Group();  
         box.getChildren().addAll(newellipse);
         box.getChildren().addAll();      
         Scene scene = new Scene(box,600,400);  
         primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX 2D shapes - Ellipse Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Ellipse with specified color 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.paint.Color, javafx.scene.shape.Ellipse.

Then we have created one class named EllipseUI 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 an Ellipse with the specified color, a Group object is created which is then passed to the Scene class object.

Ellipse is created with the help of empty constructor and X, Y, Width and Height is set using setters such as setCenterX(), setCenterY(), setRadiusX() and setRadiusY(). Color of the Ellipse is set using setColor() method.

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 - Ellipse Example”. Also, it displays Ellipse having red color filled in it.

JavaFX 2D Shape -Ellipse

Related Topics

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Pie Chart

In the JavaFX application, in order to create Pie Chart, the PieChart class is used. The Pie Chart allows us to plot data along with the sectors of the circle....

3 minutes read.

JavaFX Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Rotation

In the JavaFX application, in order to apply various rotation effects, we use this class. It has the ability to rotate the given object. It rotates the given node along...

3 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 minutes read.

JavaFX Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.