×

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 TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 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 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 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 Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

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 Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

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 Rotate Transition

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

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Translation

In the JavaFX application, in order to apply various Translate effect, we use this class. It has the ability to Translate the given object according to the specified parameter. All...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

4 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

4 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.