问题描述
我想知道你们是否可以帮助我。我正在尝试使用Java的内置图形模块制作一个动画程序。事实是,Java一次执行所有操作;不同的动画之间没有任何时间。最终产品只是最后一张图片。我需要一个在每张图片之间都放半秒的功能。
I was wondering if you guys could help me out. I'm trying to make an animation program with Java's built in graphics module... The thing is, Java executes everything at once; there isn't any time between the different animations. The end product is just the last picture. I need a function that puts like half a second in between each of the pictures.
感谢您的帮助。
规格:Blue-J,JDK 6。
Specs: Blue-J, JDK 6.
编辑:Btw,我是Java新手,这是一个阶级的事情。任务是制作动画,然后按 c前进每一帧,但我认为那是个贫民窟,所以我想要更好的东西。
Btw, I'm a Java Newbie, and this is a class thing. The assignment was to make an animation, and press 'c' to go forward each frame, but I think thats kinda ghetto, so I want something better.
推荐答案
创建,它每X毫秒执行一次,并在每次触发时绘制一帧。
Create a javax.swing.Timer that executes each X milliseconds, and draws one frame each time it is triggered.
这是来自Javadoc的示例:
This is the example from the javadoc:
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
修改延迟,例如20毫秒如果您的绘画时间不长,那将每秒提供约50帧。
Modify the delay, to e.g. 20ms. That will give you about 50 frames per second if your painting doesn't take too long.
这篇关于Java等待功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!