Derive a new Font from existing font to change font style and font size in Java

Description

The following code shows how to derive a new Font from existing font to change font style and font size.

Example


import java.awt.BorderLayout;
import java.awt.Font;
/*from ww  w.  j  av  a 2  s.c  o  m*/
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("java2s.com");

    Font myFont = new Font("Serif", Font.ITALIC | Font.BOLD, 12);
    Font newFont = myFont.deriveFont(Font.ITALIC,50F);

    button.setFont(newFont);
    f.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
  }
}

The code above generates the following result.

Derive a new Font from existing font to change font style and font size in Java




















Home »
  Java Tutorial »
    Graphics »




Animation
BufferedImage
Color
Font
Gradient
Graphics Settings
Image
Mouse Draw
Print
Shape
Text
Transform