📄 ch17.htm
字号:
<PRE>g.setFont(font);</PRE></BLOCKQUOTE><P>At this point, the next text you display in your applet will usethe new font. However, although you request a certain type andsize of font, you can't be sure of what you'll get. The systemtries its best to match the requested font, but you still needto know at least the size of the font with which you ended up.You can get all the information you need by creating a <TT>FontMetrics</TT>object, like this:<BLOCKQUOTE><PRE>FontMetrics fontMetrics = g.getFontMetrics(font);</PRE></BLOCKQUOTE><P>To get the height of a line of text, call the <TT>FontMetrics</TT>object's <TT>getHeight()</TT> method, like this:<BLOCKQUOTE><PRE>int height = fontMetrics.getHeight();<BR></PRE></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>CAUTION</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>When creating a font, be aware that the user's system may not have a particular font loaded. In that case, Java chooses a default font as a replacement. This possible font substitution is a good reason to use methods like <TT>Font.getName()</TT> in order to see whether you got the font you wanted. You especially need to know the size of the font, so you can be sure to position your text lines properly.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleDisplayingDifferentSizedFonts">Example: Displaying Different Sized Fonts</A></H3><P>You wouldn't create a font unless you had some text to display.The problem is that before you can display your text, you needto know at least the height of the font. Failure to consider thefont's height may give you text lines that overlap or that arespaced too far apart. You can use the height returned from the<TT>FontMetrics</TT> class's <TT>getHeight()</TT> method as arow increment value for each line of text you need to print. Listing17.5, which is the source code for the FontApplet2 applet, showshow this is done. Listing 17.6 is the applet's HTML document,and Figure 17.5 shows what the applet looks like.<P><A HREF="f17-5.gif"><B> Figure 17.5 : </B><I>This is Appletviewer running the FontApplet2 applet.</I></A><P><HR><BLOCKQUOTE><B>Listing 17.5 FontApplet2.java: Displaying DifferentSized Fonts.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class FontApplet2 extends Applet{ TextField textField; public void init() { textField = new TextField(10); add(textField); textField.setText("32"); } public void paint(Graphics g) { String s = textField.getText(); int height = Integer.parseInt(s); Font font = new Font("TimesRoman", Font.PLAIN, height); g.setFont(font); FontMetrics fontMetrics = g.getFontMetrics(font); height = fontMetrics.getHeight(); int row = 80; g.drawString("This is the first line.", 70, row); row += height; g.drawString("This is the second line.", 70, row); row += height; g.drawString("This is the third line.", 70, row); row += height; g.drawString("This is the fourth line.", 70, row); } public boolean action(Event event, Object arg) { repaint(); return true; }}</PRE></BLOCKQUOTE><HR><HR><BLOCKQUOTE><B>Listing 17.6 FONTAPPLET2.htmL: FontApplet2's HTMLDocument.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="FontApplet2.class" width=400 height=200 name="FontApplet2"></applet></PRE></BLOCKQUOTE><HR><P>When you run FontApplet2, you see the window shown in Figure 17.5.The size of the active font is shown in the text box at the topof the applet, and a sample of the font appears below the textbox. To change the size of the font, type a new value into thetext box and press Enter. Figure 17.6, for example, shows theapplet displaying 12-point font, whereas Figure 17.7 is the appletdisplaying 120-point characters. As you can see, no matter whatfont size you choose, the lines are properly spaced (althoughlarge fonts overrun the boundaries of the applet's canvas).<P><A HREF="f17-6.gif"><B> Figure 17.6 : </B><I>FontApplet2 can display any size characters you like. This is 12-point text.</I></A><P><P><A HREF="f17-7.gif"><B> Figure 17.7 : </B><I>This is FontApplet2 displaying 120-point text.</I></A><P><P>The spacing of the lines is accomplished by first creating a variableto hold the vertical position for the next line of text:<BLOCKQUOTE><PRE>int row = 80;</PRE></BLOCKQUOTE><P>Here, the program not only declares the <TT>row</TT> variable,but also initializes it with the vertical position of the firstrow of text.<P>The applet then prints the first text line, using <TT>row</TT>for <TT>drawString()</TT>'s third argument:<BLOCKQUOTE><PRE>g.drawString("This is the first line.", 70, row);</PRE></BLOCKQUOTE><P>In preparation for printing the next line of text, the programadds the font's height to the <TT>row</TT> variable:<BLOCKQUOTE><PRE>row += height;</PRE></BLOCKQUOTE><P>Each line of text is printed, with <TT>row</TT> being incrementedby the font's height in between, like this:<BLOCKQUOTE><PRE>g.drawString("This is the second line.", 70, row);row += height;g.drawString("This is the third line.", 70, row);</PRE></BLOCKQUOTE><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>In regular Windows programming, creating and using fonts is ameticulous and frustrating experience. Java, however, simplifiesthis task by offering the <TT>Font</TT> and <TT>FontMetrics</TT>classes. With just a few method calls, you can create the fontsyou need for your applet. Displaying text with any font is aseasy setting the font as the current font and getting the font'ssize. The font's height is especially important because a font'sheight determines the line spacing you must use. After you'vecreated and set the font, any text you display will use the newfont.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What method of the <TT>Graphics</TT> class do you call toget the active font?<LI>What method of the <TT>Font</TT> class do you call to geta font's name?<LI>What method of the <TT>Font</TT> class do you call to geta font's height?<LI>Why is it important to determine the height of the currentfont?<LI>How do you get a reference to a <TT>FontMetrics</TT> object?<LI>When would you use a <TT>FontMetrics</TT> object to obtaininformation about a font instead of using the <TT>Font</TT> object?<LI>How can you determine the width of an entire text string?<LI>When referring to fonts, what is a point?<LI>Define the terms ascent, descent, baseline, and leading.<LI>How does a font's height relate to ascent, descent, and leading?<LI>How do you create and use a new font?<LI>What happens if the font you request is not available on theuser's system?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an applet that displays three lines of text using the16-point Helvetica font. Use the height returned from the <TT>Font</TT>class's <TT>getHeight()</TT> method to space your lines of text.<LI>Modify the applet you created in exercise 1 to display boldtext.<LI>Modify exercise 2's applet so that it uses a <TT>FontMetric</TT>object to determine the font's height.<LI>Write an applet called FontApplet3 that provides a buttonthat the user can click in order to switch between the Courier,TimesRoman, and Helvetica fonts. The final applet should looklike Figure 17.8, displaying the text using the selected fontwith a height of 32 points and using the bold style. Figure 17.9shows what the applet looks like when the user has clicked thebutton and switched to the TimesRoman font. (You can find thesolution to this problem in the CHAP17 folder of this book's CD-ROM.</OL><P><A HREF="f17-8.gif"><B> Figure 17.8 : </B><I>This is FontApplet3 displaying the Courier font.</I></A><P><A HREF="f17-9.gif"><B> Figure 17.9 : </B><I>Here's FontApplet3 displaying the TimesRoman font.</I></A><P><HR><HR WIDTH="100%"></P></CENTER><!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>, All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -