Loads a font from the resource directory. - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

Loads a font from the resource directory.

Demo Code


//package com.java2s;

import java.awt.Font;

import java.io.InputStream;

public class Main {
    public static void main(String[] argv) throws Exception {
        String filename = "java2s.com";
        float size = 2.45678f;
        System.out.println(loadFont(filename, size));
    }//from w w w  . j ava  2 s . co  m

    private static String resources = "";
    private static ClassLoader loader;

    /**
     * Loads a font from the resource directory.
     * 
     * @param filename
     *        => The name of the file in the resource directory.
     * @param size
     *        => The size in pixels of the font to load.
     */
    public static Font loadFont(String filename, float size)
            throws Exception {
        InputStream stream = loader.getResource(resources + filename)
                .openStream();
        Font font = Font.createFont(Font.TRUETYPE_FONT, stream);
        return font.deriveFont(size);
    }
}

Related Tutorials