1
\$\begingroup\$

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

After taking @J_H's suggestions into account, I ended up here:

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

package com.github.coderodde.javafx.mouseupdaterate;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
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 MouseRefreshRateFinder extends Application {
 /**
 * Screen width in pixels.
 */
 private static final int SCREEN_WIDTH = 800;
 
 /**
 * Screen height in pixels.
 */
 private static final int SCREEN_HEIGHT = 600;
 
 /**
 * Used in {@code DURATION_DECIMAL_FORMAT} and 
 * {@code FREQUENCY_DECIMAL_FORMAT}.
 */
 private static DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS =
 DecimalFormatSymbols.getInstance();
 
 static {
 DECIMAL_FORMAT_SYMBOLS.setDecimalSeparator('.');
 }
 
 /**
 * The decimal formatter for duration.
 */
 private static final DecimalFormat DURATION_DECIMAL_FORMAT =
 new DecimalFormat("0.00", DECIMAL_FORMAT_SYMBOLS);
 /**
 * The decimal formatter for frequencies.
 */
 private static final DecimalFormat FREQUENCY_DECIMAL_FORMAT =
 new DecimalFormat("0.0", DECIMAL_FORMAT_SYMBOLS);
 
 static {
 // Garbage-collect since unused from now on:
 DECIMAL_FORMAT_SYMBOLS = null;
 }
 
 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 = 
 DURATION_DECIMAL_FORMAT.format(durationInSeconds);
 
 String frequencyString = 
 FREQUENCY_DECIMAL_FORMAT
 .format(frameCounter / durationInSeconds);
 
 System.out.printf(
 "%d frames in %s s, refresh rate of %s Hz.%n",
 frameCounter, 
 durationInSecondsString,
 frequencyString);
 }
 }
}

Critique request

As always, I would like to hear about all possible improvements to my program.

asked Aug 29, 2023 at 7:05
\$\endgroup\$
3
  • 1
    \$\begingroup\$ What is the machine locale? This is kind of.. Not in the spirit of JH's suggestion, necessarily. What is the purpose of formatting decimals in a different style from the default locale? \$\endgroup\$ Commented Aug 29, 2023 at 13:55
  • 1
    \$\begingroup\$ Is your machine locale fi_FI? What is the value of Locale.getDefault();? \$\endgroup\$ Commented Aug 29, 2023 at 14:01
  • \$\begingroup\$ @Reinderien I will check my default locale tomorrow. Now tired. \$\endgroup\$ Commented Aug 29, 2023 at 14:32

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.