📄 ch16.htm
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p><META><H1><FONT SIZE=6 COLOR=#Ff0000>Chapter 16</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Drawing Graphics</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#TheAppletsCanvas">The Applet's Canvas</A><LI><A HREF="#ExampleUsingtheCoordinateSystem">Example: Using the Coordinate System</A><LI><A HREF="#DrawingShapes">Drawing Shapes</A><LI><A HREF="#ExampleDrawingaRectangle">Example: Drawing a Rectangle</A><LI><A HREF="#ExampleDrawingOtherShapes">Example: Drawing Other Shapes</A><LI><A HREF="#UnderstandingtheShapeAppletApplet">Understanding the ShapeApplet Applet</A><UL><LI><A HREF="#DrawingOvals">Drawing Ovals</A><LI><A HREF="#DrawingArcs">Drawing Arcs</A><LI><A HREF="#ExampleDrawingArcsinanApplet">Example: Drawing Arcs in an Applet</A><LI><A HREF="#DrawingPolygons">Drawing Polygons</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>Almost all applets need to create some sort of display, whetherthat display is as simple as a line of text or as sophisticatedas an animation sequence. Because Windows is a graphical system,everything you see on the screen during a Windows session is displayedgraphically. This is true even of text. Because of its graphicalnature, a system like Java's must include the capability to handledevice-independent graphics. In this chapter, you see not onlyhow you can display various graphical shapes, but also how toquery the system about the characteristics of the display.<H2><A NAME="TheAppletsCanvas"><FONT SIZE=5 COLOR=#Ff0000>The Applet's Canvas</FONT></A></H2><P>Every applet has an area of the screen, called the canvas, inwhich it can create its display. The size of an applet's canvasdepends on the size of the applet, which is in turn controlledby the parameters included in an HTML document's <TT><applet></TT>tag. Generally, the larger the applet appears in the HTML document,the larger the applet's visible canvas. Anything that you tryto draw outside of the visible canvas doesn't appear on the screen.<P>You draw graphical images on the canvas by using coordinates thatidentify pixel locations. Chances are good that you've had somesort of computer-graphics experience before Java, so you knowthat the coordinates that define pixel locations on a computerscreen can be organized in various ways. Windows, for example,supports a number of different mapping modes that determines howcoordinates are calculated in a window.<P>Thankfully, Java does away with the complications of displayinggraphics in a window by adopting a single coordinate system. Thiscoordinate system has its origin (point 0,0) in the upper-leftcorner, with the X axis increasing to the right, and the Y axisincreasing downward, as shown in Figure 16.1.<P><A HREF="f16-1.gif"><B> Figure 16.1 : </B><I>An applet's canvas uses the typical computer-display coordinate system.</I></A><P><H2><A NAME="ExampleUsingtheCoordinateSystem"><FONT SIZE=5 COLOR=#Ff0000>Example: Using the Coordinate System</FONT></A></H2><P>When you want to draw something on an applet's canvas, you usethe coordinate system shown in Figure 16.1. This coordinate systemsituates the system's origin in the applet's upper-left corner,just as it's shown in Figure 16.1. For example, Figure 16.2 showsan applet displaying a single line in its canvas. This line wasdrawn starting at coordinates 5,10, as shown in Figure 16.3.<P><A HREF="f16-2.gif"><B> Figure 16.2 : </B><I>This applet displays a single line.</I></A><P><P><A HREF="f16-3.gif"><B> Figure 16.3 : </B><I>The line in Figure 16.2 is drawn at the coordinates 5,10.</I></A><P><H2><A NAME="DrawingShapes"><FONT SIZE=5 COLOR=#Ff0000>Drawing Shapes</FONT></A></H2><P>Java's <TT>Graphics</TT> class includes methods for drawing manydifferent types of shapes, everything from straight lines to polygons.You were introduced to the <TT>Graphics</TT> class in Part IIof this book when you displayed text in an applet's <TT>paint()</TT>method. As you may recall, a reference to a <TT>Graphics</TT>object is passed to the <TT>paint()</TT> method as its singleargument. Because the <TT>Graphics</TT> class is part of the <TT>awt</TT>package, you have to include one of the following lines at thetop of your applet's code to use the class:<BLOCKQUOTE><PRE>import java.awt.Graphicsimport java.awt.*</PRE></BLOCKQUOTE><P>The first line in the preceding imports only the <TT>Graphics</TT>class, whereas the second line imports all the classes includedin the <TT>awt</TT> package. Table 16.1 lists the most commonlyused drawing methods in the <TT>Graphics</TT> class.<BR><P><CENTER><B>Table 16.1 Drawing Methods of the </B><I>Graphics</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=176><I><B>Method</B></I></TD><TD WIDTH=375><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>clearRect()</TT></TD><TD WIDTH=375>Erases a rectangular area of the canvas.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>copyArea()</TT></TD><TD WIDTH=375>Copies a rectangular area of the canvas to another area.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>drawArc()</TT></TD><TD WIDTH=375>Draws a hollow arc.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>drawLine()</TT></TD><TD WIDTH=375>Draws a straight line.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>drawOval()</TT></TD><TD WIDTH=375>Draws a hollow oval.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>drawPolygon()</TT></TD><TD WIDTH=375>Draws a hollow polygon.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>drawRect()</TT></TD><TD WIDTH=375>Draws a hollow rectangle.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>drawRoundRect()</TT></TD><TD WIDTH=375>Draws a hollow rectangle with rounded corners.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>drawString()</TT></TD><TD WIDTH=375>Displays a text string.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>fillArc()</TT></TD><TD WIDTH=375>Draws a filled arc.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>fillOval()</TT></TD><TD WIDTH=375>Draws a filled oval.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>fillPolygon()</TT></TD><TD WIDTH=375>Draws a filled polygon.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>fillRect()</TT></TD><TD WIDTH=375>Draws a filled rectangle.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>fillRoundRect()</TT></TD><TD WIDTH=375>Draws a filled rectangle with rounded corners.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>getColor()</TT></TD><TD WIDTH=375>Retrieves the current drawing color.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>getFont()</TT></TD><TD WIDTH=375>Retrieves the currently used font.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>getFontMetrics()</TT></TD><TD WIDTH=375>Retrieves information about the current font.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>setColor()</TT></TD><TD WIDTH=375>Sets the drawing color.</TD></TR><TR VALIGN=TOP><TD WIDTH=176><TT>setFont()</TT></TD><TD WIDTH=375>Sets the font.</TD></TR></TABLE></CENTER><P><P>To draw a shape in an applet's display area, you only need tocall the appropriate method and supply the arguments requiredby the method. These arguments are based on the coordinates atwhich you want to draw the shape. For example, the following codeexample draws a straight line from coordinate 5,10 to 20,30:<BLOCKQUOTE><PRE>g.drawLine(5, 10, 20, 30);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -