Java语言中怎么实现文字的反弹,折返后自动倒序输出

Java语言中怎么实现文字的反弹,当文字到达右边的时候,折返后自动倒序输出,比如

123
    123
         123 
              123 
                   312
                 321
              321
        321
   ...

https://blog.csdn.net/qq_39842184/article/details/131732621


import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TextBounce extends Application {

    private double x = 0; // 文字的x坐标
    private double y = 50; // 文字的y坐标
    private int direction = 1; // 文字的运动方向(1为向右,-1为向左)
    private Text text; // 文字对象

    @Override
    public void start(Stage primaryStage) {
        // 创建文字对象,并设置字体和文本内容
        text = new Text("Hello, World!");
        text.setFont(new Font(30));

        // 创建一个Group对象,并将文字对象添加到其中
        Group root = new Group();
        root.getChildren().add(text);

        // 创建一个Timeline对象,并设置动画
        Timeline timeline = new Timeline(
                new KeyFrame(Duration.millis(10), e -> moveText()),
                new KeyFrame(Duration.millis(5000), e -> reverseDirection())
        );
        timeline.setCycleCount(Animation.INDEFINITE);

        // 创建一个Scene对象,并将Group对象添加到其中
        Scene scene = new Scene(root, 600, 100, Color.WHITE);

        // 将Scene对象添加到Stage对象中,并设置标题和显示
        primaryStage.setScene(scene);
        primaryStage.setTitle("Text Bounce");
        primaryStage.show();

        // 开始动画
        timeline.play();
    }

    // 移动文字的方法
    private void moveText() {
        // 根据当前方向更新文字的坐标
        x += 5 * direction;
        text.setX(x);

        // 如果文字到达了窗口边界,则改变方向并折返
        if (x >= text.getScene().getWidth() - text.getLayoutBounds().getWidth() || x <= 0) {
            direction = -direction;
            text.setText(reverseText(text.getText()));
        }
    }

    // 改变方向的方法
    private void reverseDirection() {
        direction = -direction;
        text.setText(reverseText(text.getText()));
    }

    // 折返并倒序输出文字的方法
    private String reverseText(String str) {
        StringBuilder sb = new StringBuilder(str);
        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以看下这个问题的回答https://ask.csdn.net/questions/1073714
  • 这篇博客你也可以参考下:利用java实现对一个目录下中文文本的抽取,并统计词频,降序排序。
  • 你还可以看下java参考手册中的 java-学习Java语言 - 描述Java编程语言的基本概念和特点的课程。-类和对象》描述了如何编写创建对象的类,以及如何创建和使用对象。-课堂
  • 除此之外, 这篇博客: JAVA线程池是什么,能带来什么好处,线程阻塞队列有哪些怎么用,饱和策略又是什么,运行机制是什么中的 定义线程池 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    /**
         * 线程池
         */
        private static ThreadPoolExecutor executor =
                new ThreadPoolExecutor(
                        // 核心线程数和最大线程数
                        2, 3,
    
                        // 线程空闲后的存活时间
                        60L, TimeUnit.SECONDS,
    
                        // 有界阻塞队列
                        new LinkedBlockingQueue<Runnable>(5)
               );
    
  • 您还可以看一下 徐无忌老师的Java进阶高手课-深入浅出Java网络编程课程中的 案例实战:浏览器打开一个网址,整个过程是怎么样的?小节, 巩固相关知识点
  • 以下回答来自chatgpt:

    要实现文字反弹和按倒序输出的效果,可以利用Java的图形界面库如Swing或JavaFX来实现。以下是使用Swing库实现的示例代码:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class BouncingText extends JFrame {
        private JLabel textLabel;
        private Timer timer;
        private int x, y, dx;
    
        public BouncingText() {
            super("Bouncing Text");
            setSize(400, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            // 初始化文字标签的位置和速度
            x = 0;
            y = getHeight() / 2;
            dx = 3;
    
            textLabel = new JLabel("Hello, world!");
            textLabel.setFont(new Font("Arial", Font.PLAIN, 24));
            add(textLabel);
    
            // 创建定时器,每隔一定时间重绘文字标签的位置
            timer = new Timer(10, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    x += dx;
    
                    // 判断是否到达右边界
                    if (x + textLabel.getWidth() > getWidth()) {
                        x = getWidth() - textLabel.getWidth();
                        dx = -dx;  // 反弹
                        StringBuilder reversedText = new StringBuilder(textLabel.getText()).reverse();
                        textLabel.setText(reversedText.toString());  // 倒序输出
                    }
    
                    // 判断是否到达左边界
                    if (x < 0) {
                        x = 0;
                        dx = -dx;  // 反弹
                        StringBuilder reversedText = new StringBuilder(textLabel.getText()).reverse();
                        textLabel.setText(reversedText.toString());  // 倒序输出
                    }
    
                    // 更新文字标签的位置
                    textLabel.setLocation(x, y);
                }
            });
    
            // 启动定时器
            timer.start();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new BouncingText().setVisible(true);
                }
            });
        }
    }
    

    运行以上代码,就可以看到一个窗口中的文字从左至右移动,并在到达右边界时自动反弹并以倒序的方式输出。如果需要更改文字内容,可以修改textLabel.setText("Hello, world!");这一行代码中的文字。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^