How do I get screen’s display mode information?

This example shows graphics devices display mode information such as the display width, height, refresh rate and bit depth. This information can be obtained from GraphicsDevice.getDisplayMode() method which return an instance of java.awt.DisplayMode.

We can also get the size of a screen using the following example How do I get my screen size?, but this example can handle only a single screen.

package org.kodejava.example.awt;

import java.awt.*;

public class GettingScreenDisplayModeInformation {
    public static void main(String[] args) {
        // Get local graphics environment
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

        GraphicsDevice[] devices = env.getScreenDevices();

        int sequence = 1;
        for (GraphicsDevice device : devices) {
            System.out.println("Screen Number [" + (sequence++) + "]");
            System.out.println("Width       : " + device.getDisplayMode().getWidth());
            System.out.println("Height      : " + device.getDisplayMode().getHeight());
            System.out.println("Refresh Rate: " + device.getDisplayMode().getRefreshRate());
            System.out.println("Bit Depth   : " + device.getDisplayMode().getBitDepth());
            System.out.println("");
        }
    }
}

An example of the program result:

Screen Number [1]
Width       : 1280
Height      : 1024
Refresh Rate: 60
Bit Depth   : 32

Screen Number [2]
Width       : 1280
Height      : 800
Refresh Rate: 60
Bit Depth   : 32

How do I get number of available screens?

This code shows how to get number of available screen devices on the local machine or local graphics environment. We obtain the number of screens by getting the length of array returned by GraphicsEnvironment.getScreenDevices() method.

This process can produce a HeadlessException to happen if we try to run this code on a machine that doesn’t have a screen device with it.

package org.kodejava.example.awt;

import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.HeadlessException;

public class GettingNumberOfScreen {
    public static void main(String[] args) {
        try {
            // Get local graphics environment
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();

            // Returns an array of all of the screen GraphicsDevice objects.
            GraphicsDevice[] devices = env.getScreenDevices();

            int numberOfScreens = devices.length;
            System.out.println("Number of available screens = " + numberOfScreens);
        } catch (HeadlessException e) {
            // We'll get here if no screen devices was found.
            e.printStackTrace();
        }
    }
}

How do I get the available font family names?

package org.kodejava.example.awt;

import java.awt.Font;
import java.awt.GraphicsEnvironment;

public class FontFamilyNameList {
    public static void main(String[] args) {
        // Get all available font family names from GraphicsEnvironment
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String familyNames[] = ge.getAvailableFontFamilyNames();

        // Iterates familyNames array to display the available font's family names
        for (String familyName : familyNames) {
            System.out.println("Family names: " + familyName);
        }        
    }
}