Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the screen DPI in java?

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

like image 821
coder Avatar asked Jul 01 '11 06:07

coder


People also ask

How to get DPI in Java?

getWidth()/pixelPerInch; double x=Math. pow(height,2); double y=Math. pow(width,2);

How to get Java window size?

The following Java code demonstrates how to get the screen size, using the Java Toolkit class: // java - get screen size using the Toolkit class Dimension screenSize = Toolkit. getDefaultToolkit(). getScreenSize();

How to get screen resolution in Java?

Screen resolution can be obtained through a Java program by using Toolkit.getScreenSize () method. getScreenSize () is a method of the java Toolkit class of java.awt package. It gets the size of the screen. On systems with multiple displays, the primary display screen resolution is returned. getScreenSize will return the size in pixel.

How to get screen DPI programatically in Android?

This example demonstrates how do I get screen DPI programatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How do I change the DPI of a Java program?

Find java.exe and/or javaw.exe likely found in C:\Program Files\Java\jre (version#)\bin. Check Override high DPI scaling behavior. Choose System for Scaling performed by: Launch your java app and tada!

How do I get the screen size of a given object?

Answer: You can determine the screen resolution (screen size) using the Toolkit class. This method call returns the screen resolution in pixels, and stores the results in a Dimension object, as shown here: Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();


1 Answers

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.

like image 180
Barry Brown Avatar answered Sep 25 '22 15:09

Barry Brown