Java Font Text Size getStringDisplaySize(String input)

Here you can find the source of getStringDisplaySize(String input)

Description

get String Display Size

License

Open Source License

Declaration

public static int getStringDisplaySize(String input) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Sebastian Hagedorn <sh@sernet.de>.
 * This program is free software: you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 *     This program 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.  
 * See the GNU General Public License for more details.
 *     You should have received a copy of the GNU General Public 
 * License along with this program. /* www .  j a  v a  2  s  .c  o  m*/
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Sebastian Hagedorn <sh@sernet.de> - initial API and implementation
 ******************************************************************************/

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;

public class Main {
    public static int getStringDisplaySize(String input) {
        GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        BufferedImage bi = new BufferedImage(1, 1,
                BufferedImage.TYPE_INT_RGB);
        Font font = new Font("Arial", Font.PLAIN, 10);
        FontMetrics fm = bi.getGraphics().getFontMetrics(font);
        return fm.stringWidth(input);
    }
}

Related

  1. getSize(Font font)
  2. getStringDimension(Component comp, String str)
  3. getStringDimensions(Graphics2D graphics, Font font, String[] stringsByLine)
  4. getStringSize(final Graphics g, final Font font, final String text)
  5. getStringSize(Graphics g, String text, Font font)
  6. getStringSize(String textToMeasure, Font displayFont)
  7. getStringSizeForFont(final String string, final Font font)