How to get the screen DPI in java?

一世执手 提交于 2019-11-27 15:39:14

The problem is no one, not even the OS, knows the exact physical dimensions of the screen. You'd need to know that in order to calculate the PPI.

There's a display setting in the control panel where the user can manually specify the PPI and by default it's set to 96.

this code works for me on win10

import javafx.stage.Screen 

double getScaleFactor() {
        double trueHorizontalLines = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
        double scaledHorizontalLines = Screen.getPrimary().getBounds().getHeight();
        double dpiScaleFactor = trueHorizontalLines / scaledHorizontalLines;
        return dpiScaleFactor;
    }

it uses some awt apis though

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!