A Java graphics utilities class

As a quick note, here’s a little Java graphics utilities class I started putting together today. Mostly I’m just concerned with monitor/display sizes at the moment, especially when a computer system has multiple displays.


import java.awt.*;

public class GraphicsUtils {

    /**
     * get the width and height of the current monitor.
     * this method does NOT working properly with multiple displays
     * on macOS systems.
     */
    public static String getWidthHeightString() {
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();
        return "width = " + width + ", height = " + height;
    }

    /**
     * get a list of all monitor/display sizes on the current system.
     * this seems to work properly with multiple monitors on macOS.
     */
    public static String getMonitorSizes() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[]    gs = ge.getScreenDevices();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < gs.length; i++) {
            DisplayMode dm = gs[i].getDisplayMode();
            sb.append(i + ", width: " + dm.getWidth() + ", height: " + dm.getHeight() + "\n");
        }
        return sb.toString();
    }

}

That’s it for today, just a little source code sharing.