Java图形实用工具类

・2 分钟阅读

这个是一个小型Java图形实用程序类,目前我只关心显示器/显示器尺寸,特别是当计算机系统有多个显示器时。






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();
 }

}

这就是今天的工作,只是一个小的源代码共享。

Xn_warm profile image