Java Utililty Methods Swing Font Metrics

List of utility methods to do Swing Font Metrics

Description

The list of methods to do Swing Font Metrics are organized into topic(s).

Method

FontMetricsgetFontMetrics(JComponent c, Graphics g)
Returns the FontMetrics for the current Font of the passed in Graphics.
if (getFontMetricsMethod != null) {
    try {
        return (FontMetrics) getFontMetricsMethod.invoke(null, new Object[] { c, g });
    } catch (IllegalArgumentException e) {
    } catch (IllegalAccessException e) {
    } catch (InvocationTargetException e) {
return c.getFontMetrics(g.getFont());
FontMetricsgetFontMetrics(JComponent c, Graphics g, Font f)
get Font Metrics
FontMetrics fm = null;
if (getJavaVersion() >= 1.6) {
    try {
        Class swingUtilities2Class = Class.forName("sun.swing.SwingUtilities2");
        Class classParams[] = { JComponent.class, Graphics.class, Font.class };
        Method m = swingUtilities2Class.getMethod("getFontMetrics", classParams);
        Object methodParams[] = { c, g, f };
        fm = (FontMetrics) m.invoke(null, methodParams);
...
FontMetricsgetFontMetrics(JComponent c, Graphics g, Font font)
Returns the FontMetrics for the specified Font.
if (c != null)
    return c.getFontMetrics(font);
return Toolkit.getDefaultToolkit().getFontMetrics(font);
FontRenderContextgetFRC(JComponent c, FontMetrics fm)
Returns the FontRenderContext for the passed in FontMetrics or for the passed in JComponent if FontMetrics is null
if (fm == null && c != null) {
    return getFRC(c, c.getFontMetrics(c.getFont()));
return null;
BufferedImagegetImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)
get Image
BufferedImage bi = null;
int width = SwingUtilities.computeStringWidth(fm, text);
int height = fm.getHeight();
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setColor(bg);
g2.fillRect(0, 0, width, height);
g2.setFont(font);
...
MethodgetMethodGetFontMetrics()
get Method Get Font Metrics
try {
    Class clazz = Class.forName(SWING_UTILITIES2_NAME);
    return clazz.getMethod("getFontMetrics", new Class[] { JComponent.class, Graphics.class });
} catch (ClassNotFoundException e) {
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
return null;
...
intgetTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
Determines the relative offset into the given text that best represents the given span in the view coordinate system.
return getTabbedTextOffset(s, metrics, x0, x, e, startOffset, true);
intgetTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset)
Determines the width of the given segment of text taking tabs into consideration.
int nextX = x;
char[] txt = s.array;
int txtOffset = s.offset;
int n = s.offset + s.count;
for (int i = txtOffset; i < n; i++) {
    if (txt[i] == '\t') {
        if (e != null) {
            nextX = (int) e.nextTabStop((float) nextX, startOffset + i - txtOffset);
...
Rectangle2DgetTextBounds(FontMetrics fm, String s)
Get bounds of a multi-line text
if (s.indexOf('\n') >= 0) {
    String[] a = s.split("\n");
    int fh = fm.getFont().getSize();
    int lh = (int) fh + 1;
    double h = lh * a.length;
    double w = 0;
    for (int ai = 0; ai < a.length; ai++) {
        int tw = SwingUtilities.computeStringWidth(fm, a[ai]);
...
intgetWidthOfText(FontMetrics fontMetrics, String text)
get Width Of Text
return SwingUtilities.computeStringWidth(fontMetrics, text);