Tuesday 5 January 2021

JavaFX: How to define rounded rectangle?

Using setArcHeight and setArcWidth methods of Rectangle instance, you can define a rounded rectangle.

 

Rectangle rectangle = new Rectangle(300, 100, Color.GRAY);

rectangle.setArcHeight(30);

rectangle.setArcWidth(30);

 

Find the below working application.

 

RoundedRectangleDemo.java

package com.sample.app;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class RoundedRectangleDemo extends Application {

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

		Rectangle rectangle1 = new Rectangle(400, 200, Color.GRAY);
		rectangle1.setArcHeight(30);
		rectangle1.setArcWidth(30);
		
		Rectangle rectangle2 = new Rectangle(300, 100, Color.LIGHTBLUE);
		rectangle2.setArcHeight(30);
		rectangle2.setArcWidth(30);

		StackPane stackPane = new StackPane();
		stackPane.getChildren().addAll(rectangle1, rectangle2);

		Scene scene = new Scene(stackPane, 500, 400, Color.WHITE);

		primaryStage.setTitle("Rounded Rectangle Demo");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

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

 

Run above application, you will see below screen.


 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment