Thursday 21 October 2021

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

No comments:

Post a Comment