devstory

Le Tutoriel de JavaFX Rectangle et Ellipse

  1. JavaFX Rectangle
  2. Rectangle d'angle arrondi
  3. JavaFX Ellipse
  4. Les effets

1. JavaFX Rectangle

La classe Rectangle est la classe qui aide à dessiner un rectangle, comme la classe Line, Circle, elle est étendue de la classe 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. Rectangle d'angle arrondi

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. Les effets

Exemple de rotation :
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);
    }

}

Tutoriels de JavaFX

Show More