Java Font Text Width stringWidth(Graphics g, String s)

Here you can find the source of stringWidth(Graphics g, String s)

Description

Returns the width of string in the current Graphics

License

Open Source License

Declaration

public static int stringWidth(Graphics g, String s) 

Method Source Code


//package com.java2s;
/* Mesquite source code.  Copyright 1997 and onward, W. Maddison and D. Maddison. 
    //ww w. ja  v  a 2  s.c o m
    
Disclaimer:  The Mesquite source code is lengthy and we are few.  There are no doubt inefficiencies and goofs in this code. 
The commenting leaves much to be desired. Please approach this source code with the spirit of helping out.
Perhaps with your help we can be more than a few, and make Mesquite better.
    
Mesquite is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.
Mesquite's web site is http://mesquiteproject.org
    
This source code and its compiled class files are free and modifiable under the terms of 
GNU Lesser General Public License.  (http://www.gnu.org/copyleft/lesser.html)
 */

import java.awt.*;

public class Main {
    /** Returns the width of string in the current Graphics */
    public static int stringWidth(Graphics g, String s) {
        FontMetrics fm = g.getFontMetrics(g.getFont());
        if (fm == null)
            return -1;
        return fm.stringWidth(s);
    }
}

Related

  1. getWidthOfDots(FontMetrics fontMetrics)
  2. setFixedWidthFont(Component comp)
  3. shrinkFontForWidth(Font base, String text, int desiredWidth, Graphics getMetrics)
  4. stringPixelWidth(String text, FontMetrics fm)
  5. stringWidth(final FontMetrics fm, final String string)
  6. stringWidth(Graphics2D g, String s)
  7. stringWidth(java.awt.Font font, String string)
  8. stringWidth(String text, Font font)
  9. wrap(String str, FontMetrics fm, int maxWidth)