3
\$\begingroup\$

(See the continuation of this post in A JavaFX program to find out the mouse refresh rate v3.)

(This post is a continuation of A JavaFX program to find out the mouse refresh rate.)

After adopting the answer by @Reinderien, I ended up with this:

com.github.coderodde.javafx.mouseupdaterate.MouseUpdateRateFinder.java:

package com.github.coderodde.javafx.mouseupdaterate;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public final class MouseUpdateRateFinder extends Application {

    private static final int SCREEN_WIDTH = 800;
    private static final int SCREEN_HEIGHT = 600;

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

    @Override
    public void start(Stage stage) {
        Group root = new Group();

        MyMouseListener listener = new MyMouseListener();

        Canvas canvas = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT);
        
        // Add the event listeners:
        canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, 
                               listener::handlePressed);
        
        canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, 
                               listener::handleDragged);
        
        canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, 
                               listener::handleReleased);

        root.getChildren().add(canvas);
        stage.setScene(new Scene(root));
        stage.show();
    }

    private static final class MyMouseListener {
        private long dragStartNanoseconds;
        private int frameCounter;

        public void handlePressed(MouseEvent t) {
            dragStartNanoseconds = System.nanoTime();
            frameCounter = 0;
        }

        public void handleDragged(MouseEvent t) {
            frameCounter++;
        }

        public void handleReleased(MouseEvent t) {
            double durationInNanoseconds = 
                    System.nanoTime() - dragStartNanoseconds;
            
            double durationInSeconds = durationInNanoseconds / 1e9;
            
            String durationInSecondsString = 
                    String.format("%.2f", durationInSeconds).replace(',', '.');
            
            String frequencyString = 
                    String.format("%.1f", frameCounter / durationInSeconds)
                          .replace(',', '.');
            
            System.out.printf(
                "%d frames in %s s, refresh rate of %s Hz.%n",
                frameCounter, 
                durationInSecondsString,
                frequencyString);
        }
    }
}

Critique request

Is there more room for improvement? Please tell me anything that comes to mind.

\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

LGTM.


Calling .replace(',', '.') seems a curious way to address Locale concerns.

Wouldn't you prefer to use the decimal format interface to obtain the desired . decimal point? Then a single format string would suffice, instead of three.

Or perhaps the calling environment should be responsible for proper config, rather than this library code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.