Returns Font which represents the same font, but with size size . - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

Returns Font which represents the same font, but with size size .

Demo Code


//package com.java2s;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    private static Map<String, List<Font>> fonts = new HashMap<>();

    /**// w  ww.  j a va2  s.c  o m
     * Returns {@link Font} which represents the same font as {@code inFont}, but with size {@code size}.
     * Doesn't create new {@code Font} if can.
     *
     * @return required font
     * @param size size of new font
     * @param inFont old font
     * @see Font#deriveFont(float)
     */
    public static Font getFontWithSize(int size, Font inFont) {
        if (fonts.containsKey(inFont.getName())) {
            for (Font font : fonts.get(inFont.getName())) {
                if ((font.getSize() == size)
                        && (font.getStyle() == inFont.getStyle())) {
                    return font;
                }
            }
        } else {
            fonts.put(inFont.getName(), new ArrayList<>());
        }
        Font newFont = inFont.deriveFont((float) size);
        fonts.get(inFont.getName()).add(newFont);
        return newFont;
    }
}

Related Tutorials