Java Swing Font Metrics getFontMetrics(JComponent c, Graphics g, Font font)

Here you can find the source of getFontMetrics(JComponent c, Graphics g, Font font)

Description

Returns the FontMetrics for the specified Font.

License

Open Source License

Parameter

Parameter Description
c Graphics Graphics
g the g
font Font to get FontMetrics for

Return

the font metrics

Declaration

public static FontMetrics getFontMetrics(JComponent c, Graphics g, Font font) 

Method Source Code

//package com.java2s;
/*//from   ww w.j  av a2  s  .  c o  m
 * Hello Minecraft!.
 * Copyright (C) 2013  huangyuhui <huanghongxun2008@126.com>
 *
 * 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.  If not, see {http://www.gnu.org/licenses/}.
 */

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import java.awt.Toolkit;

import javax.swing.JComponent;

public class Main {
    /**
     * Returns the FontMetrics for the current Font of the passed in Graphics.
     * This method is used when a Graphics is available, typically when
     * painting. If a Graphics is not available the JComponent method of the
     * same name should be used.
     * <p>
     * Callers should pass in a non-null JComponent, the exception to this is if
     * a JComponent is not readily available at the time of painting.
     * <p>
     * This does not necessarily return the FontMetrics from the Graphics.
     *
     * @param c JComponent requesting FontMetrics, may be null
     * @param g Graphics Graphics
     * @return the font metrics
     */
    public static FontMetrics getFontMetrics(JComponent c, Graphics g) {
        return getFontMetrics(c, g, g.getFont());
    }

    /**
     * Returns the FontMetrics for the specified Font. This method is used when
     * a Graphics is available, typically when painting. If a Graphics is not
     * available the JComponent method of the same name should be used.
     * <p>
     * Callers should pass in a non-null JComonent, the exception to this is if
     * a JComponent is not readily available at the time of painting.
     * <p>
     * This does not necessarily return the FontMetrics from the Graphics.
     *
     * @param c Graphics Graphics
     * @param g the g
     * @param font Font to get FontMetrics for
     * @return the font metrics
     */
    public static FontMetrics getFontMetrics(JComponent c, Graphics g, Font font) {
        if (c != null)
            // Note: We assume that we're using the FontMetrics
            // from the widget to layout out text, otherwise we can get
            // mismatches when printing.
            return c.getFontMetrics(font);
        return Toolkit.getDefaultToolkit().getFontMetrics(font);
    }
}

Related

  1. getFontMetrics(JComponent c, Graphics g)
  2. getFontMetrics(JComponent c, Graphics g)
  3. getFontMetrics(JComponent c, Graphics g)
  4. getFontMetrics(JComponent c, Graphics g)
  5. getFontMetrics(JComponent c, Graphics g, Font f)
  6. getFRC(JComponent c, FontMetrics fm)
  7. getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)
  8. getMethodGetFontMetrics()
  9. getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)