📄 fonts.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD> <TITLE>2D Text Tutorial Lesson 1: Using Fonts</TITLE> <META NAME="AUTHOR" CONTENT="Monica Pawlan of the JDC"> <META NAME="CREATED" CONTENT="09/15/98"> <META NAME="OWNER" CONTENT="Editorial/JDC"> <META NAME="revision" CONTENT="@(#)fonts.src 1.10 11/10/99 JDC"> <META NAME="TOPIC" CONTENT="tutorial, graphics, media, JDK 1.2, text, API"> <STYLE TYPE="text/css"> <!-- CODE {font-family: Courier, Monospace; font-size: 12pt} PRE {font-family: Courier, Monospace; font-size: 11pt} --> </STYLE></HEAD><!-- Start Body Insert--><BODY BGCOLOR="#ffffff"><!-- End Body Insert--><!-- Start PageTop Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%"> <TR ALIGN="CENTER" VALIGN="TOP"> <TD WIDTH="157" ALIGN="LEFT"> <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT=""> <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A> <BR> <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD> <TD> <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD ALIGN="RIGHT"> <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD WIDTH="100%"> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"> <TR> <TD BGCOLOR="#CC9966" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> <TR> <TD BGCOLOR="#CC9966"> <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD> </TR> </TABLE> </TD> <TD ALIGN="LEFT"> <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD> <TD> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"> <TR> <TD VALIGN="CENTER"> <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD> <TD VALIGN="CENTER"> <FONT FACE="Helvetica" SIZE="1"> <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD> <TD VALIGN="CENTER"> <INPUT TYPE="image" SRC="/images/search.button.gif" value="search" BORDER="0" WIDTH="55" HEIGHT="14" ALT="Search"></TD> </TR> </TABLE> </TD> </TR> </TABLE> </FORM> <P> <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0"> <TR VALIGN="TOP"> <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55" HEIGHT="18" ALT=""></TD> <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152"> <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR VALIGN="TOP"> <TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" ALT="Online Training" BORDER=0></A></TD> </TR> </TABLE> </TD> </TR></TABLE><!-- End PageTop Insert --><!-- Start NavBar Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT"><!-- tab categories --><TR><TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD></TR><TR><TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD></TR><TR><TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD></TR><TR><TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD></TR><TR><TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News & Events from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD></TR><TR><TD> </TD></TR><!-- End NavBar Insert --><!-- START SUB-NAV --><TR> <TD><!-- INSERT SUB-NAV INFO --> </TD></TR><!-- END SUB-NAV --></TABLE><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><!-- Template Version 2.0 --><!-- ================== --><!-- Start Main Content --><!-- ================== --><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"> <DIV ALIGN="RIGHT"> <FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT> <H2>2D Text Tutorial<BR> <FONT SIZE="3">Lesson 1: Using Fonts</FONT></H2> <H4><EM>By Monica Pawlan</EM></H4> <FONT SIZE="-1">[<A HREF="index.html#TOC">CONTENTS</A>] [<A HREF="style.html">NEXT>></A>]</FONT></DIV> <P>Styled text is essentially a simple text string drawn with a font. The font defines the text string size, and its overall look and feel. <P>This lesson describes how to use the enhanced<A HREF="http://java.sun.com/products/jdk/1.2/docs/api/index.html"><CODE>java.awt.Font</CODE></A> class to create fonts. The next lesson explains how to use fonts to create and draw styled text.<P><UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#what">What is a Font?</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#find">Find Available Fonts</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#create">Creating and Deriving Fonts</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="exercises.html#one">Exercises</A></FONT></UL><P><HR><A NAME="what"></A><H3>What is a Font?</H3><IMG SRC="./Art/061.gif" ALT="Dictionary of Terms" ALIGN="LEFT" HEIGHT="90" WIDTH="71" HSPACE="5" VSPACE="5"><P>A font is a complete set of type in one point size and type face. Forexample, all English language characters and symbols in Helvetica 10 point bold face make up a font. The font defines the characteristic look, size, and style (bold, italics, or plain) of the text string that is drawn with the font.<P>How does a font define the characteristic look? A font is created fromglyphs,and a glyph is a bit-mapped image that defines the appearance of thecharactersor symbols in the font. All fonts within the same font family have asimilarappearance because they are all made from the same glyph set. Likewise,different fonts families use different glyph sets to achieve their owndistinguishing look.<P>A font family not only consists of fonts with a similar appearance, butalso different points sizes and styles. Helvetica 10 point bold and Helvetica 12 point italic are two fonts in the same family, and Times Roman 8 point bold and Times Roman 10 point regular are two fonts in a different font family.<A NAME="find"></A><H3>Find Available Fonts</H3>To use a font, you have to create a <CODE>Font</CODE> object, and to dothat, you need to know what fonts are available on the system andtheir names. Fonts have logical, family and font names. The logical name is a name mapped onto one of the specific fontsavailable on the platform.<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">To get the logical name for a <CODE>Font</CODE> object, call<CODE>java.awt.Font.getName</CODE></FONT></UL>The family name is the name of the font family that determines the typographic design across several faces, such as Helvetica or Times Roman.<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">To get the family name for a given <CODE>Font</CODE> object, call<CODE>java.awt.Font.getFamily</CODE>.</FONT></UL>The font name represents a specific font within a family such as Helvetica Bold.<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">To get the font name, call <CODE>java.awt.Font.getFontName</CODE>,and to determine which font faces are available on a system, call<CODE>java.awt.GraphicsEnvironment.getAllFonts.</CODE></FONT></UL><A NAME="create"></A><H3>Creating and Deriving Fonts</H3><IMG SRC="./Art/font.gif" ALIGN=LEFT ALT="Fonts">The easiest way to create a font is by specifying the font name, pointsize, and style. Once you have a <CODE>Font</CODE> object, you can derive any number of new <CODE>Font</CODE> objects by callingthe <CODE>Font.deriveFont</CODE> method on the existing font and specifyinga new point size, style, transform (position, slant, scale, or rotation), orattribute map.<PRE> Font boldFont = new Font("Helvetica", Font.BOLD, 12); Font italicDerived = boldFont.deriveFont(Font.ITALIC, 12); Font plainDerived = boldFont.deriveFont(Font.PLAIN, 14);</PRE>Once you have a font, you can use it to create a <CODE>TextLayout</CODE>object and draw styled text, which are the topics of the next lesson. </FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"> <TR> <TD VALIGN="TOP"> <P ALIGN=CENTER> <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif"> [ This page was updated: <!-- new date --> 12-Nov-99 ]</font></P> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/products/">Products & APIs</A> | <A HREF="/developer/index.html">Developer Connection</A> | <A HREF="/developer/infodocs/index.shtml">Docs & Training</A> | <A HREF="/developer/support/index.html">Online Support</A><BR> <A HREF="/developer/community/index.html">Community Discussion</A> | <A HREF="http://java.sun.com/industry/">Industry News</A> | <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | <A HREF="http://java.sun.com/casestudies">Case Studies</A> </FONT> </CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD ALIGN="CENTER"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - <A HREF="http://java.sun.com/applets/">Applets</A> - <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - <A HREF="http://java.sun.com/jobs/">Employment</A> - <A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> - <A HREF="http://java.sun.com/javastore/">Java Store</A> - <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A> </FONT> </TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <a href="/siteinfo/faq.html">FAQ</a> | <a href="/feedback/index.html">Feedback</a> | <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A> </FONT> </CENTER> </TD> </TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-99 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -