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.
public final static int BOLD = 1
public final static int ITALIC = 2
public final static int PLAIN = 0
public Font(String name, int style, int
size)
name -
the font name
style -
the constant style used
size -
the point size of the font
public boolean equals(Object obj)
obj -
he object to compare
with
public String getFamily()
- Returns:
- the platform-specific family name of this font.
public String getName()
- Returns:
- the logical name of this font.
public int getSize()
- Returns:
- the point size of this font.
public boolean isBold()
- Returns:
- true if this font is bold; false otherwise.
public boolean isItalic()
- Returns:
- true if this font is italic; false otherwise.
public boolean isPlain()
- Returns:
- true if this font is neither bold nor italic; false otherwise.
public String toString()
- Returns:
- a string representation of this font.