JavaFX Text layout along circle path

Description

JavaFX Text layout along circle path


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

public class Main extends Application {
  @Override// ww w .  ja v a 2  s  .  co  m
  public void start(Stage primaryStage) {
    final double SIZE = 400.0;
    final double RADIUS = SIZE / 2;
    String[] s = "WELCOME TO DEMO2S.COM".split("");

    Pane letters = new Pane();

    for (int i = 0; i < s.length; i++) {
      double angle = (2 * i * Math.PI) / s.length;
      double x = RADIUS + RADIUS * Math.cos(angle);
      double y = RADIUS + RADIUS * Math.sin(angle);
      Text t = new Text(x, y, s[i]);
      t.setFont(Font.font("Times New Roman", RADIUS / 8));
      t.setRotate(90 + Math.toDegrees(angle));
      letters.getChildren().add(t);
    }

    StackPane pane = new StackPane();
    pane.getChildren().add(letters);
    pane.setPadding(new Insets(20));
    pane.setAlignment(Pos.CENTER);

    Scene scene = new Scene(pane);
    primaryStage.setTitle("java2s.com");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

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



PreviousNext

Related