Java AWT Graphics set font

Description

Java AWT Graphics set font


// Display strings in different fonts and colors.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

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

public class Main extends JPanel {
  // display Strings in different fonts and colors
  @Override//from   w w w.  jav  a  2  s.co  m
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    // set font to Serif (Times), bold, 12pt and draw a string
    g.setFont(new Font("Serif", Font.BOLD, 12));
    g.drawString("Serif 12 point bold.", 20, 30);

    // set font to Monospaced (Courier), italic, 24pt and draw a string
    g.setFont(new Font("Monospaced", Font.ITALIC, 24));
    g.drawString("Monospaced 24 point italic.", 20, 50);

    // set font to SansSerif (Helvetica), plain, 14pt and draw a string
    g.setFont(new Font("SansSerif", Font.PLAIN, 14));
    g.drawString("SansSerif 14 point plain.", 20, 70);

    // set font to Serif (Times), bold/italic, 18pt and draw a string
    g.setColor(Color.RED);
    g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 18));
    g.drawString(g.getFont().getName() + " " + g.getFont().getSize() + " point bold italic.", 20, 90);
  }

  public static void main(String[] args) {
    // create frame for Main
    JFrame frame = new JFrame("Using fonts");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Main Main = new Main();
    frame.add(Main);
    frame.setSize(420, 150);
    frame.setVisible(true);
  }
}



PreviousNext

Related