📄 120-125.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:The Graphical User Interface</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=3//-->
<!--PAGES=120-125//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="116-120.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="125-128.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">The FontMetrics Class</FONT></H3>
<P>The <I>FontMetrics</I> class is a public abstract class in the <I>java.awt</I> package. Font metrics are based on dimensions that are special to fonts. Font dimensions include the baseline, ascent, descent, leading, and height. Distances are generally given in pixels (when they are returned by the <I>FontMetrics</I> attribute methods). The font metrics dimensions are shown in Figure 3.2</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/03-02.jpg',309,167 )"><IMG SRC="images/03-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-02.jpg',309,167)"><FONT COLOR="#000077"><B>Figure 3.2</B></FONT></A> Font metrics dimensions.</P>
<P>Lines of cast type, called <I>slugs</I>, were originally set by hand; the compositor inserted a lead strip between slugs. That’s why the blank space between lines of type is called <I>leading</I>. The Linotype, the first practical typesetting machine, was used in 1886 at the <I>New York Tribune</I> [Mertle]. The nomenclature of typesetting has passed to the electronic era essentially unchanged. Typical font metrics are in units of points (at 72 points per inch). Because of the raster orientation of the <I>java.awt</I> , all the font metrics are measured in pixels. The number of pixels per inch is called the display’s <I>pitch</I>. Thus, there is no display-independent way to automatically relate the font metrics of the AWT to the font metrics of traditional typesetting. Such a system would have to be able to detect the pitch of the display before the traditional font metrics could be computed.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
package java.awt;
public abstract class FontMetrics {
public Font getFont()
public int getLeading()
public int getAscent()
public int getDescent()
public int getHeight()
public int getMaxAscent()
public int getMaxDescent()
public int getMaxDecent()
public int getMaxAdvance()
public int charWidth(int ch)
public int charWidth(char ch)
public int stringWidth(String str)
public int charsWidth(char data[], int off, int len)
public int bytesWidth(byte data[], int off, int len)
public int[] getWidths()
public String toString()
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading11"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose that the following constants are defined:
</P>
<!-- CODE SNIP //-->
<PRE>
Graphics g;
FontMetrics theFontMetrics = g.getFontMetrics();
Font aFont;
int distanceInPixels;
char ch;
char charArray[];
byte byteArray[];
int offset, length
</PRE>
<!-- END CODE SNIP //-->
<P>Here’s how to get the instance of the <I>Font</I> class upon which the <I>FontMetrics</I> are based:</P>
<!-- CODE SNIP //-->
<PRE>
afont = theFontMetrics.getFont();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the standard leading (line spacing between descent and ascent):
</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.getLeading();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the ascent, descent, and height:
</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.getAscent();
distanceInPixels = theFontMetrics.getDescent();
distanceInPixels = theFontMetrics.getHeight();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the maximum ascent and descent of a font:
</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.getMaxAscent();
distanceInPixels = theFontMetrics.getMaxDescent();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the maximum height of a font, add the maximum descent and ascent. To get the width of a character in the font, use this code:
</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.charWidth(ch);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the width of a string in the font:
</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.stringWidth(str);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the width of an array of characters in the font, starting at the offset and proceeding for <I>length</I> characters:</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.charsWidth(charArray, offset, length);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the width of an array of bytes in the font, starting at the offset and proceeding for <I>length</I> characters (characters come in as bytes and are converted to 16-bit characters):</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.bytesWidth(byteArray, offset, length);
</PRE>
<!-- END CODE SNIP //-->
<P>To get the width of the first 256 characters in the font:
</P>
<!-- CODE SNIP //-->
<PRE>
distanceInPixels = theFontMetrics.getWidths();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the string representation of <I>FontMetrics</I>:</P>
<!-- CODE SNIP //-->
<PRE>
str = theFontMetrics.toString();
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading12"></A><FONT COLOR="#000077">How to Draw a String with a Background</FONT></H4>
<P>Often the user will wish to draw a string that has a superimposed background. This is important if the string is to be changed dynamically. In the following example we show how to draw the date and time. The <I>clearRect</I> call erases a part of the display so that the string does not overwrite itself.</P>
<!-- CODE //-->
<PRE>
synchronized private void draw() {
Dimension dim = f.size();
int height = dim.height - 60;
int width = dim.width;
Date theDate = new Date();
String date_string = theDate.toString();
int xloc = 10;
int yloc = dim.height - 60;
int string_width = getFontMetrics(
g.getFont()).stringWidth(date_string);
int string_height = getFontMetrics(
g.getFont()).getHeight();
g.clearRect(xloc,yloc,string_width,string_height);
g.drawString(date_string, xloc,height+xloc);
}
</PRE>
<!-- END CODE //-->
<P>An example of the string, with filled background in place, is shown in Figure 3.3.
</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/03-03.jpg',155,25 )"><IMG SRC="images/03-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-03.jpg',155,25)"><FONT COLOR="#000077"><B>Figure 3.3</B></FONT></A> String with filled background.</P>
<H4 ALIGN="LEFT"><A NAME="Heading13"></A><FONT COLOR="#000077">How to Draw a Vertical String</FONT></H4>
<P>The following method draws a string vertically (something the AWT does not normally do):
</P>
<!-- CODE //-->
<PRE>
public void drawVerticalString(Graphics g,String str,int x,int y) {
int str_height = g.getFontMetrics().getHeight();
(str_height*str.length())/2;
for (int i = 0; i<str.length(); i++) {
int char_width = g.getFontMetrics().stringWidth
(str.substring(i,i+1));
g.drawString(str.substring(i,i+1),x-char_width/2,y);
y+=str_height;
} // end for
} // end drawVerticalString
</PRE>
<!-- END CODE //-->
<P>An example of the use of the vertical string is shown in Figure 3.4.
</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/03-04.jpg',302,456 )"><IMG SRC="images/03-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/03-04.jpg',302,456)"><FONT COLOR="#000077"><B>Figure 3.4</B></FONT></A> An example of the vertical string: the histogram.</P>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">The MenuItem Class</FONT></H3>
<P>An instance of the <I>MenuItem</I> class is typically added to a <I>Menu</I> instance. When an event is posted by a component, an instance of the <I>MenuItem</I> class can be passed in the <I>target</I> member of the <I>Event</I> instance. Thus, it is typical to write <I>handleEvent</I> methods that compare targets with <I>MenuItem</I> instances to see what the user has selected. The event handler routines are discussed in more detail later in this chapter.</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class MenuItem extends java.awt.MenuComponent {
/* Instance Variables */
boolean enabled;
String label;
/* Methods */
public void MenuItem(String label);
public synchronized void addNotify();
public String getLabel();
public void setLabel(String label);
public boolean isEnabled();
public void enable();
public void enable(boolean on);
public void disable();
public String paramString();
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="116-120.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="125-128.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -