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.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       : 2560
Height      : 1080
Refresh Rate: 60
Bit Depth   : 32

Screen Number [2]
Width       : 2560
Height      : 1080
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.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 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();
        }
    }
}

The output of the code snippet above:

Number of available screens = 2

How do I get the available font family names?

package org.kodejava.awt;

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 name: " + familyName);
        }
    }
}

Some font family names are shown below:

Family name: Agency FB
Family name: Algerian
Family name: Arial
Family name: Arial Black
Family name: Arial Narrow
Family name: Arial Rounded MT Bold
Family name: Bahnschrift
Family name: Baskerville Old Face
Family name: Bauhaus 93
Family name: Bell MT
...

How do I get the available font names?

package org.kodejava.awt;

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

public class FontNameList {
    public static void main(String[] args) {
        // Get all available fonts from GraphicsEnvironment
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = ge.getAllFonts();

        // Iterates all available fonts and get their name and family name
        for (Font font : fonts) {
            String fontName = font.getName();
            String familyName = font.getFamily();

            System.out.println("Font: " + fontName + "; family: " + familyName);
        }
    }
}

Here are some fonts name printed from the code snippet above:

Font: Agency FB; family: Agency FB
Font: Agency FB Bold; family: Agency FB
Font: Algerian; family: Algerian
Font: Arial; family: Arial
Font: Arial Black; family: Arial Black
Font: Arial Bold; family: Arial
Font: Arial Bold Italic; family: Arial
Font: Arial Italic; family: Arial
Font: Arial Narrow; family: Arial Narrow
Font: Arial Narrow Bold; family: Arial Narrow
...