在javafx中将文本放置在圆圈中
我目前正试图在Java应用程序中创建一系列圆圈中的字符。本质上,“欢迎java”这个短语将被放置在一个圆中,从最右边开始,W.在字符沿圆圈向下移动时,它们以一种方式旋转,使得字母的底部向内,以便形成一个圆。p> 我的代码似乎可以适当地旋转字符,但它们不会出现在窗格中心以外的任何位置。所以我的问题是:如何使用javafx实用程序在窗格中将字母隔开,使其远离中心?我的代码如下:在javafx中将文本放置在圆圈中,java,javafx,Java,Javafx,我目前正试图在Java应用程序中创建一系列圆圈中的字符。本质上,“欢迎java”这个短语将被放置在一个圆中,从最右边开始,W.在字符沿圆圈向下移动时,它们以一种方式旋转,使得字母的底部向内,以便形成一个圆。p> 我的代码似乎可以适当地旋转字符,但它们不会出现在窗格中心以外的任何位置。所以我的问题是:如何使用javafx实用程序在窗格中将字母隔开,使其远离中心?我的代码如下: import javafx.application.Application; import javafx.scene.Sc
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.geometry.Pos;
public class Characters extends Application{
@Override
public void start (Stage primaryStage){
//Create the pane
GridPane pane = new GridPane();
pane.setPrefSize(600, 600);
pane.setAlignment(Pos.CENTER);
//Font class instance
Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 35);
//Welcome to Java string
String welcome = "Welcome to Java";
double rotation = 90;
double x = Math.cos(rotation)*100;
double y = Math.sin(rotation)*100;
//Loop
for (int i = 0; i < welcome.length(); i++){
x = Math.cos(Math.toRadians(rotation));
y = Math.sin(Math.toRadians(rotation));
System.out.println("Y: " + y);
System.out.println("X: " + x);
Text text = new Text(x, y, welcome.charAt(i)+"");
System.out.println("Actual X" + text.getX());
System.out.println("Actual Y" + text.getY());
text.setFont(font);
text.setRotate(rotation);
pane.getChildren().add(text);
rotation += 22.5;
}
//Create the scene for the application
Scene scene = new Scene(pane, 500, 500);
primaryStage.setTitle("Characters around circle");
primaryStage.setScene(scene);
//Display
primaryStage.show();
}
public static void main (String [] args){
launch(args);
}
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.stage.stage;
导入javafx.scene.layout.GridPane;
导入javafx.scene.text.Font;
导入javafx.scene.text.fontpostation;
导入javafx.scene.text.FontWeight;
导入javafx.scene.text.text;
导入javafx.geometry.Pos;
公共类字符扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
//创建窗格
GridPane=新建GridPane();
窗格大小(600600);
窗格设置对齐(位置中心);
//字体类实例
Font Font=Font.Font(“新罗马时代”,FontWeight.BOLD,FONTPOSERATION.REQUALE,35);
//欢迎使用Java字符串
String welcome=“欢迎使用Java”;
双旋转=90;
双x=数学cos(旋转)*100;
双y=数学sin(旋转)*100;
//环路
for(int i=0;i
}这会让你更接近你想要的结果。里面有几个错误。您不应该在这里使用网格窗格,因为它的布局会干扰您的文本放置,并且您的公式也是错误的
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Characters extends Application {
@Override
public void start(Stage primaryStage) {
// Create the pane
// GridPane pane = new GridPane();
Pane pane = new Pane();
pane.setPrefSize(600, 600);
// pane.setAlignment(Pos.CENTER);
// Font class instance
Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 35);
// Welcome to Java string
String welcome = "Welcome to Java";
double rotation = 90;
double offset = pane.getPrefWidth() / 2;
double radius = 100;
double x = offset + Math.cos(rotation) * radius;
double y = offset + Math.sin(rotation) * radius;
// Loop
for (int i = 0; i < welcome.length(); i++) {
x = offset + Math.cos(Math.toRadians(rotation)) * radius;
y = offset + Math.sin(Math.toRadians(rotation)) * radius;
System.out.println("Y: " + y);
System.out.println("X: " + x);
Text text = new Text(x, y, welcome.charAt(i) + "");
System.out.println("Actual X" + text.getX());
System.out.println("Actual Y" + text.getY());
text.setFont(font);
text.setRotate(rotation);
pane.getChildren().add(text);
rotation += 22.5;
}
// Create the scene for the application
// Scene scene = new Scene(pane, 500, 500);
Scene scene = new Scene(pane);
primaryStage.setTitle("Characters around circle");
primaryStage.setScene(scene);
// Display
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.layout.Pane;
导入javafx.scene.text.Font;
导入javafx.scene.text.fontpostation;
导入javafx.scene.text.FontWeight;
导入javafx.scene.text.text;
导入javafx.stage.stage;
公共类字符扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
//创建窗格
//GridPane=新建GridPane();
窗格=新窗格();
窗格大小(600600);
//窗格设置对齐(位置中心);
//字体类实例
Font Font=Font.Font(“新罗马时代”,FontWeight.BOLD,FONTPOSERATION.REQUALE,35);
//欢迎使用Java字符串
String welcome=“欢迎使用Java”;
双旋转=90;
双偏移量=pane.getPrefWidth()/2;
双半径=100;
双x=偏移+数学坐标(旋转)*半径;
双y=偏移+数学正弦(旋转)*半径;
//环路
for(int i=0;i
这将使您更接近您想要的结果。里面有几个错误。您不应该在这里使用网格窗格,因为它的布局会干扰您的文本放置,并且您的公式也是错误的
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Characters extends Application {
@Override
public void start(Stage primaryStage) {
// Create the pane
// GridPane pane = new GridPane();
Pane pane = new Pane();
pane.setPrefSize(600, 600);
// pane.setAlignment(Pos.CENTER);
// Font class instance
Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 35);
// Welcome to Java string
String welcome = "Welcome to Java";
double rotation = 90;
double offset = pane.getPrefWidth() / 2;
double radius = 100;
double x = offset + Math.cos(rotation) * radius;
double y = offset + Math.sin(rotation) * radius;
// Loop
for (int i = 0; i < welcome.length(); i++) {
x = offset + Math.cos(Math.toRadians(rotation)) * radius;
y = offset + Math.sin(Math.toRadians(rotation)) * radius;
System.out.println("Y: " + y);
System.out.println("X: " + x);
Text text = new Text(x, y, welcome.charAt(i) + "");
System.out.println("Actual X" + text.getX());
System.out.println("Actual Y" + text.getY());
text.setFont(font);
text.setRotate(rotation);
pane.getChildren().add(text);
rotation += 22.5;
}
// Create the scene for the application
// Scene scene = new Scene(pane, 500, 500);
Scene scene = new Scene(pane);
primaryStage.setTitle("Characters around circle");
primaryStage.setScene(scene);
// Display
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.layout.Pane;
导入javafx.scene.text.Font;
导入javafx.scene.text.fontpostation;
导入javafx.scene.text.FontWeight;
导入javafx.scene.text.text;
导入javafx.stage.stage;
公共类字符扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
//创建窗格
//GridPane=新建GridPane();
窗格=新窗格();
窗格大小(600600);
//窗格设置对齐(位置中心);
//字体类实例
Font Font=Font.Font(“新罗马时代”,FontWeight.BOLD,FONTPOSERATION.REQUALE,35);
//欢迎使用Java字符串
String welcome=“欢迎使用Java”;
双旋转=90;
双偏移量=pane.getPrefWidth()/2;
双半径=100;
双x=偏移+数学坐标(旋转)*半径;
双y=偏移+数学正弦(旋转)*半径;
//环路
for(int i=0;i@Override
public void start(Stage primaryStage) {
//Create the pane
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
Group textGroup = new Group();
//Font class instance
Font font = Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 35);
//Welcome to Java string
String welcome = "Welcome to Java";
double rotation = 90;
double radius = 100d;
//Loop
for (char c : welcome.toCharArray()) {
// ignore whitespace, otherwise add rotated char
if (!Character.isWhitespace(c)) {
Text text = new Text(Character.toString(c));
text.setFont(font);
Rotate rotationMatrix = new Rotate(rotation, 0, radius);
text.getTransforms().add(rotationMatrix);
textGroup.getChildren().add(text);
}
rotation += 22.5;
}
pane.getChildren().add(textGroup);
//Create the scene for the application
Scene scene = new Scene(pane, 500, 500);
primaryStage.setTitle("Characters around circle");
primaryStage.setScene(scene);
//Display
primaryStage.show();
}