Learning Java
Custom Search

Previous Next

Fonts

The Font class found in the java.awt package is used for setting the font on some objects. In the constructor of the Font class, you specify the font name, the style (bold, italics, etc), and the size.

    Font(String name, int style, int size)

The font name can be a font face name or a font family name, and may represent either a logical font or a physical font. The family names for logical fonts are: Dialog, DialogInput, Monospaced, Serif, or SansSerif.

The FontMetrics class can be used to obtain information on the current font. Its methods below can be used to obtain this font information.

stringWidth(String) returns the full width of the string in pixels
charWidth(char) returns the full width of the character
getHeight() returns the height of the font

The example below illustrates the use of the Font and the FontMetrics classes.

Below we have 2 classes in the same text file, FontSamples and TextFramePanel.

FontSamples should be familiar at this point. It is our frame where we will instantiate our JPanel TextFramePanel. Then we will add this panel to our frame.

In our class TextFramePanel we will override our paintComponent method. We define a String fontName to hold our various fonts. We then vary the values in Font(String name, int style, int size). To apply each font to our Graphics object, we use setFont. Then we print our text on our panel with drawString. The text we use will tell us what font is being printed and it's height. In order to get the height of the font so that we can print it, we use the FontMetrics object and the getHeight method from it.

import java.awt.*;
import javax.swing.*;

public class FontSamples extends JFrame {

    public FontSamples() {
        super("Font Samples");
        setSize(200, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TextFramePanel tf = new TextFramePanel();
        add(tf);
        setVisible(true);
    }

    public static void main(String[] arguments) {
        FontSamples frame = new FontSamples();
    }
}

class TextFramePanel extends JPanel {
    String fontName;

    public void paintComponent(Graphics comp) {
	int h, y;
	FontMetrics metrics;
 
	fontName = "Dialog";
        Font font = new Font(fontName, Font.BOLD, 18);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 20);

	fontName = "Monospaced";
        font = new Font(fontName, Font.ITALIC, 10);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 40);

	fontName = "SansSerif";
        font = new Font(fontName, Font.PLAIN, 12);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 60);

	fontName = "Serif";
        font = new Font(fontName, Font.PLAIN, 12);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 80);

	fontName = "Arial";
        font = new Font(fontName, Font.PLAIN, 12);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 100);

	fontName = "Arial Bold";
        font = new Font(fontName, Font.PLAIN, 12);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 120);

	fontName = "Comic Sans MS";
        font = new Font(fontName, Font.PLAIN, 12);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 140);

	fontName = "Impact";
        font = new Font(fontName, Font.PLAIN, 12);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 160);

	fontName = "Book Antiqua";
        font = new Font(fontName, Font.PLAIN, 12);
        comp.setFont(font);
        metrics = getFontMetrics(font);
        h = metrics.getHeight();
        comp.drawString(fontName + " " + h, 10, 180);
    }
}

 

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved