JavaFX | Ellipse with examples
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:
- Ellipse(): creates an empty instance of ellipse
- Ellipse(double X, double Y): creates a ellipse with the given x and y radius
- 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:
- 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 constructorimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.shape.Ellipse;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;publicclassellipse_0extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating ellipse");// create a ellipseEllipse ellipse =newEllipse(200.0f,120.0f,150.0f,80.f);// create a GroupGroup group =newGroup(ellipse);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- 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.importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.shape.Ellipse;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;publicclassellipse_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating ellipse");// create a ellipseEllipse ellipse =newEllipse();// set centerellipse.setCenterX(150.0f);ellipse.setCenterY(120.0f);// set radiusellipse.setRadiusX(130.0f);ellipse.setRadiusY(100.0f);// create a GroupGroup group =newGroup(ellipse);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- 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() functionimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.shape.Ellipse;importjavafx.scene.control.*;importjavafx.scene.paint.Color;importjavafx.stage.Stage;importjavafx.scene.Group;publicclassellipse_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating ellipse");// create a ellipseEllipse ellipse =newEllipse();// set centerellipse.setCenterX(150.0f);ellipse.setCenterY(120.0f);// set radiusellipse.setRadiusX(130.0f);ellipse.setRadiusY(100.0f);// set fillellipse.setFill(Color.BLUE);// create a GroupGroup group =newGroup(ellipse);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:
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
Recommended Posts:
- JavaFX | Arc with examples
- JavaFX | Box with examples
- JavaFX | ContextMenu with examples
- JavaFX | Cylinder with examples
- JavaFX | Polygon with examples
- JavaFX | Polyline with examples
- JavaFX | CheckMenuItem with examples
- JavaFX | Circle with examples
- JavaFX | DatePicker with examples
- JavaFX | QuadCurve with examples
- JavaFX | Button with examples
- JavaFX | Alert with examples
- JavaFX | PointLight with examples
- JavaFX | RadioButton with examples
- JavaFx | ColorPicker with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ManasChhabra2

