Java tutorial
//package com.java2s; import java.awt.*; import java.util.Arrays; public class Main { public static int getStringHeight(Graphics g, Font font, String text) { if (font == null || g == null || text == null) { return -1; } FontMetrics fm = g.getFontMetrics(font); if (!text.contains(("\\n"))) { return (int) fm.getStringBounds(text, g).getHeight(); } return Arrays.stream(text.split("\\\\n")).mapToInt(row -> (int) fm.getStringBounds(text, g).getHeight()) .reduce(Integer::sum).getAsInt(); } }