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 FontMetrics

Declaration

public static FontMetrics getFontMetrics(JComponent c, Graphics g) 

Method Source Code


//package com.java2s;
import java.awt.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.JComponent;

public class Main {
    /**/* ww w.j  a v  a2 s  .  co m*/
     * Holds the public static method {@code SwingUtilities2#getFontMetrics}.
     */
    private static Method getFontMetricsMethod = null;

    /**
     * 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 FontMetrics
     */
    public static FontMetrics getFontMetrics(JComponent c, Graphics g) {
        if (getFontMetricsMethod != null) {
            try {
                return (FontMetrics) getFontMetricsMethod.invoke(null, new Object[] { c, g });
            } catch (IllegalArgumentException e) {
                // Use the fallback
            } catch (IllegalAccessException e) {
                // Use the fallback
            } catch (InvocationTargetException e) {
                // Use the fallback
            }
        }
        return c.getFontMetrics(g.getFont());
    }
}

Related

  1. getFontMetrics(Font font)
  2. getFontMetrics(JComponent c, Graphics g)
  3. getFontMetrics(JComponent c, Graphics g)
  4. getFontMetrics(JComponent c, Graphics g)
  5. getFontMetrics(JComponent c, Graphics g)
  6. getFontMetrics(JComponent c, Graphics g, Font f)
  7. getFontMetrics(JComponent c, Graphics g, Font font)
  8. getFRC(JComponent c, FontMetrics fm)
  9. getImage(String text, boolean clockwise, Font font, FontMetrics fm, Color bg, Color fontColor)