Hướng dẫn sử dụng JavaFX Rectangle và Ellipse
Công ty Vĩnh Cửu tuyển dụng lập trình viên Java

1- JavaFX Rectangle

Class Rectangle là class giúp vẽ ra một hình chữ nhật, cũng giống như các class Line, Circle,. nó cũng mở rộng từ class Shape.
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- Hình chữ nhật góc tròn

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);
 
       // Bán kính theo trục X
       ellipse.setRadiusX(120.0f);
 
       // Bán kính theo trục Y
       ellipse.setRadiusY(80.0f);

       // Mầu nền
       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- Các hiệu ứng

Ví dụ xoay hình.
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);
 
       // Bán kính theo trục X
       ellipse.setRadiusX(120.0f);
 
       // Bán kính theo trục Y
       ellipse.setRadiusY(80.0f);
 
       // Mầu nền
       ellipse.setFill(Color.CORNFLOWERBLUE);
     
        // Xoay một góc 30 độ
       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);
   }

}