Java Font Text Width getStringWidth(final Font f, final String s)

Here you can find the source of getStringWidth(final Font f, final String s)

Description

Determines the width of the given string if it were drawn using the specified font.

License

Open Source License

Parameter

Parameter Description
g the font
s the string to determine the width of

Return

the width of the string, in pixels

Declaration

@SuppressWarnings("deprecation")
public static int getStringWidth(final Font f, final String s) 

Method Source Code

//package com.java2s;
/*/*from  ww  w  . j a v  a  2  s  .  c o  m*/
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 */

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

import java.awt.Toolkit;

public class Main {
    /**
     * Determines the width of the given string if it were drawn using the
     * specified graphics context.
     * @param g the graphics context
     * @param s the string to determine the width of
     * @return the width of the string, in pixels
     */
    public static int getStringWidth(final Graphics g, final String s) {
        return getStringWidth(g.getFontMetrics(), s);
    }

    /**
     * Determines the width of the given string if it were drawn using the
     * specified font.
     * @param g the font
     * @param s the string to determine the width of
     * @return the width of the string, in pixels
     */
    @SuppressWarnings("deprecation")
    public static int getStringWidth(final Font f, final String s) {
        return getStringWidth(Toolkit.getDefaultToolkit().getFontMetrics(f), s);
    }

    /**
     * Determines the width of the given string if it were drawn in a font
     * with the specified metrics.
     * @param m the font metrics
     * @param s the string to determine the width of
     * @return the width of the string, in pixels
     */
    public static int getStringWidth(final FontMetrics m, final String s) {
        return m.stringWidth(s);
    }
}

Related

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