Java AWT Font get style

Description

Java AWT Font get style

import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

class DrawPanel extends JPanel {

  public void paint(Graphics g) {
    Font f = g.getFont();/*from www .java  2 s  . c o  m*/
    String fontName = f.getName();
    String fontFamily = f.getFamily();
    int fontSize = f.getSize();
    int fontStyle = f.getStyle();

    String msg = "Family: " + fontName;
    msg += ", Font: " + fontFamily;
    msg += ", Size: " + fontSize + ", Style: ";
    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);
  }
}

public class Main {
  public static void main(String[] args) {
    DrawPanel panel = new DrawPanel();

    JFrame application = new JFrame();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    application.add(panel);
    application.setSize(250, 250);
    application.setVisible(true);
  }
}



PreviousNext

Related