java 如何让一个矩形随时间移动

标签 java graphics paint

我正在尝试制作一个程序,当用户单击它时,它会获得坐标并按时间将该矩形移动到该坐标,我正在尝试学习的是如何在移动时打勾 (我没有上课,我只需要知道如何做到这一点)

最佳答案

您可能希望使用 Timer 和 TimerTask 每隔一秒左右移动一次矩形。

示例:

public class Reminder {
    Timer timer;

    public Reminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds * 1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
            System.out.format("Time's up!%n");
            timer.cancel(); //Terminate the timer thread
        }
    }

    public static void main(String args[]) {
        new Reminder(5);
        System.out.format("Task scheduled.%n");
    }
}

关于java 如何让一个矩形随时间移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113787/

相关文章:

r - R 中的高性能 2D OpenGL 图形用于使用 qtpaint (qt) 或 rdyncall (SDL/OpenGL) 包快速显示光栅图像?

Java - Swing - 绘画(在 JPanel 上添加其他形状)

java - 无法结束我的 Android 应用程序

image - 像 Photoshop 这样的高通滤波器

opengl - 为什么 OpenGL 有一个远剪裁平面,用什么习语来处理这个问题?

java - 按下按钮后显示的图像消失

java - 如何将 Rectangle 对象发送到 Java 中的 Paint() 方法

java - Realm 删除除一张表之外的所有表

Java Swing : Add resources at run time

Java Web 应用程序调用不同的其他 Java 应用程序(工作线程)