java获取系统dpi,如何在java中获取屏幕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);

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

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();

}

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??

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值