⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch17.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 3 页
字号:
        g.drawString(info, 20, 125);    }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the applet uses the classes in the <TT>awt</TT>package.<BR>Tell Java that the applet uses the classes in the <TT>applet</TT>package.<BR>Derive the <TT>FontApplet</TT> class from Java's <TT>Applet</TT>class.<BR>    Override the <TT>paint()</TT> method.<BR>        Get a Font object representing the active font.<BR>        Get the name of the font.<BR>        Get the family name of the font.<BR>        Get the style of the font.<BR>        Create a style string based on the value of the styleinteger.<BR>        Get the size of the font.<BR>        Convert the size to a string.<BR>        Get an info string for the font.<BR>        Display the font's attributes.</BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 17.2&nbsp;&nbsp;FONTAPPLET.htmL: The HTML Documentfor Running </B><I>FontApplet</I><B>.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>&lt;title&gt;Applet Test Page&lt;/title&gt;&lt;h1&gt;Applet Test Page&lt;/h1&gt;&lt;applet    code=&quot;FontApplet.class&quot;    width=380    height=170    name=&quot;FontApplet&quot;&gt;&lt;/applet&gt;</PRE></BLOCKQUOTE><HR><P>As you can see from Listing 17.1, using the <TT>Font</TT> class'smethods is fairly straightforward. Just call the method, whichreturns a value that describes some aspect of the font representedby the <TT>Font</TT> object.<H3><A NAME="GettingFontMetrics">Getting Font Metrics</A></H3><P>In many cases, the information you can retrieve from a <TT>Font</TT>object is enough to keep you out of trouble. For example, by usingthe size returned by the <TT>getSize()</TT> method, you can properlyspace the lines of text. Sometimes, though, you want to know moreabout the font you're using. For example, you might want to knowthe width of a particular character or even the width in pixelsof an entire text string. In these cases, you need to work withtext metrics.<P>True to form, the Java Developer's Kit includes the <TT>FontMetrics</TT>class, which makes it easy to obtain information about fonts.You create a <TT>FontMetrics</TT> object like this:<BLOCKQUOTE><PRE>FontMetrics fontMetrics = getFontMetrics(font);</PRE></BLOCKQUOTE><P>You may recall that <TT>getFontMetrics()</TT>, which returns areference to a <TT>FontMetrics</TT> object for the active font,is a method of the <TT>Graphics</TT> class. Its single argumentis the <TT>Font</TT> object for which you want the font metrics.<P>Once you have the <TT>FontMetrics</TT> object, you can call itsmethods in order to obtain detailed information about the associatedfont. Table 17.2 lists the most commonly used methods.<BR><P><CENTER><B>Table 17.2&nbsp;&nbsp;Commonly Used </B><I>FontMetrics</I><B>Methods.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=147><I><B>Method</B></I></TD><TD WIDTH=315><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>charWidth()</TT></TD><TD WIDTH=315>Returns the width of a character.</TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>getAscent()</TT></TD><TD WIDTH=315>Returns the font's ascent.</TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>getDescent()</TT></TD><TD WIDTH=315>Returns the font's descent.</TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>getFont()</TT></TD><TD WIDTH=315>Returns the associated <TT>Font</TT> object.</TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>getHeight()</TT></TD><TD WIDTH=315>Returns the font's height.</TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>getLeading()</TT></TD><TD WIDTH=315>Returns the font's leading (line spacing).</TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>stringWidth()</TT></TD><TD WIDTH=315>Returns the width of a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=147><TT>toString()</TT></TD><TD WIDTH=315>Returns a string of information about the font.</TD></TR></TABLE></CENTER><P><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>If you haven't used fonts before, some of the terms-<I>leading</I>, <I>ascent</I>, and <I>descent</I>-used in Table 17.2 may be unfamiliar to you. <I>Leading</I> (pronounced &quot;ledding&quot;) is the amount of white space between lines of text. <I>Ascent</I> is the height of a character, from the baseline to the top of the character. <I>Descent</I> is the size of the area that accommodates the descending portions of letters, such as the tail on a lowercase <I>g</I>. <I>Height</I> is the sum of ascent, descent, and leading. See Figure 17.3 for examples of each.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><A HREF="f17-3.gif"><B> Figure 17.3 : </B><I>Ascent, descent, and leading determine the overall height of a font.</I></A><P><H3><A NAME="ExampleDisplayingFontMetrics">Example: Displaying Font Metrics</A></H3><P>Most of the methods listed in Table 17.2 are self-explanatory.However, you probably want a chance to see them in action. Listing17.3 is the source code for the MetricsApplet, and Listing 17.4is the applet's HTML document. When you run the MetricsAppletapplet, you see the window shown in Figure 17.4. At the top ofthe window is a text box into which you can enter different stringsof text. When you press Enter, the applet displays the lengthof the string in pixels. Immediately below the text box is informationabout the current font.<P><A HREF="f17-4.gif"><B> Figure 17.4 : </B><I>This is Appletviewer running the MetricsApplet applet.</I></A><P><HR><BLOCKQUOTE><B>Listing 17.3&nbsp;&nbsp;MetricsApplet.java: An Applet ThatDisplays Text Metrics.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class MetricsApplet extends Applet{    TextField textField;    public void init()    {        textField = new TextField(20);        add(textField);        textField.setText(&quot;Default string&quot;);    }    public void paint(Graphics g)    {        Font font = getFont();        FontMetrics fontMetrics = g.getFontMetrics(font);        int n = fontMetrics.getLeading();        String leading = String.valueOf(n);        n = fontMetrics.getAscent();        String ascent = String.valueOf(n);        n = fontMetrics.getDescent();        String descent = String.valueOf(n);        n = fontMetrics.getHeight();        String height = String.valueOf(n);        String s = textField.getText();        n = fontMetrics.stringWidth(s);        String width = String.valueOf(n);        g.drawString(&quot;FONT INFO:&quot;, 55, 60);        g.drawString(&quot;Leading: &quot; + leading, 70, 80);        g.drawString(&quot;Ascent: &quot; + ascent, 70, 95);        g.drawString(&quot;Descent: &quot; + descent, 70, 110);        g.drawString(&quot;Height: &quot; + height, 70, 125);        g.drawString(&quot;STRING INFO:&quot;, 55, 155);        g.drawString(&quot;Width: &quot; + width, 70, 175);    }    public boolean action(Event event, Object arg)    {        repaint();        return true;    }}</PRE></BLOCKQUOTE><HR><HR><BLOCKQUOTE><B>Listing 17.4&nbsp;&nbsp;METRICSAPPLET.htmL: MetricsApplet'sHTML Document.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>&lt;title&gt;Applet Test Page&lt;/title&gt;&lt;h1&gt;Applet Test Page&lt;/h1&gt;&lt;applet    code=&quot;MetricsApplet.class&quot;    width=200    height=200    name=&quot;MetricsApplet&quot;&gt;&lt;/applet&gt;</PRE></BLOCKQUOTE><HR><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>Because all of the applets you've written so far in this book haven't used text metrics when displaying text, you may wonder why you even need to bother with this stuff. Chances are that when you're running your applets under Windows 95 using the default font, everything will work fine. But remember that your applets may run on machines using other operating systems, and their default fonts may not be exactly the same size. Also, when you create your own fonts, you may not know the resultant font's size exactly. In order to position text accurately, you need to use font metrics, as you'll see later in this chapter.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="CreatingFonts"><FONT SIZE=5 COLOR=#Ff0000>Creating Fonts</FONT></A></H2><P>You may think an applet that always uses the default font is boringto look at. In many cases, you'd be right. An easy way to spruceup an applet is to use different fonts. Luckily, Java enablesyou to create and set fonts for your applet. You do this by creatingyour own font object, like this:<BLOCKQUOTE><PRE>Font font = new Font(&quot;TimesRoman&quot;, Font.PLAIN, 20);</PRE></BLOCKQUOTE><P>The constructor for the <TT>Font</TT> class takes three arguments:the font name, style, and size. The style can be any combinationof the font attributes that are defined in the <TT>Font</TT> class.Those attributes are <TT>Font.PLAIN</TT>, <TT>Font.BOLD</TT>,and <TT>Font.ITALIC</TT>.<H3><A NAME="ExampleCreatingaFontwithMultipleStyles">Example: Creating a Font with Multiple Styles</A></H3><P>Although you can create fonts with the plain, bold, or italicstyles, you may at times need to combine font styles. Suppose,for example, that you wanted to use both bold and italic styles.The line<BLOCKQUOTE><PRE>Font font = new Font(&quot;Courier&quot;, Font.BOLD + Font.ITALIC, 18);</PRE></BLOCKQUOTE><P>gives you an 18-point bold italic Courier font. (A point is ameasurement of a font's height and is equal to 1/72 of an inch.)<H3><A NAME="UsingtheFont">Using the Font</A></H3><P>After you've created the font, you have to tell Java to use thefont. You do this by calling the <TT>Graphics</TT> class's <TT>setFont()</TT>method, like this:<BLOCKQUOTE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -