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

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

Description

Returns the FontMetrics for the current Font of the passed in Graphics.

License

Open Source License

Parameter

Parameter Description
c JComponent requesting FontMetrics, may be null
g Graphics Graphics

Return

the font metrics

Declaration

public static FontMetrics getFontMetrics(JComponent c, Graphics g) 

Method Source Code


//package com.java2s;
/*/*w ww .  j ava  2  s  . c  om*/
 * Copyright (C) 2015 Jack Jiang(cngeeker.com) The BeautyEye Project. 
 * All rights reserved.
 * Project URL:https://github.com/JackJiang2011/beautyeye
 * Version 3.6
 * 
 * Jack Jiang PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * MySwingUtilities2.java at 2015-2-1 20:25:40, original version by Jack Jiang.
 * You can contact author with jb2011@163.com.
 */

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(final JComponent c, final Graphics g)
  2. getFontMetrics(Font font)
  3. getFontMetrics(Font font)
  4. getFontMetrics(Font font)
  5. getFontMetrics(JComponent c, Graphics g)
  6. getFontMetrics(JComponent c, Graphics g)
  7. getFontMetrics(JComponent c, Graphics g)
  8. getFontMetrics(JComponent c, Graphics g)
  9. getFontMetrics(JComponent c, Graphics g, Font f)