Java Font Text Size getStringSize(Graphics g, String text, Font font)

Here you can find the source of getStringSize(Graphics g, String text, Font font)

Description

get String Size

License

LGPL

Declaration

public static Dimension getStringSize(Graphics g, String text, Font font) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.Dimension;

import java.awt.Font;
import java.awt.FontMetrics;

import java.awt.Graphics;

public class Main {

    public static Dimension getStringSize(Graphics g, String text, Font font) {
        if (text == null || text.isEmpty())
            return new Dimension();
        FontMetrics fm;/*www  . j a va2 s .com*/
        if (font == null) {
            fm = g.getFontMetrics();
        } else {
            fm = g.getFontMetrics(font);
        }
        int w = fm.stringWidth(text);
        int h = Math.max(fm.getHeight(), fm.getAscent() + fm.getDescent());
        return new Dimension(w, h);
    }
}

Related

  1. getSize(Font font)
  2. getStringDimension(Component comp, String str)
  3. getStringDimensions(Graphics2D graphics, Font font, String[] stringsByLine)
  4. getStringDisplaySize(String input)
  5. getStringSize(final Graphics g, final Font font, final String text)
  6. getStringSize(String textToMeasure, Font displayFont)
  7. getStringSizeForFont(final String string, final Font font)