awt

Retrieve display device info

In this example we are going to see how to retrieve information about the display of your device. You can use this in your application in order to present to the user all the information he needs to know about the display he is working on.

Basically to retrieve display information for your device, you should follow these steps:

  • Use GraphicsEnvironment.getLocalGraphicsEnvironment() to get the graphics information of your device.
  • Use GraphicsEnvironment.getScreenDevices to get the screen devices of your system. This will return you an array of GraphicsDevice.
  • You can now iterate through the array and use graphicsDevices[i].getIDstring() to get the id of each display device.
  • Use graphicsDevices[i].getDisplayModes() to get the display modes of this specific device in the array. This will return you an rray of DisplayMode objects concerning this specific device.
  • You can iterate through that array and use displayModes[j].getWidth(), displayModes[j].getHeight(), displayModes[j].getBitDepth(), displayModes[j].getRefreshRate(), to get all the crucial information about the device.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

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

public class DisplayInfo {

  public static void main(String[] args) {

    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();

    for (int i=0; i<graphicsDevices.length; i++) {

  System.out.println("Graphics device " + graphicsDevices[i].getIDstring());

  DisplayMode[] displayModes = graphicsDevices[i].getDisplayModes();

  for (int j=0; j<displayModes.length; j++) {

    int screenWidth = displayModes[j].getWidth();

    int screenHeight = displayModes[j].getHeight();

    int bitDepth = displayModes[j].getBitDepth();

    int refreshRate = displayModes[j].getRefreshRate();

    System.out.println("Display mode : " + j + "nScreen Width : "+ screenWidth + "nScreen Height : " + screenHeight + "nBitDepth : " + (bitDepth==DisplayMode.BIT_DEPTH_MULTI?"Multi":bitDepth) + "nRefresh rate : " + (refreshRate==DisplayMode.REFRESH_RATE_UNKNOWN?"Unknown":refreshRate) + "n");

  }
    }

  }

}

Example Output:

Graphics device :0.0
Display mode : 0
Screen Width : 1280
Screen Height : 1024
BitDepth : Multi
Refresh rate : Unknown

Graphics device :0.1
Display mode : 0
Screen Width : 1280
Screen Height : 800
BitDepth : Multi
Refresh rate : Unknown

 
This was an example on how to retrieve display device information.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button