如何在java中获取屏幕DPI? [英] How to get the screen DPI in java?

查看:850
本文介绍了如何在java中获取屏幕DPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个我需要屏幕DPI的应用程序..我查了几个论坛并得到了一个代码片段如下:

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.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处保持不变。代码有什么问题??

But whatever be the value of my screen resolution, the pixelPerInch value remains the same at 96. What is the problem with the code??

我得到另一个 swt 相同的代码,如下所示:

I got another swt code for the same thing which goes as follows:

  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 ..出了什么问题?我的代码是错误的还是我以错误的方式解释它?

But again here also whatever be my screen resolution, the getDPI() returns the same value of 96.. What is going wrong?? Is my code wrong or am I interpreting it in a wrong way??

推荐答案

问题是没有人,甚至不是操作系统,知道屏幕的确切物理尺寸。您需要知道为了计算PPI。

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.

控制面板中有一个显示设置,用户可以手动指定PPI,默认设置为PPI到96。

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.

这篇关于如何在java中获取屏幕DPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆