Java Font Text Size getStringDimension(Component comp, String str)

Here you can find the source of getStringDimension(Component comp, String str)

Description

Calculates the dimension of a string

License

Open Source License

Parameter

Parameter Description
comp the component that should display the text
str the str

Return

the dimension

Declaration

public static Dimension getStringDimension(Component comp, String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2010 BREDEX GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  ww  .java2s. c o  m*/
 *     BREDEX GmbH - initial API and implementation and/or initial documentation
 *******************************************************************************/

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;

public class Main {
    /**
     * Calculates the dimension of a string
     * @param comp the component that should display the text
     * @param str the str
     * @return the dimension
     */
    public static Dimension getStringDimension(Component comp, String str) {
        FontMetrics fm = comp.getFontMetrics(comp.getFont());
        return new Dimension(fm.stringWidth(str), fm.getHeight());
    }
}

Related

  1. getSize(Font font)
  2. getStringDimensions(Graphics2D graphics, Font font, String[] stringsByLine)
  3. getStringDisplaySize(String input)
  4. getStringSize(final Graphics g, final Font font, final String text)
  5. getStringSize(Graphics g, String text, Font font)