Get Screen Size and Resolution in Java

An example on getting and printing screen size and resolution (dots per pixel) in Java.



Example


import java.awt.*;
class GetScreenSize
{
public static void main(String args[])
{

// Create Toolkit object
Toolkit toolkit=Toolkit.getDefaultToolkit();

// Get width, getScreenSize() returns Dimension & Dimension has width [variable]
System.out.println("The screen width is "+toolkit.getScreenSize().width);

// Get height, getScreenSize() returns Dimension & Dimension has height [variable]
System.out.println("The screen height is "+toolkit.getScreenSize().height);

// getScreenResolution() gives no.of dots per pixel
System.out.println("The screen resolution is "+toolkit.getScreenResolution());

}
}

Output


The screen width is 1024
The screen height is 768
The screen resolution is 96


All the classes, Toolkit, Dimension are present in the java.awt package. Your output may look different depending upon your monitor.

Also see my post on Centering a JDialog on screen and on JFrame

No comments: