先上图,个人觉得挺有意思的。 刚学JAVAFX的时候,遇到这个题,一开始也没想到有什么好方法能实现这个字符串围圈的效果。索性不想了,暴力求解,耗费三十余分钟,一点点调出来的。
写完这段代码,我最深的感受就是:确实,条条大路通罗马。就算是条弯路,多花点时间,还是有希望走到的。但如果一开使没找到直路(或者可以说是巧妙的解题方法),就在那干想、干等,如果一直没想出来,反而浪费了时间。说不定别人走弯路的已经曲折地抵达了,如果一直想着非要找条直路过去,最佳的路线过去,反而这漫长的等待最优解的过程相对成了弯路。
所以写代码时,走弯路不可怕,有行动就行。暴力解法也是解了,hhhhhh!
下面是我的暴力解法效果图和代码(就是要让字符串工整点绕个圈展示出来):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class CircleString extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
String str = "AVA WELCOME TO J";
double rotateAngle = -24;
double x = 400;
double vx = 40;
double y = 164;
double vy = 16;
for (int i = 0; i < str.length(); i++) {
String s = ""+str.charAt(i);
if(i==0){
x+=vx;
y+=vy+2;
}else if(i==1){
x+=vx+4;
y+=vy+2-11;
}else if(i==2){
x+=vx-2;
y+=vy+2+11;
}else if(i==3){
x+=vx-1-3;
y+=vy+2;
}else if(i==4){
x+=vx;
y+=vy;
y+=46;
x-=46;
}else if(i==5){
x-=6;
y+=46;
}else if(i==6){
x-=22;
y+=38;
}else if(i==7){
x-=38;
y+=21;
}else if(i==8){
x=440;
y+=8;
}else if(i==9){
x-=48;
y-=14;
}else if(i==10){
x-=38;
y-=32;
}else if(i==12){
x-=58;
y-=68;
}else if(i==13){
x+=8;
y-=52;
}else if(i==15){
x+=16;
y-=56;
}else{
x+=vx;
}
rotateAngle+=22.8;
Text text = new Text(x,y,s);
text.setFont(Font.font("Arial",56));
text.setFill(Color.BLACK);
text.setRotate(rotateAngle);
pane.getChildren().add(text);
}
primaryStage.setTitle("welcome to java");
Scene scene = new Scene(pane,900,600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
后来和好友讨论,找到了更好的解法,是用了JAVAFX下面的Circle类的相关已有函数和Math包下的余弦正弦函数。 (说到底,是初学JAVAFX,积累少了,要不然想到这函数,这个题很好就解决了,不过还是感觉没有我自己调的好看,要不怎么说有意思呢?hhhhhh)可见积累在编程领域是特别特别重要的。
下面是巧妙在编程上运用数学的解法的效果图和代码(是不是感觉还是没我暴力解出来的看着整齐舒服,单词的间距处理也没有上面的暴力解法好看。所以直路还不一定能得最佳结果,虽然说省时哈。暴力的弯路加上用心的设计是有运气得到更好结果的,毕竟是用了心和时间)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* @author:tengsen
* @create: 2022-06-20 21:49
* @Description:
*/
public class CircleString2 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane borderPane = new BorderPane();
Circle circle = new Circle(450,300,120);
borderPane.getChildren().add(circle);
circle.setFill(Color.WHITE);
String str = "WELCOME TO JAVA";
double temp = (2*Math.PI)/str.length();
for (int i = 0; i < str.length(); i++) {
double x = circle.getCenterX()+circle.getRadius()*Math.cos(temp*i);
double y = circle.getCenterY()+circle.getRadius()*Math.sin(temp*i);
Text t = new Text(x,y,str.charAt(i)+"");
t.setRotate(90+(360/str.length())*i);
t.setFont(Font.font(42));
borderPane.getChildren().add(t);
}
Scene scene = new Scene(borderPane,900,600);
primaryStage.setTitle("Welcome to java");
primaryStage.setScene(scene);
primaryStage.show();
}
}