Java Font Text Width getStringWidth(Font font, String text)

Here you can find the source of getStringWidth(Font font, String text)

Description

Gets the string width.

License

Open Source License

Parameter

Parameter Description
font the font
text the text

Return

the string width

Declaration

public static int getStringWidth(Font font, String text) 

Method Source Code


//package com.java2s;
import java.awt.Font;
import java.awt.Graphics2D;

public class Main {
    /** The dummy gfx. */
    private static Graphics2D dummyGFX;

    /**/*w  w  w  . ja  va2  s  .c  om*/
     * Gets the string width.
     *
     * @param font
     *            the font
     * @param text
     *            the text
     * @return the string width
     */
    public static int getStringWidth(Font font, String text) {
        dummyGFX.setFont(font);
        return dummyGFX.getFontMetrics().stringWidth(text);
    }
}

Related

  1. cutTextToWidth(String text, int spaceForText, Font font, Graphics2D graphics, String suffix)
  2. findStringLimit(String aString, Font aFont, int aWidth)
  3. findWidth(FontMetrics metrics, char[] chars, int off, int len, float maxWidth, float[] outWidth)
  4. fontWidth(final Graphics graphics, final Font font, final String str)
  5. getStringWidth(final Font f, final String s)
  6. getStringWidth(FontMetrics f, String s)
  7. getStringWidth(Graphics g, Font font, String x)
  8. getStringWidth(Graphics2D g2, final String str)
  9. getStringWidth(Graphics2D g2d, String info, Font font)