Font Contract

An instance of Java's Font class represents a particular font. Each font has a family name (Helvetica, TimesRoman, Courier, etc.), a style (bold, italic, etc.), and a size (8, 10, 12, etc.). To see how these attributes affect the font, make selections in the following three lists, and then select the Preview button:



Instances of the font class are created via new Font(name, style, size);, where name is a string naming the font family (e.g. "Helvetica", "TimesRoman", etc.), style is described below, and size is an integer that is the point size for the font. The style parameter can be any one of the following combinations of special constants provided by the Font class:

For example, here is the paint method of an applet that creates four different fonts:

	public void paint(Graphics gfx)
	{
		Font h = new Font("Helvetica", Font.PLAIN, 12);
		Font tr = new Font("TimesRoman", Font.BOLD, 20);
		Font g = new Font("Geneva", Font.ITALIC, 14);
		Font c= new Font("Courier", Font.BOLD + Font.ITALIC, 36);
		gfx.setFont(h);
		gfx.drawString("Helvetica Plain 12", 10, 20);
		gfx.setFont(tr);
		gfx.drawString("TimesRoman Bold 20", 10, 50);
		gfx.setFont(g);
		gfx.drawString("Geneva Italic 14", 10, 75);
		gfx.setFont(c);
		gfx.drawString("Courier Bold Italic 36", 10, 110);
	}

The result of executing the above applet is the following window:

 

Below is a simple contract for the Font class. The Font class actually implements more constructors and methods than are described here. The full list of constructors and methods can be found in the Java API for the class java.awt.Font.


Index for the Font Class

Class Constants:

Constructors:

Methods:


Class Constants of the Font class


BOLD

public final static int BOLD = 1

The bold style constant. This can be combined with the other style constants for mixed styles.


ITALIC

public final static int ITALIC = 2

The italicized style constant. This can be combined with the other style constants for mixed styles.


PLAIN

public final static int PLAIN = 0

The plain style constant. This can be combined with the other style constants for mixed styles.



Constructors of the Font class

public Font(String name, int style, int size)

Creates a new font with the specified name, style, and point size.

Parameters:
name - the font name
style - the constant style used
size - the point size of the font



Methods of the Font Class


equals

public boolean equals(Object obj)

The result is true if and only if the argument is not null and is a Font object with the same name, same style, and same point size as this Font..
 
 
Parameters:
obj - he object to compare with
Returns:
true if the objects are the same; false otherwise.


getFamily

public String getFamily()

Returns:
the platform-specific family name of this font.


getName

public String getName()

Returns:
the logical name of this font.


getSize

public int getSize()

Returns:
the point size of this font.


isBold

public boolean isBold()

Returns:
true if this font is bold; false otherwise.


isItalic

public boolean isItalic()

Returns:
true if this font is italic; false otherwise.


isPlain

public boolean isPlain()

Returns:
true if this font is neither bold nor italic; false otherwise.


toString

public String toString()

Returns:
a string representation of this font.
 



Franklyn Turbak adapted this page from the
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to
doug.kramer@sun.com