Creates (in a rather clumsy way) the font metrics for a given Font . - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

Creates (in a rather clumsy way) the font metrics for a given Font .

Demo Code

/*//from   w ww.j  a v  a2 s.  com
 * OpenBench LogicSniffer / SUMP project 
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * Copyright (C) 2006-2010 Michael Poppitz, www.sump.org
 * Copyright (C) 2010 J.W. Janssen, www.lxtreme.nl
 */
//package com.java2s;
import java.awt.*;

import java.awt.image.*;

public class Main {
    /**
     * Creates (in a rather clumsy way) the font metrics for a given {@link Font}.
     * 
     * @param aFont
     *          the font instance to create the font metrics for, cannot be
     *          <code>null</code>.
     * @return a font metrics, never <code>null</code>.
     */
    private static FontMetrics createFontMetrics(final Font aFont) {
        BufferedImage img = new BufferedImage(1, 1,
                BufferedImage.TYPE_INT_ARGB);
        Graphics canvas = img.getGraphics();

        try {
            return canvas.getFontMetrics(aFont);
        } finally {
            canvas.dispose();
            canvas = null;
            img = null;
        }
    }
}

Related Tutorials