Here you can find the source of getStringDisplaySize(String input)
public static int getStringDisplaySize(String input)
//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); } }