java javafx动画循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13029920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javafx animation looping
提问by Yohanes AI
in c++ or c programming language, we know to change the cordinate we use gotoxy(x,y) and we can use looping and sleep to change the cordinate and making animation. like this;
在 c++ 或 c 编程语言中,我们知道使用 gotoxy(x,y) 更改坐标,我们可以使用循环和睡眠来更改坐标并制作动画。像这样;
for(x = 20; x < 25; x++){
gotoxy(x,20); cout << "*"
}
but my queston is how about in JAVAFX 2.0 programming? i'm using netbeans 7.2. thanks for your any help.
但我的问题是在 JAVAFX 2.0 编程中怎么样?我正在使用 netbeans 7.2。感谢您的任何帮助。
采纳答案by Reimeus
Have look at using a Timeline Animation. It is a key component of animation in JavaFX and
is used to establish when, and in what sequence, key parts of an animation occur.
看看使用时间轴动画。它是 JavaFX 中动画的关键组件,
用于确定动画的关键部分何时以及以何种顺序发生。
Here is an example
这是一个例子
回答by jewelsea
Use the JavaFX Animation Package.
使用JavaFX 动画包。
There are numerous examples in the JavaFX Animation Tutorial, as Andy pointed out in his comment.
正如 Andy 在评论中指出的那样,JavaFX 动画教程中有许多示例。
And there is a cute example of a running horse animation loop.
还有一个跑马动画循环的可爱例子。
The key is that you don't sleep the JavaFX application thread and you have to release control of the JavaFX thread back to the JavaFX system each time you update something and want it rendered. The JavaFX animation classes take care of these things for you so that you don't have to worry about it. If you just loop like you do in the sample code from your question, JavaFX will just render the scene once after your loop has completed and you will never see anything happen.
关键是您不会让 JavaFX 应用程序线程休眠,并且每次更新某些内容并希望呈现它时,您都必须将 JavaFX 线程的控制权释放回 JavaFX 系统。JavaFX 动画类会为您处理这些事情,因此您不必担心。如果您只是像在问题的示例代码中那样循环,JavaFX 只会在循环完成后渲染场景一次,您将永远看不到任何事情发生。
Here is a fairly boring example which uses a Timelineto emulate the c++ code in your question to move a dot a pixel every 400 milliseconds.
这是一个相当无聊的示例,它使用时间轴来模拟问题中的 C++ 代码,每 400 毫秒移动一个像素点。
import java.util.Date;
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
/** Simple JavaFX Animation Sample. */
public class AnimationSample extends Application {
private int x = 20;
private String status = "";
private final Circle dot = new Circle(20, 20, 3);
private final TimeCounter counter = new TimeCounter();
public static void main(String[] args) throws Exception { launch(args); }
@Override public void start(final Stage stage) throws Exception {
final Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new EventHandler() {
@Override public void handle(Event event) {
refreshScene();
}
}),
new KeyFrame(Duration.millis(400))
);
timeline.setCycleCount(Timeline.INDEFINITE);
stage.setScene(new Scene(new Group(dot), 50, 50));
stage.show();
counter.reset();
timeline.play();
}
private void refreshScene() {
gotoxy(x, 20);
status = "*****".equals(status) ? "*" : status + "*";
System.out.println(String.format("%7d", counter.elapsed()) + " ms " + x + " " + status);
if (x == 24) {
x = 20;
} else {
x++;
}
}
private void gotoxy(int x, int y) {
dot.setCenterX(x);
dot.setCenterY(y);
}
class TimeCounter {
private long start = new Date().getTime();
void reset() { start = new Date().getTime(); }
long elapsed() { return new Date().getTime() - start; }
}
}
回答by user1338062
There are three different options in JavaFX, depending on your needs.
根据您的需要,JavaFX 中有三种不同的选项。
The most basic one is AnimationTimer. It's equivalent to Swing's Timer. It simply contains a handle method which is called on every frame, and passed current time as argument. You probably want some internal bookkeeping so that you do not do expensive calculations every time handle is called.
最基本的一种是AnimationTimer。它相当于 Swing 的 Timer。它只包含一个在每一帧上调用的句柄方法,并将当前时间作为参数传递。您可能需要一些内部簿记,以便每次调用 handle 时都不会进行昂贵的计算。
Transitionhas an interpolate(frac) method, which gets called with values of frac between 0.0 and 1.0. It's up to you to do all UI changes you want to, based on the frac value. Both Transition and Timeline extend Animation, so you can set stuff like cycle duration, whether the Transition is reversed at end, etc.
Transition有一个 interpolate(frac) 方法,该方法使用 0.0 和 1.0 之间的 frac 值调用。您可以根据 frac 值进行所有您想要的 UI 更改。Transition 和 Timeline 都扩展了 Animation,因此您可以设置诸如循环持续时间、Transition 在结束时是否反转等内容。
Timelineis the most complex one. You define arbitrary amount of KeyFrames (think of states) that contain wanted properties of different Nodes, and the Timeline will do all the work for you interpolating how to animate the change between the provided values. For example, you can give a keyframe where x property of a Node is 0, and another where it's 100, and Timeline will do the animating for you.
时间线是最复杂的。您可以定义任意数量的包含不同节点所需属性的关键帧(想想状态),时间轴将为您完成所有工作,为您插入如何为提供的值之间的变化设置动画。例如,您可以指定一个节点的 x 属性为 0 的关键帧,另一个为 100 的关键帧,时间轴将为您制作动画。