我正在开发一个需要屏幕 DPI 的应用程序。我查看了几个论坛并获得了如下代码片段:
Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
System.out.println("screen width: "+screen.getWidth());
System.out.println("screen height: "+screen.getHeight());
int pixelPerInch=java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println("pixelPerInch: "+pixelPerInch);
double height=screen.getHeight()/pixelPerInch;
double width=screen.getWidth()/pixelPerInch;
double x=Math.pow(height,2);
double y=Math.pow(width,2);
但是无论我的屏幕分辨率是什么值,pixelPerInch
值都保持在 96。代码有什么问题??
我得到了另一个相同的 swt
代码,如下所示:
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Display Device");
createContents(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private void createContents(Shell shell) {
Device device = shell.getDisplay();
System.out.println("getBounds(): "+ device.getBounds());
System.out.println("getClientArea(): "+device.getClientArea());
System.out.println("getDepth(): "+device.getDepth());
System.out.println("getDPI(): "+device.getDPI());
device.setWarnings(true);
System.out.println("Warnings supported: "+device.getWarnings());
}
public static void main(String[] args) {
new MainClass().run();
}
但是无论我的屏幕分辨率如何,getDPI()
都会返回相同的值 96.. 哪里出了问题?是我的代码有误还是我以错误的方式解释它??
最佳答案
问题是没有人知道屏幕的确切物理尺寸,甚至操作系统也不知道。您需要知道这一点才能计算 PPI。
控制面板中有一个显示设置,用户可以在其中手动指定 PPI,默认设置为 96。
关于java - 如何在java中获取屏幕DPI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6544510/