Java Font from System getDefaultFontRenderContext()

Here you can find the source of getDefaultFontRenderContext()

Description

Gets the FontRenderContext for the default screen-device.

License

LGPL

Return

FontRenderContext a FontRenderContext for the default screen.

Declaration

public static synchronized FontRenderContext getDefaultFontRenderContext() 

Method Source Code

//package com.java2s;
/********************************************************************************
 *                                                                              *
 *  (c) Copyright 2010 Verizon Communications USA and The Open University UK    *
 *                                                                              *
 *  This software is freely distributed in accordance with                      *
 *  the GNU Lesser General Public (LGPL) license, version 3 or later            *
 *  as published by the Free Software Foundation.                               *
 *  For details see LGPL: http://www.fsf.org/licensing/licenses/lgpl.html       *
 *               and GPL: http://www.fsf.org/licensing/licenses/gpl-3.0.html    *
 *                                                                              *
 *  This software is provided by the copyright holders and contributors "as is" *
 *  and any express or implied warranties, including, but not limited to, the   *
 *  implied warranties of merchantability and fitness for a particular purpose  *
 *  are disclaimed. In no event shall the copyright owner or contributors be    *
 *  liable for any direct, indirect, incidental, special, exemplary, or         *
 *  consequential damages (including, but not limited to, procurement of        *
 *  substitute goods or services; loss of use, data, or profits; or business    *
 *  interruption) however caused and on any theory of liability, whether in     *
 *  contract, strict liability, or tort (including negligence or otherwise)     *
 *  arising in any way out of the use of this software, even if advised of the  *
 *  possibility of such damage.                                                 *
 *                                                                              *
 ********************************************************************************/

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import java.awt.font.FontRenderContext;

import java.awt.image.BufferedImage;

public class Main {
    /** A FontRenderContext that can be used to measure Strings */
    private static FontRenderContext frc = null;

    /**//from   www.  jav a 2 s  . c o  m
     * Gets the FontRenderContext for the default screen-device.
     * The FontRenderContext returned here, can be used to measure
     * for instance the visual size of Strings that are to be painted by a component
     * that is not realized yet. 
     * The FontRenderContext is mapped to the defaultScreenDevice 
     * of the JVM. The returned here FontRenderContext can only to be used for 
     * components that are painted onto the screen if you need a FontRenderContex
     * for printing this will probably not work. 
     * For performance reasons we return only a reference
     * to a static FontRenderContext-object that is created in this class.
     * You should take care NOT to set the object returned here to null.
     * If you do this method will have to create a new one next time it is called.  
     * So just use the returned FontRenderContext for obtaining the 
     * metrics of a font and than leave it alone.
     * @author FahleE (Sun Developer Forum)
     * @return FontRenderContext a FontRenderContext for the 
     * default screen.
     */
    public static synchronized FontRenderContext getDefaultFontRenderContext() {
        if (frc == null)
            createFontRenderContext();
        return frc;
    }

    /**
     * This method returns the FontRenderContext
     * for the default Screen-device.
     * @author FahleE (Sun Developer Forum)
     * @return FontRenderContext the FontRenderContext that is used for the screen-device
     */
    private static void createFontRenderContext() {
        GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage bi = gc.createCompatibleImage(1, 1); //we need at least one pixel
        Graphics2D g2 = bi.createGraphics();
        frc = g2.getFontRenderContext();
    }

    /**
     * Get the current GraphicsConfiguration for this machine.
     * @author DrLaszloJamf (Sun Developer Forum)
     * @return GraphicsConfiguration the current graphics configuration.
     */
    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
}

Related

  1. getDefaultBorderInsets(int fontSize)
  2. getDefaultFont()
  3. getDefaultFont()
  4. getDefaultFont()
  5. getDefaultFont()
  6. getDefaultFrc()
  7. getDefaultMapXMLCreatorFontName()
  8. getDefaultMonoFontSize()
  9. getDefaultSansSerifFont()