How to get the screen DPI in java?

前端 未结 2 2084
离开以前
离开以前 2020-12-03 23:06

I am developing an app for which I need the screen DPI.. I checked a few forums and got a code snippet which goes as follows:

Dimension screen = java.awt.Too         


        
相关标签:
2条回答
  • 2020-12-03 23:17

    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

    0 讨论(0)
  • 2020-12-03 23:18

    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.

    0 讨论(0)
提交回复
热议问题