What is the preferred way of getting the frame rate of a JavaFX application?

后端 未结 3 1547
梦谈多话
梦谈多话 2020-12-17 00:08

This is quite a simple question:

What is the preferred way of getting the frame rate of a JavaFX application?

Google turns up a result from 2009, but that e

3条回答
  •  悲哀的现实
    2020-12-17 01:03

    I just copied James_D program and changed to using the PerformanceTracker. The options I copied from the program I had downloaded earlier called JavaFXBalls3. The options don't seem to make a difference.

    Press any key to to see what the label says. Mine's always near 60. Maybe a more complicated scene would be lower. AFAIK 60 is the max for animation.

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    import com.sun.javafx.perf.PerformanceTracker;
    import java.security.AccessControlException;
    import javafx.animation.AnimationTimer;
    import javafx.scene.control.Label;
    import javafx.scene.layout.VBox;
    
    
    public class FPS extends Application {
        public static void main(String[] args) { launch(args); }
    
        private static PerformanceTracker tracker;
    
        @Override
        public void start(Stage stage) {
            VBox root = new VBox(20);
            Label label1 = new Label();
            Label label2 = new Label();
            root.getChildren().addAll(label1, label2);
    
            Scene scene = new Scene(root, 200, 100);
    
            try {
                System.setProperty("prism.verbose", "true");
                System.setProperty("prism.dirtyopts", "false");
                //System.setProperty("javafx.animation.fullspeed", "true");
                System.setProperty("javafx.animation.pulse", "10");
            } catch (AccessControlException e) {}
    
            scene.setOnKeyPressed((e)->{
                label2.setText(label1.getText());
            });
            stage.setScene(scene);
            stage.show();
    
    
            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;
        }
    
    }
    

提交回复
热议问题