?? creating and deriving fonts.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0074)http://java.sun.com/docs/books/tutorial/2d/textandfonts/fontselection.html -->
<HTML><HEAD><TITLE>Creating and Deriving Fonts</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<SCRIPT language=JavaScript><!-- hidefunction openWin(term) { url="../../information/glossary.html#" + term; myWin= window.open(url, "Glossary", "width=400,height=150,scrollbars=yes,status=no,toolbar=no,menubar=no"); myWin.focus();}//--></SCRIPT>
<META content="MSHTML 5.00.3813.800" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff link=#000099><B><FONT size=-1>The Java</FONT><SUP><FONT
size=-2>TM</FONT></SUP> <FONT size=-1>Tutorial</FONT></B> <BR>
<TABLE width=550 summary="layout">
<TBODY>
<TR>
<TD align=left vAlign=center><A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/index.html"
target=_top><IMG align=center alt="Previous Page" border=0 height=26
src="Creating and Deriving Fonts.files/PreviousArrow.gif" width=26></A> <A
href="http://java.sun.com/docs/books/tutorial/2d/TOC.html#textandfonts"
target=_top><IMG align=center alt="Lesson Contents" border=0 height=26
src="Creating and Deriving Fonts.files/TOCIcon.gif" width=26></A> <A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/linebreakmeasure.html"
target=_top><IMG align=center alt="Next Page" border=0 height=26
src="Creating and Deriving Fonts.files/NextArrow.gif" width=26></A></TD>
<TD align=middle vAlign=center><FONT size=-1><A
href="http://java.sun.com/docs/books/tutorial/index.html"
target=_top>Start of Tutorial</A> > <A
href="http://java.sun.com/docs/books/tutorial/2d/index.html"
target=_top>Start of Trail</A> > <A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/index.html"
target=_top>Start of Lesson</A> </FONT></TD>
<TD align=right vAlign=center><FONT size=-1><A
href="http://java.sun.com/docs/books/tutorial/search.html"
target=_top>Search</A> <BR><A
href="http://java.sun.com/docs/books/tutorial/forms/sendusmail.html">Feedback
Form</A> </FONT></TD></TR></TBODY></TABLE><IMG align=bottom alt="" height=8
src="Creating and Deriving Fonts.files/blueline.gif" width=550
NATURALSIZEFLAG="3"> <BR><FONT size=-1><B>Trail</B>: 2D Graphics
<BR><B>Lesson</B>: Working with Text and Fonts </FONT>
<H2>Creating and Deriving Fonts </H2>
<BLOCKQUOTE>You can display a text string with any font available on your
system in any size and style that you choose. To determine what fonts are
available on your system, you can call the
<CODE>GraphicsEnvironment.getAvailableFontFamilyNames</CODE> method. This
method returns an array of strings that contains the family names of the
available fonts. Any of the strings, along with a size and style argument, can
be used to create a new <CODE>Font</CODE> object. After creating a
<CODE>Font</CODE> object, you can reset its font family name, size or style to
create a custom font. </BLOCKQUOTE>
<H3>Example: FontSelection</H3>
<BLOCKQUOTE>The following applet allows you to change the font, size, and
style of the displayed text.
<P>
<P>
<CENTER><A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/FontSelectionDemo.html"
target=_blank><IMG align=bottom alt="Click this figure to run the applet."
height=257 src="Creating and Deriving Fonts.files/fontselection.gif" width=480
NATURALSIZEFLAG="3"></A><BR><EM>This is a picture of the applet's GUI. To run
the applet, click the picture. The applet will appear in a new browser
window.</EM></CENTER>
<P></P>The complete code for this applet is in <A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/example-1dot2/FontSelection.java"
target=_blank><FONT color=#bb000f><CODE>FontSelection.java</CODE></FONT></A><A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/example-1dot2/FontSelection.java"
target=_blank><IMG align=absMiddle alt=" (in a .java source file)" border=0
height=11 src="Creating and Deriving Fonts.files/sourceIcon.gif"
width=11></A>.
<P>The <CODE>getAvailableFontFamilyNames</CODE> method of
<CODE>GraphicsEnvironment</CODE> returns the font family names of the fonts
available on your system.
<BLOCKQUOTE><PRE>GraphicsEnvironment gEnv =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String envfonts[] = gEnv.getAvailableFontFamilyNames();
Vector vector = new Vector();
for ( int i = 1; i < envfonts.length; i++ ) {
vector.addElement(envfonts[i]);
}
</PRE></BLOCKQUOTE>The initial <CODE>Font</CODE> object is created with style
<CODE>Font.PLAIN</CODE> and size 10. The other available styles are ITALIC,
BOLD and BOLD+ITALIC.
<BLOCKQUOTE><PRE>Font thisFont;
...
thisFont = new Font("Arial", Font.PLAIN, 10);
</PRE></BLOCKQUOTE>A new <CODE>Font</CODE> is created from the specified font
name, style and size.
<BLOCKQUOTE><PRE>public void changeFont(String f, int st, String si){
Integer newSize = new Integer(si);
int size = newSize.intValue();
thisFont = new Font(f, st, size);
repaint();
}
</PRE></BLOCKQUOTE>To use the same font family but change one or both of the
style and size attributes, you can call one of the <CODE>deriveFont</CODE>
methods.
<P>To control the font used to render text, you set the font attribute in the
<CODE>Graphics2D</CODE> context before rendering. The font attribute is set by
passing a <CODE>Font</CODE> object to the <CODE>setFont</CODE> method. In this
example, the font attribute is set to the newly constructed <CODE>Font</CODE>
object and then the string is drawn in the center of the Component using the
specified font. In the <CODE>paint</CODE> method, the font attribute of the
<CODE>Graphics2D</CODE> context is set to the new <CODE>Font</CODE>. The
string is drawn in the middle of the component with the new font.
<BLOCKQUOTE><PRE>g2.setFont(thisFont);
String change = "Pick a font, size, and style to change me";
FontMetrics metrics = g2.getFontMetrics();
int width = metrics.stringWidth( change );
int height = metrics.getHeight();
g2.drawString( change, w/2-width/2, h/2-height/2 );
</PRE></BLOCKQUOTE>
<BLOCKQUOTE>
<HR>
<STRONG>Note:</STRONG> Due to bug # <A
href="http://developer.java.sun.com/developer/bugParade/bugs/4155852.html"
target=_blank><FONT color=#009bbb>4155852</FONT></A><A
href="http://developer.java.sun.com/developer/bugParade/bugs/4155852.html"
target=_blank><IMG align=absMiddle alt=" (outside of the tutorial)" border=0
height=11 src="Creating and Deriving Fonts.files/otherIcon.gif"
width=11></A>, FontSelection might not work properly for all font names
returned from the call to getFontFamilyNames. The sample might not respond
to changes in size or style and the text might not show up at all when some
fontnames are chosen. In general, Courier and Helvetica work fine. In the
meantime, check back periodically to see if these problems have been fixed.
<HR>
</BLOCKQUOTE>
<P></P></BLOCKQUOTE><IMG align=bottom alt="" height=8
src="Creating and Deriving Fonts.files/blueline.gif" width=550
NATURALSIZEFLAG="3"> <BR>
<TABLE width=550 summary="layout">
<TBODY>
<TR>
<TD align=left vAlign=center><A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/index.html"
target=_top><IMG align=center alt="Previous Page" border=0 height=26
src="Creating and Deriving Fonts.files/PreviousArrow.gif" width=26></A> <A
href="http://java.sun.com/docs/books/tutorial/2d/TOC.html#textandfonts"
target=_top><IMG align=center alt="Lesson Contents" border=0 height=26
src="Creating and Deriving Fonts.files/TOCIcon.gif" width=26></A> <A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/linebreakmeasure.html"
target=_top><IMG align=center alt="Next Page" border=0 height=26
src="Creating and Deriving Fonts.files/NextArrow.gif" width=26></A></TD>
<TD align=middle vAlign=center><FONT size=-1><A
href="http://java.sun.com/docs/books/tutorial/index.html"
target=_top>Start of Tutorial</A> > <A
href="http://java.sun.com/docs/books/tutorial/2d/index.html"
target=_top>Start of Trail</A> > <A
href="http://java.sun.com/docs/books/tutorial/2d/textandfonts/index.html"
target=_top>Start of Lesson</A> </FONT></TD>
<TD align=right vAlign=center><FONT size=-1><A
href="http://java.sun.com/docs/books/tutorial/search.html"
target=_top>Search</A> <BR><A
href="http://java.sun.com/docs/books/tutorial/forms/sendusmail.html">Feedback
Form</A> </FONT></TD></TR></TBODY></TABLE>
<P><FONT size=-1><A
href="http://java.sun.com/docs/books/tutorial/information/copyright.html">Copyright</A>
1995-2004 Sun Microsystems, Inc. All rights reserved. </FONT></P></BODY></HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -