我使用JDK 8.0附带的JavaFx,在配备2.4 GHz Intel Core 2 Duo处理器和4GB RAM的MacBook Pro上使用。
我有一个奇怪的行为,使用以下类:
import com.sun.javafx.perf.PerformanceTracker;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class HelloWorld extends Application {
private static PerformanceTracker tracker;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene= new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
Label label1 = new Label();
Label label2 = new Label();
((Pane)root).getChildren().addAll(label1, label2);
scene.setOnKeyPressed((e)->{
label2.setText(label1.getText());
});
tracker = PerformanceTracker.getSceneTracker(scene);
AnimationTimer frameRateMeter = new AnimationTimer() {
@Override
public void handle(long now) {
label1.setText(String.format("Current frame rate: %.3f fps", getFPS()));
}
};
//frameRateMeter.start();
}
private float getFPS () {
float fps = tracker.getAverageFPS();
tracker.resetAverageFPS();
return fps;
}
}
执行此代码时,CPU使用0.2%到最大10%的百分比。 如果我删除评论:
frameRateMeter.start();
我得到相同的代码使用20%到40%的CPU。
这只是一个例子,但是,我写的应用程序,注释掉上面的行,运行使用大约40%的CPU并删除注释运行在100%的CPU附近。
这是正常的吗?我注意到,添加连续执行的时间轴(也非常简单)的任何时间都会产生一种荒谬的CPU使用。
在JavaFX中使用动画真是太贵了,还是我错过了什么?
任何帮助都会非常感激。
答案 0 :(得分:2)
我认为你经常更新Label中的FPS值:
类AnimationTimer允许创建一个计时器,当它处于活动状态时,在每个帧中调用。
当您更新label1
中的文本时,JavaFX会一次又一次地绘制新帧。您可以轻松检查:将FPS写入STDOUT而不是label1
:
...
System.out.println(String.format("Current frame rate: %.3f fps", tracker.getAverageFPS()));
// label1.setText(String.format("Current frame rate: %.3f fps", tracker.getAverageFPS()));
...
在这种情况下,您应该看到更少的FPS费率。
因此,尝试使用任何Java / JavaFX计时器每隔一秒或半秒更新FPS值,即:
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(500), event -> {
label1.setText(String.format("Current frame rate: %.3f fps", tracker.getAverageFPS()));
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
我的macOS平均CPU值约为1.8-2%。
答案 1 :(得分:1)
这是一种从AnimationTimer
计算 FPS [每秒帧数]而不使用任何其他类的简单方法:
/**
* AnimationTimer .
*
* @author GOXR3PLUS
*/
public class PaintService extends AnimationTimer {
/** The next second. */
long nextSecond = 0L;
/** The Constant ONE_SECOND_NANOS. */
private static final long ONE_SECOND_NANOS = 1_000_000_000L;
/**
* When this property is <b>true</b> the AnimationTimer is running
*/
private volatile SimpleBooleanProperty running = new SimpleBooleanProperty(false);
@Override
public void start() {
nextSecond = 0L;
super.start();
running.set(true);
}
@Override
public void stop() {
super.stop();
running.set(false);
}
/**
* @return True if AnimationTimer is running
*/
public boolean isRunning() {
return running.get();
}
/**
* @return Running Property
*/
public SimpleBooleanProperty runningProperty() {
return running;
}
@Override
public void handle(long nanos) {
// -- Show FPS if necessary.
if (true) { //originally i had a variable (showFPS) here
framesPerSecond++;
// Check for 1 second passed
if (nanos >= nextSecond) {
fps = framesPerSecond;
framesPerSecond = 0;
nextSecond = nanos + ONE_SECOND_NANOS;
}
label.setText("FPS: " + fps);
}
}
}