Here you can find the source of getFontMetrics(JComponent c, Graphics g)
Parameter | Description |
---|---|
c | JComponent requesting FontMetrics, may be null |
g | Graphics Graphics |
public static FontMetrics getFontMetrics(JComponent c, Graphics g)
//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()); } }