Example usage for java.awt Font deriveFont

List of usage examples for java.awt Font deriveFont

Introduction

In this page you can find the example usage for java.awt Font deriveFont.

Prototype

public Font deriveFont(Map<? extends Attribute, ?> attributes) 

Source Link

Document

Creates a new Font object by replicating the current Font object and applying a new set of font attributes to it.

Usage

From source file:Main.java

/**
 * magnify the all fonts in swing.//from  ww  w  .j ava 2s. c o m
 * 
 * @param mag
 *            magnify parameter <br/> mag > 1. : large <br/> mag < 1. :
 *            small
 */
public static void magnifyAllFont(float mag) {
    List history = new LinkedList();
    UIDefaults defaults = UIManager.getDefaults();
    Enumeration it = defaults.keys();
    while (it.hasMoreElements()) {
        Object key = it.nextElement();
        Object a = defaults.get(key);
        if (a instanceof Font) {
            if (history.contains(a))
                continue;
            Font font = (Font) a;
            font = font.deriveFont(font.getSize2D() * mag);
            defaults.put(key, font);
            history.add(font);
        }
    }
}

From source file:Main.java

/**
 * Changes font size of the specified component.
 *
 * @param component component to modify/*from  www. j a v  a  2  s  .  c om*/
 * @param change    font size change amount
 * @param <C>       component type
 * @return modified component
 */
public static <C extends Component> C changeFontSize(final C component, final int change) {
    if (component != null && component.getFont() != null) {
        final Font font = component.getFont();
        component.setFont(font.deriveFont((float) font.getSize() + change));
    }
    return component;
}

From source file:Main.java

/**
 * Modifica fontul de la un label si il face bold
 * @param component//from w w  w  .  j a v a 2  s. c  om
 */
public static void setFontBold(Component component) {
    Font oldFont = component.getFont();
    if (oldFont != null)
        component.setFont(oldFont.deriveFont(Font.BOLD));
}

From source file:Main.java

/**
 * Modifica fontul de la un label si il face bold
 * @param component/*from   www  .ja  va  2 s.c o m*/
 */
public static void setFontPlain(Component component) {
    Font oldFont = component.getFont();
    if (oldFont != null)
        component.setFont(oldFont.deriveFont(Font.PLAIN));
}

From source file:Main.java

/** 
 * Adjust all fonts for displaying to the given size.
 * <br>Use with care!//from www .j a v  a 2 s .c o  m
 */
public static void setUIFontSize(final int size) {

    final java.util.Enumeration<Object> keys = UIManager.getLookAndFeelDefaults().keys();
    while (keys.hasMoreElements()) {
        final Object key = keys.nextElement();
        final Object value = UIManager.get(key);
        if (value instanceof Font) {
            final Font ifont = (Font) value;
            final Font ofont = ifont.deriveFont((float) size);
            UIManager.put(key, ofont);
        }
    }
}

From source file:Main.java

/**
 * Gets the text width.//from   w  w w . j ava 2 s  .  c  o  m
 *
 * @param text
 *            the text
 * @param bold
 *            the bold
 * @return the text width
 */
public static int getTextWidth(final String text, final boolean bold) {
    Font font = UIManager.getFont("Label.font");
    if (bold) {
        font = font.deriveFont(Font.BOLD);
    }
    return getTextWidth(text, font);
}

From source file:Main.java

public static void removeFontAttr(Component component, Object key) {
    Font original = component.getFont();
    Map attributes = original.getAttributes();
    attributes.remove(key);/*w  ww. j  a v  a  2  s.  com*/
    component.setFont(original.deriveFont(attributes));
}

From source file:Main.java

public static void setFontAttribute(Component component, Object key, Object value) {
    Font original = component.getFont();
    Map attributes = original.getAttributes();
    attributes.put(key, value);/*from www.j  av a2s.  c  o m*/
    component.setFont(original.deriveFont(attributes));
}

From source file:GraphicsUtil.java

public static void drawString(Graphics g, String text, RectangularShape bounds, Align align, double angle) {
    Graphics2D g2 = (Graphics2D) g;
    Font font = g2.getFont();
    if (angle != 0)
        g2.setFont(font.deriveFont(AffineTransform.getRotateInstance(Math.toRadians(angle))));

    Rectangle2D sSize = g2.getFontMetrics().getStringBounds(text, g2);
    Point2D pos = getPoint(bounds, align);
    double x = pos.getX();
    double y = pos.getY() + sSize.getHeight();

    switch (align) {
    case North:/*from   w w  w  . j  a  v  a 2s . co  m*/
    case South:
    case Center:
        x -= (sSize.getWidth() / 2);
        break;
    case NorthEast:
    case East:
    case SouthEast:
        x -= (sSize.getWidth());
        break;
    case SouthWest:
    case West:
    case NorthWest:
        break;
    }

    g2.drawString(text, (float) x, (float) y);
    g2.setFont(font);
}

From source file:Main.java

public static Font subscriptFont(Font baseFont) {
    Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>(baseFont.getAttributes());
    attributes.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
    return baseFont.deriveFont(attributes);
}