Java Font Text Width getStringWidth(Graphics2D g2d, String info, Font font)

Here you can find the source of getStringWidth(Graphics2D g2d, String info, Font font)

Description

get String Width

License

Open Source License

Declaration

public static int getStringWidth(Graphics2D g2d, String info, Font font) 

Method Source Code

//package com.java2s;
/*/*  ww  w  .ja v  a2  s  .  c o m*/
 * MegaMekLab - Copyright (C) 2008
 *
 * Original author - jtighe (torren@users.sourceforge.net)
 *
 * 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 2 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.
 */

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

public class Main {
    public static int getStringWidth(Graphics2D g2d, String info, Font font) {
        FontMetrics fm = g2d.getFontMetrics(font);
        Rectangle2D rect = fm.getStringBounds(info, g2d);

        return (int) (rect.getWidth());
    }
}

Related

  1. getStringWidth(final Font f, final String s)
  2. getStringWidth(Font font, String text)
  3. getStringWidth(FontMetrics f, String s)
  4. getStringWidth(Graphics g, Font font, String x)
  5. getStringWidth(Graphics2D g2, final String str)
  6. getStringWidth(String loginID, Font font)
  7. getStringWidth(String s, FontMetrics fm)
  8. getStringWidth(String str, java.awt.Font font)
  9. getTextForWidth(double bboxWth, String txt, Font font)