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; /**/*from w w w. j a va 2 s . c o m*/ * @(#)PaletteUtilities.java * * Copyright (c) 2008 by the original authors of JHotDraw and all its * contributors. All rights reserved. * * You may not use, copy or modify this file, except in compliance with the * license agreement you entered into with the copyright holders. For details * see accompanying license terms. */ import java.awt.*; 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 */ 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 JComponent requesting FontMetrics, may be null * @param g Graphics * @param font Font to get FontMetrics for */ @SuppressWarnings("deprecation") 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); } }