This section introduces methods and constants for font control. Most font methods and font constants are part of class Font. Some methods of class Font and class Graphics are summarized in Fig. 12.10.
Method or constant |
Description |
---|---|
Font constants, constructors and methods |
|
public final static int PLAIN |
|
A constant representing a plain font style. |
|
public final static int BOLD |
|
A constant representing a bold font style. |
|
public final static int ITALIC |
|
A constant representing an italic font style. |
|
public Font( String name, int style, int size ) |
|
Creates a Font object with the specified font name, style and size. |
|
public int getStyle() |
|
Returns an integer value indicating the current font style. |
|
public int getSize() |
|
Returns an integer value indicating the current font size. |
|
public String getName() |
|
Returns the current font name as a string. |
|
public String getFamily() |
|
Returns the font's family name as a string. |
|
public boolean isPlain() |
|
Returns TRue if the font is plain, else false. |
|
public boolean isBold() |
|
Returns true if the font is bold, else false. |
|
public boolean isItalic() |
|
Returns TRue if the font is italic, else false. |
|
Graphics methods for manipulating Fonts |
|
public Font getFont() |
|
Returns a Font object reference representing the current font. |
|
public void setFont( Font f ) |
|
Sets the current font to the font, style and size specified by the Font object reference f. |
Class Font's constructor takes three argumentsthe font name, font style and font size. The font name is any font currently supported by the system on which the program is running, such as standard Java fonts Monospaced, SansSerif and Serif. The font style is Font.PLAIN, Font.ITALIC or Font.BOLD (each is a static field of class Font). Font styles can be used in combination (e.g., Font.ITALIC + Font.BOLD). The font size is measured in points. A point is 1/72 of an inch. Graphics method setFont sets the current drawing fontthe font in which text will be displayedto its Font argument.
Portability Tip 12.2
The number of fonts varies greatly across systems. Java provides five logical font namesSerif, Monospaced, SansSerif, Dialog and DialogInputthat can be used on all Java platforms. The Java runtime environment (JRE) on each platform maps these logical font names to actual fonts installed on the platform. The actual fonts used may vary by platform. |
The application of Fig. 12.11Fig. 12.12 displays text in four different fonts, with each font in a different size. Figure 12.11 uses the Font constructor to initialize Font objects (in lines 16, 20, 24 and 29) that are each passed to Graphics method setFont to change the drawing font. Each call to the Font constructor passes a font name (Serif, Monospaced or SansSerif) as a string, a font style (Font.PLAIN, Font.ITALIC or Font.BOLD) and a font size. Once Graphics method setFont is invoked, all text displayed following the call will appear in the new font until the font is changed. Each font's information is displayed in lines 17, 21, 25 and 3031 using method drawString. Note that the coordinate passed to drawString corresponds to the lower-left corner of the baseline of the font. Line 28 changes the drawing color to red, so the next string displayed appears in red. Lines 3031 display information about the final Font object. Method getFont of class Graphics returns a Font object representing the current font. Method getName returns the current font name as a string. Method getSize returns the font size in points.
Figure 12.11. Graphics method setFont changes the drawing font.
(This item is displayed on page 608 in the print version)
1 // Fig. 12.11: FontJPanel.java 2 // Display strings in different fonts and colors. 3 import java.awt.Font; 4 import java.awt.Color; 5 import java.awt.Graphics; 6 import javax.swing.JPanel; 7 8 public class FontJPanel extends JPanel 9 { 10 // display Strings in different fonts and colors 11 public void paintComponent( Graphics g ) 12 { 13 super.paintComponent( g ); // call superclass's paintConponent 14 15 // set font to Serif (Times), bold, 12pt and draw a string 16 g.setFont( new Font( "Serif", Font.BOLD, 12 ) ); 17 g.drawString( "Serif 12 point bold.", 20, 50 ); 18 19 // set font to Monospaced (Courier), italic, 24pt and draw a string 20 g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) ); 21 g.drawString( "Monospaced 24 point italic.", 20, 70 ); 22 23 // set font to SansSerif (Helvetica), plain, 14pt and draw a string 24 g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); 25 g.drawString( "SansSerif 14 point plain.", 20, 90 ); 26 27 // set font to Serif (Times), bold/italic, 18pt and draw a string 28 g.setColor( Color.RED ); 29 g.setFont( new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) ); 30 g.drawString( g.getFont().getName() + " " + g.getFont().getSize() + 31 " point bold italic.", 20, 110 ); 32 } // end method paintComponent 33 } // end class FontJPanel |
Figure 12.12. Creating JFrame to display fonts.
1 // Fig. 12.12: Fonts.java 2 // Using fonts. 3 import javax.swing.JFrame; 4 5 public class Fonts 6 { 7 // execute application 8 public static void main( String args[] ) 9 { 10 // create frame for FontJPanel 11 JFrame frame = new JFrame( "Using fonts" ); 12 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 13 14 FontJPanel fontJPanel = new FontJPanel(); // create FontJPanel 15 frame.add( fontJPanel ); // add fontJPanel to frame 16 frame.setSize( 420, 170 ); // set frame size 17 frame.setVisible( true ); // display frame 18 } // end main 19 } // end class Fonts |
Figure 12.12 contains method main, which creates a JFrame. We add a FontJPanel object to this JFrame (line 15), which displays the graphics created in Fig. 12.11.
Software Engineering Observation 12.2
To change the font, you must create a new Font object. Font objects are immutableclass Font has no set methods to change the characteristics of the current font. |
Font Metrics
Sometimes it is necessary to get information about the current drawing font, such as its name, style and size. Several Font methods used to get font information are summarized in Fig. 12.10. Method getStyle returns an integer value representing the current style. The integer value returned is either Font.PLAIN, Font.ITALIC, Font.BOLD or the combination of Font.ITALIC and Font.BOLD. Method getFamily returns the name of the font family to which the current font belongs. The name of the font family is platform specific. Font methods are also available to test the style of the current font, and these too are summarized in Fig. 12.10. Methods isPlain, isBold and isItalic return TRue if the current font style is plain, bold or italic, respectively.
Sometimes precise information about a font's metrics must be knownsuch as height, descent (the amount a character dips below the baseline), ascent (the amount a character rises above the baseline) and leading (the difference between the descent of one line of text and the ascent of the line of text below itthat is, the interline spacing). Figure 12.13 illustrates some of the common font metrics.
Figure 12.13. Font metrics.
Class FontMetrics declares several methods for obtaining font metrics. These methods and Graphics method getFontMetrics are summarized in Fig. 12.14. The application of Fig. 12.15Fig. 12.16 uses the methods of Fig. 12.14 to obtain font metric information for two fonts.
Method |
Description |
---|---|
FontMetrics methods |
|
public int getAscent() |
|
Returns the ascent of a font in points. |
|
public int geTDescent() |
|
Returns the descent of a font in points. |
|
public int getLeading() |
|
Returns the leading of a font in points. |
|
public int getHeight() |
|
Returns the height of a font in points. |
|
Graphics methods for getting a Font's FontMetrics |
|
public FontMetrics getFontMetrics() |
|
Returns the FontMetrics object for the current drawing Font. |
|
public FontMetrics getFontMetrics( Font f ) |
|
Returns the FontMetrics object for the specified Font argument. |
Figure 12.15. Font metrics.
(This item is displayed on page 611 in the print version)
1 // Fig. 12.15: MetricsJPanel.java 2 // FontMetrics and Graphics methods useful for obtaining font metrics. 3 import java.awt.Font; 4 import java.awt.FontMetrics; 5 import java.awt.Graphics; 6 import javax.swing.JPanel; 7 8 public class MetricsJPanel extends JPanel 9 { 10 // display font metrics 11 public void paintComponent( Graphics g ) 12 { 13 super.paintComponent( g ); // call superclass's paintComponent 14 15 g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) ); 16 FontMetrics metrics = g.getFontMetrics(); 17 g.drawString( "Current font: " + g.getFont(), 10, 40 ); 18 g.drawString( "Ascent: " + metrics.getAscent(), 10, 55 ); 19 g.drawString( "Descent: " + metrics.getDescent(), 10, 70 ); 20 g.drawString( "Height: " + metrics.getHeight(), 10, 85 ); 21 g.drawString( "Leading: " + metrics.getLeading(), 10, 100 ); 22 23 Font font = new Font( "Serif", Font.ITALIC, 14 ); 24 metrics = g.getFontMetrics( font ); 25 g.setFont( font ); 26 g.drawString( "Current font: " + font, 10, 130 ); 27 g.drawString( "Ascent: " + metrics.getAscent(), 10, 145 ); 28 g.drawString( "Descent: " + metrics.getDescent(), 10, 160 ); 29 g.drawString( "Height: " + metrics.getHeight(), 10, 175 ); 30 g.drawString( "Leading: " + metrics.getLeading(), 10, 190 ); 31 } // end method paintComponent 32 } // end class MetricsJPanel |
Figure 12.16. Creating JFrame to display font metric information.
(This item is displayed on page 612 in the print version)
1 // Fig. 12.16: Metrics.java 2 // Displaying font metrics. 3 import javax.swing.JFrame; 4 5 public class Metrics 6 { 7 // execute application 8 public static void main( String args[] ) 9 { 10 // create frame for MetricsJPanel 11 JFrame frame = new JFrame( "Demonstrating FontMetrics" ); 12 frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 13 14 MetricsJPanel metricsJPanel = new MetricsJPanel(); 15 frame.add( metricsJPanel ); // add metricsJPanel to frame 16 frame.setSize( 510, 250 ); // set frame size 17 frame.setVisible( true ); // display frame 18 } // end main 19 } // end class Metrics |
Line 15 of Fig. 12.15 creates and sets the current drawing font to a SansSerif, bold, 12-point font. Line 16 uses Graphics method getFontMetrics to obtain the FontMetrics object for the current font. Line 17 outputs the String representation of the Font returned by g.getFont(). Lines 1821 use FontMetric methods to obtain the ascent, descent, height and leading for the font.
Line 23 creates a new Serif, italic, 14-point font. Line 24 uses a second version of Graphics method getFontMetrics, which accepts a Font argument and returns a corresponding FontMetrics object. Lines 2730 obtain the ascent, descent, height and leading for the font. Note that the font metrics are slightly different for the two fonts.
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover