java - 如何使 JButton 沿着绘制的线条移动

标签 java swing timer runnable animated

image

我有 Jbutton(由信封表示)和信封将沿用的行的 ArrayList。谁能帮我?任何想法或教程将不胜感激。

最佳答案

我假设您的目标是沿线制作包络动画,以提供正在传输的数据的动画。

对于每一行,您首先需要找到直线方程。线性方程的形式为 y = a * x + b。如果您提到的 List 中已经有它,那很好。另一方面,如果您有两个点(开始位置和结束位置),请使用带有上述等式的点来为每一行找到 ab

在你得到你的线的方程式之后,你可以使用一个动画(可能使用 SwingWorkerSwingTimer)来增加 x 定期计算包络坐标,然后使用直线方程计算新的 y 坐标。

希望对你有帮助

更新:添加了使用 SwingTimer 进行演示的移动盒子的代码片段

package keymovement;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

public class TestMovingObject {

    static class Envelope {
        double x;
        double y;

        void setPosition(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }

    static class DrawArea extends JPanel {

        Envelope envelope = new Envelope();

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            int x = (int) Math.round(envelope.x);
            int y = (int) Math.round(envelope.y);
            g.drawRect(x, y, 20, 20);
        }

        public Envelope getEnvelope() {
            return envelope;
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        SwingUtilities.invokeLater( new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }

                JFrame window = new JFrame("Moving box");
                window.setPreferredSize(new Dimension(500, 400));
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                final DrawArea mainArea = new DrawArea();
                window.add(mainArea);
                window.pack();

                window.setLocationRelativeTo(null);
                window.setVisible(true);

                int delay = 20; // execute every 20 milliseconds i.e. 50 times per second  

                Timer timer = new Timer(delay, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Envelope envelope = mainArea.getEnvelope();
                        double x = envelope.x + 1;  // use smaller increments depending on the desired speed
                        double y = 0.5 * x + 2;     // moving along the line y = 0.5 * x + 2
                        envelope.setPosition(x, y);
                        mainArea.repaint();
                    }
                });
                timer.setRepeats(true);
                timer.start();

            }
        });
    }

}

关于java - 如何使 JButton 沿着绘制的线条移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159439/

相关文章:

java - 为什么 matlabcontrol 在代理创建时中断调用线程?

java - 将图像添加到多边形

java - 手动编码 Swing 组件

Java 比较 GridLayout 面板上的两个图像/按钮数组

c++ - SetTimer 函数是在后台运行还是进入休眠状态?

.net - 正确的方法来使Winforms UI阻塞对话框,直到出现一些非用户驱动的事件条件?

java - android fragment 内的 fragment

java - 哪个版本的 JaspeReports 支持 Java 8?

java - 尝试锁定文件进行读取时感到失望

javascript - 在 Javascript 中多次刷新后保持计时器一致