Monday, 26 October 2020

JavaFX: Working with Ellipse shape

 

Ellipse class creates a new ellipse with the specified size and location in pixels.

 

Ellipse class provides following constructors to draw Ellipse shape.

 

public Ellipse()

public Ellipse(double radiusX, double radiusY)

public Ellipse(double centerX, double centerY, double radiusX, double radiusY)

centerX the horizontal position of the center of the ellipse in pixels

centerY the vertical position of the center of the ellipse in pixels

radiusX the horizontal radius of the ellipse in pixels

radiusY the vertical radius of the ellipse in pixels

 

Example

Ellipse ellipse1 = new Ellipse();

Ellipse ellipse2 = new Ellipse(150, 100);

Ellipse ellipse3 = new Ellipse(150, 100, 100, 50);

 

Corresponding fxml looks like below.

<Ellipse fx:id="ellipse1" centerX="150" centerY="100"

                  radiusX ="100" radiusY="50"

                  fill="lightgreen" stroke="black" strokeWidth="5"/>

 

Find the below working application.

 

ellipseDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.EllipseController">

	<Ellipse fx:id="ellipse1" centerX="150" centerY="100"
		radiusX ="100" radiusY="50"
		fill="lightgreen" stroke="black" strokeWidth="5"/>
	
</Group>

 

EllipseController.java

package com.sample.app.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.shape.Ellipse;

public class EllipseController implements Initializable {

	@FXML
	private Ellipse ellipse1;

	
	@Override
	public void initialize(URL location, ResourceBundle resources) {

	}

}

 

EllipseDemo.java

package com.sample.app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class EllipseDemo extends Application {

	public static void main(String args[]) {
		launch(args);

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(EllipseDemo.class.getResource("/ellipseDemo.fxml"));

		Scene scene = new Scene(root, 300, 200, Color.WHITE);

		primaryStage.setTitle("Ellipse Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

Output

 


 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment