8

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

brimborium
  • 9,362
  • 9
  • 48
  • 76
coder
  • 823
  • 2
  • 10
  • 17
  • possible duplicate of [How to "correctly" detect DPI of display with Java?](http://stackoverflow.com/questions/4707756/how-to-correctly-detect-dpi-of-display-with-java) – uckelman Oct 03 '11 at 11:10

2 Answers2

6

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.

Barry Brown
  • 20,233
  • 15
  • 69
  • 105
  • okay thanx.. Then what shall I do if I want to adjust my Frame window size according to the screen width and resolution.. Suppose I want my Frame window to be 5 inches wide whatever be my screen resolution, what shall I do?? – coder Jul 01 '11 at 07:05
  • 1
    You're kinda stuck. There's no good solution, currently. You can beg the user to go through the DPI calibration in the Display control panel (Windows only); it takes just a moment. But you can't rely on the user to do it. Check again in a few years if/when Apple ever gets their resolution-independece solution working. – Barry Brown Jul 01 '11 at 07:13
  • Something I have desired too espcially for touch screen apps. Afraid the pipeline isn't there for monitors to report their size to the OS and then have the OS provide it to java. Of course really not easy with things like projectors and multiple displays with a multi-monitor desktop rendering area, so Alas one has to rely on a configuration file or user input. – peterk Feb 07 '14 at 04:10
  • Is GraphicsConfiguration.getNormalizingTransform() not usable in some way? It seems to be a transform which converts between PostScript dimensions and the display dimensions. – Hakanai Feb 21 '14 at 08:50
-2

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

deviant
  • 3,539
  • 4
  • 32
  • 47
  • Where did you get "Screen" class? Is it from some additional dependency? I don't see it on Java 1.8 – Sergey Karpushin Apr 19 '18 at 22:56
  • 1
    This won't work when Windows HiDPI scaling is on, as `Toolkit.getScreenSize()` will return the size in "virtual" pixels, **not** screen pixels. The only reliable way to determine the size of the screen nowadays is via the `java.awt.GraphicsConfiguration#getBounds()` call. – Bass Dec 27 '18 at 11:31
  • 1
    What is wrong with `javafx.stage.Screen.getPrimary().getDpi()`? – smac89 Aug 01 '19 at 19:36
  • 2
    @Bass Actually `GraphicsConfiguration#getBounds()` is also affected by the HiDPI scaling. With Java 8 a 1650x1080 monitor appears as a 1120x700 rectangle when the scaling is set to 150%. – Emmanuel Bourg Nov 13 '19 at 16:03