o7planning

JavaFX Rectangle and Ellipse Tutorial with Examples

  1. JavaFX Rectangle
  2. Rounded corner rectangle
  3. JavaFX Ellipse
  4. Effects

1. JavaFX Rectangle

JavaFX Rectangle is the class that helps draw a rectangle, the same as class Line and Circle, it is extends from Shape class.
RectangleDemo.java
package org.o7planning.javafx.rectangle;

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

public class RectangleDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        Rectangle rectangle = new Rectangle();
        rectangle.setX(20);
        rectangle.setY(50);
        rectangle.setWidth(200);
        rectangle.setHeight(150);
        rectangle.setFill(Color.BISQUE);

        Group root = new Group();
        root.getChildren().add(rectangle);

        Scene scene = new Scene(root, 400, 250, Color.WHITE);
        primaryStage.setTitle("JavaFX Rectangle (o7planning.org)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

2. Rounded corner rectangle

RectangleRoundedCornerDemo.java
package org.o7planning.javafx.rectangle;

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

public class RectangleRoundedCornerDemo extends Application {

    @Override
    public void start(Stage primaryStage) {

        Rectangle rectangle = new Rectangle();
        rectangle.setX(80);
        rectangle.setY(50);
        rectangle.setWidth(200);
        rectangle.setHeight(150);
        rectangle.setFill(Color.CADETBLUE);
       
        rectangle.setArcHeight(105);
        rectangle.setArcWidth(50);

        Group root = new Group();
        root.getChildren().add(rectangle);

        Scene scene = new Scene(root, 400, 250, Color.WHITE);
        primaryStage.setTitle("JavaFX Rectangle (o7planning.org)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

3. JavaFX Ellipse

EllipseDemo.java
package org.o7planning.javafx.ellipse;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

public class EllipseDemo extends Application {

   @Override
   public void start(Stage primaryStage) {

       Group root = new Group();

       Ellipse ellipse = new Ellipse();
       ellipse.setCenterX(180.0f);
       ellipse.setCenterY(150.0f);

       // Radius X
       ellipse.setRadiusX(120.0f);

       // Radius Y
       ellipse.setRadiusY(80.0f);

       // Fill color.
       ellipse.setFill(Color.CORNFLOWERBLUE);

       root.getChildren().add(ellipse);

       Scene scene = new Scene(root, 400, 250);

       primaryStage.setTitle("JavaFX Ellipse (o7planning.org)");

       primaryStage.setScene(scene);
       primaryStage.show();
   }

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

}

4. Effects

Rotate:
EllipseDemo2.java
package org.o7planning.javafx.ellipse;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

public class EllipseDemo2 extends Application {

    @Override
    public void start(Stage primaryStage) {

        Group root = new Group();

        Ellipse ellipse = new Ellipse();
        ellipse.setCenterX(180.0f);
        ellipse.setCenterY(150.0f);

        // Radius X
        ellipse.setRadiusX(120.0f);

        // Radius Y
        ellipse.setRadiusY(80.0f);

        // Fill color.
        ellipse.setFill(Color.CORNFLOWERBLUE);
       
        // Rotate 30 degrees
        ellipse.setRotate(30);

        root.getChildren().add(ellipse);

        Scene scene = new Scene(root, 400, 250);

        primaryStage.setTitle("JavaFX Ellipse (o7planning.org)");

        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

JavaFX Tutorials

Show More