gpt4 book ai didi

java - 使用JavaFX 的简单Java 方形动画不流畅,为什么?

转载 作者:搜寻专家 更新时间:2023-11-01 03:34:04 25 4
gpt4 key购买 nike

所以我注意到我的动画的帧率/平滑度有一些问题。动画有点断断续续。但是,在进行测试之后,我注意到只要触发调整大小事件,它就会再次变得平滑,即使只有 0.1 px。我安装了最新的 Java。

我无法使用 vsync,除非它的 opengl 并且 javafx 似乎正在使用三重错误或其他东西。无论哪种方式,性能都非常糟糕。我的 Windows 机器非常好,而且是最新版本。

所以在调用 show() 函数后,我添加了:

Window.setWidth(Window.getWidth() + 0.1)

问题已解决,但我当然想知道引擎盖下发生了什么,以及如何在不诉诸这种原始黑客的情况下真正解决这个问题?

JavaFX Canvas Double Buffering

我的代码如下:

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.util.Duration;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.control.Button;

public class Gui extends Application{

Stage Window;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {

Window = primaryStage;
Window.setTitle("Gui Tester");

Group root = new Group();
Rectangle box = new Rectangle(0, 0, 50,50);
box.setFill(Color.GREEN);
KeyValue x = new KeyValue(box.xProperty(), 900);
KeyFrame keyFrame = new KeyFrame(Duration.millis(3000), x);
Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
timeline.getKeyFrames().add(keyFrame);
timeline.play();
root.getChildren().add(box);

GridPane grid = new GridPane();
grid.setVgap(8);

root.getChildren().add(grid);

Button newGame = new Button("START NEW GAME");

GridPane.setConstraints(newGame, 1, 1);

Button continueGame = new Button("CONTINUE");
GridPane.setConstraints(continueGame, 1, 2);

grid.getChildren().addAll(newGame, continueGame);

Scene scene = new Scene(root, 1000, 1000);
scene.getStylesheets().add("tester.css");
Window.setScene(scene);
Window.show();
Window.setWidth(Window.getWidth() + 0.1)
}
}

最佳答案

在 Mac OS X 10.11.4、Java 1.8.0_92、NVIDIA GeForce 9400 上测试,无节能。

example显示,使用 Timeline,这个 example ,使用 PathTransition,开始起伏不定。随后,在大约一个自动反转周期后,它们都平滑了。使用 -Xint 选项,它“以仅解释模式运行应用程序”,如图所示 here , 显着减少了最初的口吃,尤其是在第二次运行之后。如果应用程序仍然局限于单个内核,或者动画创建一个无法到达安全点的繁忙循环,则由于即时编译器开销而导致的延迟可能会被放大,如图所示 here .

关于java - 使用JavaFX 的简单Java 方形动画不流畅,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37170197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com