Here you can find the source of getStringWidth(final Font f, final String s)
Parameter | Description |
---|---|
g | the font |
s | the string to determine the width of |
@SuppressWarnings("deprecation") public static int getStringWidth(final Font f, final String s)
//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); } }