Skip to main content

Getting Computer Monitor Resolution in Java

You can use Java's java.awt.Toolkit class to get the computer monitor resolution. Specifically, you can use Toolkit.getDefaultToolkit().getScreenSize() to get the screen dimensions, or use Toolkit.getDefaultToolkit().getScreenResolution() to get the screen resolution (in pixels per inch).

Here's an example code to read the computer monitor resolution:

import java.awt.Dimension;
import java.awt.Toolkit;

public class ScreenResolutionExample {
public static void main(String[] args) {
// 获取屏幕的尺寸
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
System.out.println("Screen size: " + screenWidth + " x " + screenHeight);

// 获取屏幕的分辨率
int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();
System.out.println("Screen resolution: " + screenResolution + " dpi");
}
}

The output will be similar to:

Screen size: 1920 x 1080
Screen resolution: 96 dpi

Where screenWidth and screenHeight are the screen width and height (in pixels), and screenResolution is the screen resolution.