Java Utililty Methods FontMetrics

List of utility methods to do FontMetrics

Description

The list of methods to do FontMetrics are organized into topic(s).

Method

intgetMaxFontHeight(final java.awt.Font font)
Gets the maximum font height used by of ALL characters, as recorded by the font.
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
FontMetrics metrics = g.getFontMetrics(font);
int height = metrics.getMaxAscent() + metrics.getMaxDescent();
g.dispose();
return height;
ListgetMonospacedFontNames()
get Monospaced Font Names
List<String> monospaceFontFamilyNames = new ArrayList<String>();
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontFamilyNames = graphicsEnvironment.getAvailableFontFamilyNames();
BufferedImage bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = bufferedImage.createGraphics();
for (String fontFamilyName : fontFamilyNames) {
    boolean isMonospaced = true;
    int fontStyle = Font.PLAIN;
...
String[][]getMonospacedFontsFamillyName()
get Monospaced Fonts Familly Name
String[] fontFamilly = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
List<String> mono = new ArrayList();
List<String> notMono = new ArrayList();
for (int i = 0; i < fontFamilly.length; i++) {
    if (isMonospaced(fontFamilly[i])) {
        mono.add(fontFamilly[i]);
    } else {
        notMono.add(fontFamilly[i]);
...
DimensiongetPrefSize(FontMetrics fm, String keyTip)
get Pref Size
int prefWidth = fm.stringWidth(keyTip) + 2 * INSETS + 1;
int prefHeight = fm.getHeight() + INSETS - 1;
return new Dimension(prefWidth, prefHeight);
StringgetStringForMaxWidth(FontMetrics fm, String s, int maxWidth)
Returns a string that fit into the maximum width based on the parameter string.
boolean changed = false;
for (int nameWidth = fm.stringWidth(s); nameWidth > maxWidth; nameWidth = fm.stringWidth(s.concat("..."))) {
    s = s.substring(0, s.length() - 1);
    changed = true;
if (changed) {
    return s.concat("...");
return s;
BufferedImagegetStringImage(Font font, String... strs)
get String Image
BufferedImage image = new BufferedImage(1, 1, 6);
Graphics graphics = image.getGraphics();
graphics.setFont(font);
FontRenderContext fontRenderContext = graphics.getFontMetrics().getFontRenderContext();
Rectangle2D rectangle2D = font.getStringBounds(getStringArrayMaxLengthString(strs), fontRenderContext);
graphics.dispose();
int width = (int) Math.ceil(rectangle2D.getWidth());
int height = 0;
...
intgetStringLengthInPixels(String s, Graphics g)
get String Length In Pixels
return Toolkit.getDefaultToolkit().getFontMetrics(g.getFont()).stringWidth(s);
RectanglegetStringRect(Font f, String s)
get String Rect
Graphics g = image.createGraphics();
Rectangle r = g.getFontMetrics(f).getStringBounds(s, g).getBounds();
g.dispose();
return r;
PointgetTextCenterShear(final FontMetrics fm, final String text)
Returns shear to center text
return new Point(getTextCenterShearX(fm, text), getTextCenterShearY(fm));
intgetTextCenterShearX(final FontMetrics fm, final String text)
get Text Center Shear X
return -fm.stringWidth(text) / 2;