Fonts.java// Fig. 13.11: Fonts.java
// Display strings in different fonts and colors.
import java.awt.Font;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
class FontJPanel extends JPanel
{
// display Strings in different fonts and colors
@Override
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);
}
} // end class FontJPanel
public class Fonts
{
// execute application
public static void main(String[] args)
{
// create frame for FontJPanel
JFrame frame = new JFrame("Using fonts");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FontJPanel fontJPanel = new FontJPanel();
frame.add(fontJPanel);
frame.setSize(420, 150);
frame.setVisible(true);
}
} // end class Fonts
Maintained by John Loomis, updated Wed Feb 08 12:16:22 2017