2016-11-03 5 views
2

私はいくつかの低レベルのレンダリングのことをやろうとしていますが、私は正しいサイズのすべてを作るために実際のディスプレイDPIを知る必要があります。Javaコードから実際の表示密度(DPI)を見つける方法は?

私はこれを行う方法を見つけました: java.awt.Toolkit.getDefaultToolkit().getScreenResolution() - 実際のDPIの1/2である「網膜」ディスプレイでOS Xで正しくない結果を返します。 (私の場合は220でなければなりませんが、110です)

したがって、他のいくつかのより正確なAPIが利用可能でなければなりません。あるいは、OS X用のハックを実装する必要があります。網膜"。しかし、私はこの情報も照会する方法を見つけることができませんでした。 this answerがありますが、私のマシンではToolkit.getDefaultToolkit().getDesktopProperty("apple.awt.contentScaleFactor")だけがnullを返します。

どうすればいいですか?

答えて

2

現在、java.awt.GraphicsEnvironmentから取得できるようです。最新のJDK(8u112)で動作するコード例を以下に示します。

// find the display device of interest 
final GraphicsDevice defaultScreenDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); 

// on OS X, it would be CGraphicsDevice 
if (defaultScreenDevice instanceof CGraphicsDevice) { 
    final CGraphicsDevice device = (CGraphicsDevice) defaultScreenDevice; 

    // this is the missing correction factor, it's equal to 2 on HiDPI a.k.a. Retina displays 
    final int scaleFactor = device.getScaleFactor(); 

    // now we can compute the real DPI of the screen 
    final double realDPI = scaleFactor * (device.getXResolution() + device.getYResolution())/2; 
} 
関連する問題