📄 ch16.htm
字号:
</PRE></BLOCKQUOTE><P>The <TT>g</TT> in the preceding code line is the <TT>Graphics</TT>object passed to the <TT>paint()</TT> method. As you can see,the <TT>drawLine()</TT> method takes four arguments, which areX,Y coordinate pairs that specify the starting and ending pointsof the line.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>TIP</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>There may be times when you need to retrieve information about the system's currently set graphical attributes. Java's <TT>Graphics</TT> class supplies methods like <TT>getColor()</TT>, <TT>getFont()</TT>, and <TT>getFontMetrics()</TT> to enable you to obtain this information.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="ExampleDrawingaRectangle"><FONT SIZE=5 COLOR=#Ff0000>Example: Drawing a Rectangle</FONT></A></H2><P>Most of the shape-drawing methods are as easy to use as the <TT>drawLine()</TT>method is. Suppose that you want to write an applet that drawsa filled rounded rectangle inside a hollow rectangle. You'd thenadd calls to the <TT>Graphics</TT> class's <TT>fillRoundRect()</TT>and <TT>drawRect()</TT> to the applet's <TT>paint()</TT> method.Listing 16.1 is just such an applet, whereas Listing 16.2 is theHTML document that displays the applet. Figure 16.4 shows theapplet running under Appletviewer.<P><A HREF="f16-4.gif"><B> Figure 16.4 : </B><I>This is RectApplet running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 16.1 RECTAPPLET.JAVA: Drawing Rectangles.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class RectApplet extends Applet{ public void paint(Graphics g) { g.drawRect(35, 15, 125, 200); g.fillRoundRect(50, 30, 95, 170, 15, 15); }}</PRE></BLOCKQUOTE><HR><HR><BLOCKQUOTE><B>Listing 16.2 RECTAPPLET.htmL: HTML Document forRectApplet.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE><title>Applet Test Page</title><h1>Applet Test Page</h1><applet code="RectApplet.class" width=200 height=250 name="RectApplet"></applet></PRE></BLOCKQUOTE><HR><P>In RectApplet's <TT>paint()</TT> method, you can see the methodcalls that produce the graphical display. The first line createsthe outside rectangle. That method call looks like this:<BLOCKQUOTE><PRE>g.drawRect(35, 15, 125, 200);</PRE></BLOCKQUOTE><P>The <TT>drawRect()</TT> method's four arguments are the X,Y coordinatesof the rectangle's upper-left corner and the width and heightof the rectangle. The rounded filled rectangle is almost as easyto draw:<BLOCKQUOTE><PRE>g.fillRoundRect(50, 30, 95, 170, 15, 15);</PRE></BLOCKQUOTE><P>The first four arguments of the <TT>fillRoundRect()</TT> methodare the same as those for the <TT>drawRect()</TT> method. Thefifth and sixth arguments are the size of the rectangle that representsthe rounded corners. Think of this rectangle as being placed oneach corner of the main rectangle and a curved line drawn betweenits corners, as shown in Figure 16.5.<P><A HREF="f16-5.gif"><B> Figure 16.5 : </B><I>The coordinates for the rounded corners are given as the width and height of the rectangle that encloses the rounded corner.</I></A><P><H2><A NAME="ExampleDrawingOtherShapes"><FONT SIZE=5 COLOR=#Ff0000>Example: Drawing Other Shapes</FONT></A></H2><P>Some shapes you can draw with the <TT>Graphics</TT> class aremore complex than others. For example, the <TT>drawArc()</TT>method requires six arguments in order to draw a simple curvedline. To see how drawing other shapes works, you'll now createthe ShapeApplet applet, which enables you to switch from one shapeto another in the applet's display. Listing 16.3 is ShapeApplet'ssource code. Figures 16.6 and 16.7 show what the applet lookslike running under the Appletviewer application.<P><A HREF="f16-6.gif"><B> Figure 16.6 : </B><I>This is what ShapeApplet looks like when it first runs.</I></A><P><P><A HREF="f16-7.gif"><B> Figure 16.7 : </B><I>This is ShapeApplet displaying an oval.</I></A><P><HR><BLOCKQUOTE><B>Listing 16.3 ShapeApplet.java: An Applet That DrawsVarious Shapes.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class ShapeApplet extends Applet{ int shape; Button button; public void init() { shape = 0; button = new Button("Next Shape"); add(button); } public void paint(Graphics g) { int x[] = {35, 150, 60, 140, 60, 150, 35}; int y[] = {50, 80, 110, 140, 170, 200, 230}; int numPts = 7; switch(shape) { case 0: g.drawLine(35, 50, 160, 230); break; case 1: g.drawRect(35, 50, 125, 180); break; case 2: g.drawRoundRect(35, 50, 125, 180, 15, 15); break; case 3: g.drawOval(35, 50, 125, 180); break; case 4: g.drawArc(35, 50, 125, 180, 90, 180); break; case 5: g.drawPolygon(x, y, numPts); break; case 6: g.fillPolygon(x, y, numPts); break; } } public boolean action(Event event, Object arg) { ++shape; if (shape == 7) shape = 0; repaint(); return true; }}</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>ShapeApplet</TT> class from Java's <TT>Applet</TT>class.<BR> Declare the class's shape data field.<BR> Declare the class's button data field.<BR> Override the <TT>init()</TT> method.<BR> Initialize the shape counter.<BR> Create the applet's <TT>Button</TT> object.<BR> Add the <TT>Button</TT> object to the applet.<BR> Override the <TT>paint()</TT> method.<BR> Initialize the X and Y coordinates for the polygons.<BR> Initialize the polygon point count.<BR> Display a shape based on the value of the shape counter.<BR> Override the <TT>action()</TT> method.<BR> Increment the shape counter.<BR> Reset the shape counter if it has reached its maximumvalue.<BR> Force the applet to repaint its canvas with the next shape.<BR> Tell Java that the method executed okay.</BLOCKQUOTE><P>To run ShapeApplet, use the HTML document shown in Listing 16.2,except change all occurrences of RectApplet to ShapeApplet. Whenyou run the applet with Appletviewer, you see the window shownin Figure 16.6. To change the shape displayed in the applet'scanvas, click the Next Shape button.<H2><A NAME="UnderstandingtheShapeAppletApplet"><FONT SIZE=5 COLOR=#Ff0000>Understanding the ShapeApplet Applet</FONT></A></H2><P>You don't need to concern yourself at this point with the buttoncontrol that ShapeApplet uses to switch shapes, except to knowthat just like the <TT>TextField</TT> controls you've been using,clicking the button causes Java to call the applet's <TT>action()</TT>method. The <TT>action()</TT> method increments the shape counter,<TT>shape</TT>, and tells the applet to redraw itself. In the<TT>paint()</TT> method, the value of <TT>shape</TT> is used ina <TT>switch</TT> statement to determine which shape gets drawn.You learned about <TT>switch</TT> statements back in <A HREF="ch9.htm" >Chapter 9</A>"The <TT>if</TT> and <TT>switch</TT> Statements."<H3><A NAME="DrawingOvals">Drawing Ovals</A></H3><P>The real meat of this program are the calls to the <TT>Graphics</TT>object's various shape-drawing methods. You already know aboutthe first three: <TT>drawLine()</TT>, <TT>drawRect()</TT>, and<TT>drawRoundRect()</TT>. The call to <TT>drawOval()</TT>, however,is new and looks like this:<BLOCKQUOTE><PRE>g.drawOval(35, 50, 125, 180);</PRE></BLOCKQUOTE><P>As you can see, this method, which draws ovals and circles, takesfour arguments. These arguments are the X,Y coordinates, width,and height of a rectangle that can enclose the oval. Figure 16.8shows how the resultant oval relates to its enclosing rectangle.<P><A HREF="f16-8.gif"><B> Figure 16.8 : </B><I>An oval's coordinates are actually the coordinates of <r>an enclosing rectangle.</I></A><P><H3><A NAME="DrawingArcs">Drawing Arcs</A></H3><P>Next in <TT>paint()</TT> is the <TT>drawArc()</TT> method, whichis the most complicated (at least, from an understanding pointof view) of the shape-drawing methods. The call to <TT>drawArc()</TT>looks like this:<BLOCKQUOTE><PRE>g.drawArc(35, 50, 125, 180, 90, 180);</PRE></BLOCKQUOTE><P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -