Use fonts in paint : Paint Font « 2D Graphics « Java Tutorial






Use fonts in paint
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {

  public void paint(Graphics g) {
    Font courier = new Font ("Courier", Font.PLAIN, 12); 
    Font system = new Font ("System", Font.BOLD, 16); 
    Font helvetica = new Font ("Helvetica", Font.BOLD, 18);  

    g.setFont (courier);  
    g.drawString ("Courier", 10, 30); 
    g.setFont (system);  
    g.drawString ("System", 10, 70);  
    g.setFont (helvetica);  
    g.drawString ("Helvetica", 10, 90);  
  }
}

public class SettingFont {
  public static void main(String[] a) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setBounds(30, 30, 300, 300);
    window.getContentPane().add(new MyCanvas());
    window.setVisible(true);
  }
}








16.8.Paint Font
16.8.1.The Three Font Styles
16.8.2.Use fonts in paintUse fonts in paint