Showing posts with label awt. Show all posts
Showing posts with label awt. Show all posts

Thursday, 21 October 2021

Java: Get the screen resolution in dots-per-inch.

Toolkit#getScreenResolution method return the screen resolution in dots-per-inch.

 

ScreenResolutionDotsPerInchDemo.java

package com.sample.app.awtprograms;

import java.awt.Toolkit;

public class ScreenResolutionDotsPerInchDemo {

	public static void main(String args[]) {
		int screenResolution = Toolkit.getDefaultToolkit().getScreenResolution();

		System.out.println("screen resolution in dots-per-inch : " + screenResolution);

	}

}

 

Output

screen resolution in dots-per-inch : 129

  

Previous                                                    Next                                                    Home

Java: Get screen resolution of all the monitors in a multi monitor setup

 


 

Approach 1: Using GraphicsEnvironment

Step 1: Get the instance of GraphicsEnvironment.

GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

 

Step 2: Get all the GraphicsDevice objects in a multi-monitor setup

GraphicsDevice[] graphicsDevice = graphicsEnvironment.getScreenDevices();

  Step 3: Iterate through all the graphic devices and print width and height of the screen.

 

for (int i = 0; i < graphicsDevice.length; i++) {
	DisplayMode displayMode = graphicsDevice[i].getDisplayMode();

	double width = displayMode.getWidth();
	double height = displayMode.getHeight();

	System.out.println("Screen width : " + width);
	System.out.println("Screen height : " + height);
}

Find the below working application.

 

MultiMonitorsScreenResolutionDemo.java

package com.sample.app.awtprograms;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

public class MultiMonitorsScreenResolutionDemo {
	public static void main(String args[]) {
		GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice[] graphicsDevice = graphicsEnvironment.getScreenDevices();

		for (int i = 0; i < graphicsDevice.length; i++) {
			DisplayMode displayMode = graphicsDevice[i].getDisplayMode();

			double width = displayMode.getWidth();
			double height = displayMode.getHeight();

			System.out.println("Screen width : " + width);
			System.out.println("Screen height : " + height);
		}
	}
}


Approach 2: Using GraphicsDevice.

 

Step 1: Get all the screen device objects.

GraphicsDevice devices[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();


Step 2: Iterate over the GraphicDevice objects and print width and height of the screen. 

for (GraphicsDevice graphicDevice : devices) {
	GraphicsConfiguration graphicsConfiguration = graphicDevice.getDefaultConfiguration();
	Rectangle rectangle = graphicsConfiguration.getBounds();

	double width = rectangle.getWidth();
	double height = rectangle.getHeight();
	System.out.println("Screen width : " + width);
	System.out.println("Screen height : " + height);

}

 

MultiMonitorsScreenResolutionDemo2.java

package com.sample.app.awtprograms;

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;

public class MultiMonitorsScreenResolutionDemo2 {
	public static void main(String args[]) {
		GraphicsDevice devices[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

		for (GraphicsDevice graphicDevice : devices) {
			GraphicsConfiguration graphicsConfiguration = graphicDevice.getDefaultConfiguration();
			Rectangle rectangle = graphicsConfiguration.getBounds();

			double width = rectangle.getWidth();
			double height = rectangle.getHeight();
			System.out.println("Screen width : " + width);
			System.out.println("Screen height : " + height);

		}

	}
}

 

 

 


 

Previous                                                    Next                                                    Home

Java: Find the screen resolution (width x height) in pixels

In this post, I am going to explain multiple ways to find the screen resolution programmatically.


 

Approach 1: Using Toolkit#getScreenSize method.

Toolkit#getScreenSize method return the size of the screen. In case of systems with multiple displayes, this method considers the dimensions of primary display.

 

ScreenResolutionDemo1.java

package com.sample.app.awtprograms;

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

public class ScreenResolutionDemo1 {

	public static void main(String args[]) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		double width = screenSize.getWidth();
		double height = screenSize.getHeight();
		
		System.out.println("Screen width : " + width);
		System.out.println("Screen height : " + height);
	}

}

Output

Screen width : 1680.0
Screen height : 1050.0


Note

Even though ‘Toolkit.getScreenSize()’ method documentation says that it returns the screen size of the primary monitor on multi-monitor systems. But this is not working as expected on multi-monitor system. Refer this bug (https://bugs.java.com/bugdatabase/view_bug.do?bug_id=5100801) for more information.

 

If you also have this problem, use below snippet to find the dimensions of a primary display in a multi-monitor system.

 

ScreenResolutionDemo2.java

package com.sample.app.awtprograms;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

public class ScreenResolutionDemo2 {
	public static void main(String args[]) {
		GraphicsDevice graphicDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

		DisplayMode displayMode = graphicDevice.getDisplayMode();

		double width = displayMode.getWidth();
		double height = displayMode.getHeight();

		System.out.println("Screen width : " + width);
		System.out.println("Screen height : " + height);

	}

}


Output

Screen width : 1680.0
Screen height : 1050.0


 

Previous                                                    Next                                                    Home