- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我将 AnimationTimer
用于多个任务,例如带有不断变化的图片的动画和 ProgressIndicator
动画。为了达到所需的速度,我将线程置于 hibernate 状态,但是当多个动画同时运行时,它们会相互影响彼此的速度。有没有其他方法可以改变 AnimationTimer
的速度?代码示例:
private void initialize() {
programButtonAnimation=new AnimationTimer(){
@Override
public void handle(long now) {
showClockAnimation();
}
};
programButtonAnimation.start();
}
private void showClockAnimation(){
String imageName = "%s_"+"%05d"+".%s";
String picturePath="t093760/diploma/view/styles/images/pink_frames/"+String.format( imageName,"pink" ,frameCount,"png");
programButton.setStyle("-fx-background-image:url('"+picturePath+"')");
frameCount++;
try {
Thread.sleep(28);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(frameCount>=120){
programButtonAnimation.stop();
frameCount=0;
}
}
最佳答案
AnimationTimer
的handle
方法在 FX 应用程序线程上为渲染的每个帧调用一次。你永远不应该阻塞那个线程,所以不要在这里调用 Thread.sleep(...)
。
传递给handle(...)
方法的参数是一个时间戳,以纳秒为单位。因此,如果您想限制更新,使它们不会发生超过一次,比如 28 毫秒,您可以使用它来做到这一点:
private void initialize() {
programButtonAnimation=new AnimationTimer(){
private long lastUpdate = 0 ;
@Override
public void handle(long now) {
if (now - lastUpdate >= 28_000_000) {
showClockAnimation();
lastUpdate = now ;
}
}
};
programButtonAnimation.start();
}
private void showClockAnimation(){
String imageName = "%s_"+"%05d"+".%s";
String picturePath="t093760/diploma/view/styles/images/pink_frames/"+String.format( imageName,"pink" ,frameCount,"png");
programButton.setStyle("-fx-background-image:url('"+picturePath+"')");
frameCount++;
if(frameCount>=120){
programButtonAnimation.stop();
frameCount=0;
}
}
关于java - 如何更改 AnimationTimer 速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30146560/
这是引自 AnimationTimer类文档。 The class AnimationTimer allows to create a timer, that is called in each fr
所以,我正在使用 JavaFX 创建贪吃蛇游戏,但我似乎无法使游戏正确暂停,即它偶尔会暂停,而其他时候,游戏只是忽略暂停。所以,基本上我有一个 Main 类,我在其中初始化所有 GUI 组件,并且它还
JavaFX 的 AnimationTimer 启动时是否在单独的线程上运行? 我在调试器中运行了一个 JavaFX 应用程序,无论有没有 AnimationTimer,在这两种情况下都有 6 个线程
所以,我正在使用 JavaFX 创建贪吃蛇游戏,但我似乎无法使游戏正确暂停,即它偶尔会暂停,而其他时候,游戏只是忽略暂停。所以,基本上我有一个 Main 类,我在其中初始化所有 GUI 组件,并且它还
当玩家赢得我创建的游戏时,我试图显示一个警报对话框。但是,我遇到了一个异常(exception): java.lang.IllegalStateException: showAndWait is no
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我目前正在开发一款使用 JavaFX 制作的游戏,我想知道如何将 AnimationTimer 暂停一定的秒数?我尝试使用 Thread.sleep,但它会导致整个窗口卡住。任何帮助将不胜感激,如果需
我知道可以使用 this.stop 函数从其覆盖的 handle() 方法中停止正在进行的动画。下面的代码来自游戏,在游戏获胜并显示警报后,它应该停止运行动画。相反,警报会弹出无数次。 new Ani
我已经编写了一个简单的游戏,使用 JavaFX 作为 GUI 并使用 AnimationTimer 作为游戏本身,但我发现在 AnimationTimer 对象内更新的任何字段都无法在该对象外部找到。
我将 AnimationTimer 用于多个任务,例如带有不断变化的图片的动画和 ProgressIndicator 动画。为了达到所需的速度,我将线程置于 hibernate 状态,但是当多个动画同
我正在尝试向地球绕太阳运行的小动画添加一个“开始”按钮。我的问题是,当我单击按钮时,AnimationTimer 似乎从未收到 start() 调用。 当单击按钮的事件被触发时,动画计时器应该启动,从
考虑这段代码: import java.util.ArrayDeque; import javafx.animation.AnimationTimer; import javafx.animation
我有一个用 JavaFX 编写的俄罗斯方 block 游戏。我想把它翻译成C++来学习C++。我可以在 C++ 中使用什么来显示图形并为游戏循环计时,就像我使用 JavaFX 和 AnimationT
我正在尝试制作一个游戏,其中我有类矩形的障碍,并让它左右滑动。然而,虽然看起来应该可以工作,但我命名为障碍物 1 的矩形却没有任何移动。 import javafx.animation.Animati
我是一名优秀的程序员,十分优秀!