Scale the original font of a JComponent by a given factor - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

Scale the original font of a JComponent by a given factor

Demo Code


//package com.java2s;
import java.awt.Font;
import javax.swing.JComponent;

public class Main {
    /**//from www  .j ava2 s  .  co  m
     * Scale the original font of a component by a given factor
     * 
     * @param comp
     *          the component for which the font has to be changed
     * @param scaleFactor
     *          the scale factor (the result will be rounded to an integer)
     */
    public static void changeFont(JComponent comp, double scaleFactor) {
        Font font = comp.getFont();
        comp.setFont(scale(font, scaleFactor));
    }

    /**
     * Change the font style of a component
     * 
     * @param comp
     *          the component for which the font has to be changed
     * @param style
     *          the new style
     */
    public static void changeFont(JComponent comp, int style) {
        Font font = comp.getFont();
        comp.setFont(font.deriveFont(style));
    }

    /**
     * Scale the original font of a component by a given factor and change the style
     * 
     * @param comp
     *          the component for which the font has to be changed
     * @param scaleFactor
     *          the scale factor (the result will be rounded to an integer)
     * @param style
     *          the new style
     */
    public static void changeFont(JComponent comp, double scaleFactor,
            int style) {
        Font font = comp.getFont();
        font = scale(font, scaleFactor);
        comp.setFont(font.deriveFont(style));
    }

    private static Font scale(Font font, double factor) {
        int newSize = Math.round((float) (font.getSize() * factor));
        return font.deriveFont((float) newSize);
    }
}

Related Tutorials