mipa
mipa

Reputation: 10650

How do I find out whether my program is running on a Retina screen

How can I reliably find out whether my JavaFX program is running on a retina device and what pixel scaling ratio is used. Calling something like

System.out.println(Screen.getPrimary().getDpi());

just returns 110 on my Mac. This is exactly half of the actual physical dpis so the information provided by the Screen class is quite useless.

Is there any other way to find out what I am looking for?

Michael

Upvotes: 2

Views: 343

Answers (1)

mipa
mipa

Reputation: 10650

I think I can now answer my own question. Officially there does not seem to be any way to find out what the physical DPIs of your screen are. The only way seems to be to use some private API like in the following example:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class PixelScaleTest extends Application {

    public static double getPixelScale(Screen screen) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        Method m = Screen.class.getDeclaredMethod("getScale");
        m.setAccessible(true);
        return ((Float) m.invoke(screen)).doubleValue();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            System.out.println("PixelScale: " + getPixelScale(Screen.getPrimary()));
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
                | SecurityException e) {
            e.printStackTrace();
        }
        Platform.exit();
    }

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

}

With the pixel scale factor you can compute your physical DPIs. On a standard Screen this factor should be 1.0 and on a Mac Retina it should be 2.0.

Upvotes: 2

Related Questions