Get screen scale in JavaFX using only open API

I am trying to create a JavaFX library displaying an SVG file as an image, work with the whole version of Java 8: https://github.com/codecentric/javafxsvg

The problem is that it uses the renderScale attributes java.fx.Screen, which is private and does not have direct or indirect access in the public API.

In most cases, the value is 1.0f and can be 2.0f to display Retina on a Mac.

com.sun.javafx.tk.quantum.QuantumToolkit.getMaxRenderScale();

This gives the value that I want, but is in the com.sun package, therefore not in the public API, and you can change (and change DO, see renaming below) between lower versions.

com.sun.glass.ui.Screen.getRenderScale();

It is not a public API and was called getScale () in the previous version.

The same renaming for this next accessor, whose purpose was to expose the internal attributes!

com.sun.javafx.stage.ScreenHelper.getScreenAccessor(Screen).getRenderScale();
// was ScreenHelper.getScreenAccessor(Screen).getScale()  in previous version

If I use the abstracts of the internal API, it can work for the latest version: 1.8.0_74, but gives NoSuchMethodError for some previous versions (for example, 1.8.0_51).

How can I make it work for the whole java version of jre?

Please note that I tried a naive approach to calculating it from dpi, but I only need to get a rounded value (1.0 or 2.0), and I do not have a rule for calculating the scale.

+4
source share
1 answer

I wrote for myself this way of help.

public static double getRenderScale(Screen screen) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Method m;
    try {
        m = Screen.class.getDeclaredMethod("getScale"); // until 8u60 b15
    } catch (NoSuchMethodException e) {
        m = Screen.class.getDeclaredMethod("getRenderScale");
    }
    m.setAccessible(true);
    return ((Float) m.invoke(screen)).doubleValue();
}

This is a brute force approach, but I have not found anything better.

+1
source

Source: https://habr.com/ru/post/1629892/


All Articles