Get font style in Java

Description

The following code shows how to get font style.

Example


import java.awt.Font;
import java.awt.Graphics;
//from   www  . j a v  a2s  .c  o  m
import javax.swing.JFrame;

public class Main extends JFrame {

  public static void main(String[] a) {
    Main f = new Main();
    f.setSize(300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }

  public void paint(Graphics g) {
    Font f = g.getFont();

    int fontStyle = f.getStyle();
    String msg = "";
    
    if ((fontStyle & Font.BOLD) == Font.BOLD)
      msg += "Bold ";
    if ((fontStyle & Font.ITALIC) == Font.ITALIC)
      msg += "Italic ";
    if ((fontStyle & Font.PLAIN) == Font.PLAIN)
      msg += "Plain ";
    
    g.drawString(msg, 4, 16);
  }
}




















Home »
  Java Tutorial »
    Graphics »




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