JavaFX Point2D layout text along circle path

Description

JavaFX Point2D layout text along circle path


import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

  @Override//from w  ww  .jav  a  2 s  .c  o m
  public void start(Stage primaryStage) {

    Pane pane = new Pane();
    String[] java = "Welcome to demo2s.com".split("");
    Font font = Font.font("Times New Roman", FontWeight.EXTRA_BOLD, 30);

    Point2D center = new Point2D(200, 200);
    double radius = 100;
    double angle = 0;
    double rotate = 90;
    for (int i = 0; i < java.length; i++, angle += 22, rotate += 22) {
      double x = center.getX() + radius * Math.cos(Math.toRadians(angle));
      double y = center.getY() + radius * Math.sin(Math.toRadians(angle));
      Text text = new Text(x, y, java[i]);
      text.setRotate(rotate);
      text.setFont(font);
      pane.getChildren().add(text);
    }

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

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



PreviousNext

Related