📄 116-120.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=116-120//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="113-116.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="120-125.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The <I>drawRect</I> method is currently implemented with a series of four draw lines. We hope this will change as the AWT becomes more optimized.</P>
<P>To clear a rectangular area of the screen, the <I>Graphics</I> class draws a rectangle with the current background color:</P>
<!-- CODE SNIP //-->
<PRE>
g.clearRect(x, y, width, height);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>Graphics</I> class provides rounded shape drawing. When a rounded shape is drawn, <I>arcWidth</I> and <I>arcHeight</I> are used. <I>arcWidth</I> and <I>arcHeight</I> are the width and height of an ellipse used to draw the rounded corners. Here’s how to draw the outline of a rectangle with rounded corners using the current color:</P>
<!-- CODE SNIP //-->
<PRE>
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw a filled rectangle with rounded corners using the current color:
</P>
<!-- CODE SNIP //-->
<PRE>
g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw a highlighted 3-D rectangle using two colors:
</P>
<!-- CODE SNIP //-->
<PRE>
g.draw3DRect(x, y, width, height, raised);
</PRE>
<!-- END CODE SNIP //-->
<P>If <I>raised</I> is true, the rectangle appears raised. This effect is currently implemented using darker and brighter colors to create highlights. The same rectangle can also be drawn filled:</P>
<!-- CODE SNIP //-->
<PRE>
g.fill3DRect(x, y, width, height, raised);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw an oval outline or a filled oval, use this code:
</P>
<!-- CODE SNIP //-->
<PRE>
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw an arc inscribed in a rectangle, starting at <I>startAngle</I> and ending at <I>startAngle</I>+<I>arcAngle</I>, use the following:</P>
<!-- CODE SNIP //-->
<PRE>
g.drawArc(x, y, width, height, startAngle, arcAngle);
</PRE>
<!-- END CODE SNIP //-->
<P>Angles are in degrees. Zero degrees is parallel to the X-axis, and positive angles are measured in a counterclockwise direction. For the filled form of the arc:
</P>
<!-- CODE SNIP //-->
<PRE>
g.fillArc(x, y, width, height, startAngle, arcAngle);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw a polygon using an array of (x,y) coordinates:
</P>
<!-- CODE SNIP //-->
<PRE>
g.drawPolygon(xArray, yArray, nPoints);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw a polygon using an instance of the <I>Polygon</I> class:</P>
<!-- CODE SNIP //-->
<PRE>
g.drawPolygon(aPolygon);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>drawPolygon</I> method is implemented with a call to</P>
<!-- CODE SNIP //-->
<PRE>
drawPolygon(aPolygon.xpoints, aPolygon.ypoints, aPolygon.npoints);
</PRE>
<!-- END CODE SNIP //-->
<P>To fill the polygon with the current color:
</P>
<!-- CODE SNIP //-->
<PRE>
g.fillPolygon(xArray, yArray, nPoints);
</PRE>
<!-- END CODE SNIP //-->
<P>There must be more than three points or else the polygon call is ignored. If the polygon has overlapping parts, the even-odd fill rule is used. This is typically implemented using a scanline that is drawn from left to right. If the scanline crosses the edge of the polygon an odd number of times, the pixels are inside the polygon and will be filled; otherwise, they fall outside the polygon. This is a typical approach for filling concave polygons.
</P>
<!-- CODE SNIP //-->
<PRE>
g.fillPolygon(aPolygon);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw a string starting at (x,y) using the current font and color:
</P>
<!-- CODE SNIP //-->
<PRE>
String str = “Java is fun”;
g.drawString(str,x,y);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw a character or byte array starting at (x,y) using the current font and color:
</P>
<!-- CODE SNIP //-->
<PRE>
g.drawChars(charArray, offsetIntoArray, numberOfItemToDraw, x, y);
g.drawBytes(byteArray, offsetIntoArray, numberOfCharactersToDraw, x, y);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>drawBytes</I> method assumes that the bytes represent the least significant eight bits of the Unicode characters. To draw an image at (x,y) and notify <I>anImageObserver</I>:</P>
<!-- CODE SNIP //-->
<PRE>
aBoolean = g.drawImage(img, x, y, anImageObserver);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw an image inside a rectangle (with optional scaling):
</P>
<!-- CODE SNIP //-->
<PRE>
aBoolean = g.drawImage(img, x, y, width, height, anImageObserver);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw an image with a background color:
</P>
<!-- CODE SNIP //-->
<PRE>
aBoolean = g.drawImage(img, x, y, aColor, anImageObserver);
</PRE>
<!-- END CODE SNIP //-->
<P>To draw an image with a background color and in a rectangle:
</P>
<!-- CODE SNIP //-->
<PRE>
aBoolean = g.drawImage(img, x, y, width, height, aColor,
anImageObserver);
</PRE>
<!-- END CODE SNIP //-->
<P>To dispose of the graphics context (before the garbage collector does):
</P>
<!-- CODE SNIP //-->
<PRE>
g.dispose();
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>Graphics</I> class overrides <I>Object.finalize()</I> with a dispose call:</P>
<!-- CODE SNIP //-->
<PRE>
g.finalize();
</PRE>
<!-- END CODE SNIP //-->
<P>To convert the <I>Graphics</I> instance into a string:</P>
<!-- CODE SNIP //-->
<PRE>
g.toString();
</PRE>
<!-- END CODE SNIP //-->
<P>The following sections give practical examples for the use of the methods in the <I>Graphics</I> class.</P>
<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">How to Draw a Grid</FONT></H4>
<P>In this section you will learn how to write a class that can draw a grid into a <I>Graphics</I> instance. Consider the following code for drawing a grid:</P>
<!-- CODE SNIP //-->
<PRE>
1. import java.applet.*;
2. import java.util.*;
3. import java.awt.*;
</PRE>
<!-- END CODE SNIP //-->
<P>Lines 1–3 are used to import packages into your program. Line 4 declares the class as a <I>final</I> class, preventing other programmers from subclassing the <I>draw</I> class.</P>
<!-- CODE SNIP //-->
<PRE>
4. final public class draw {
5. // prevent instantiation
6. private draw() {}
</PRE>
<!-- END CODE SNIP //-->
<P>Line 6 declares the constructor for the <I>draw</I> class to be <I>private</I>. This prevents the <I>draw</I> class from being instanced by programmers who develop outside the <I>draw</I> class. Line 7 is the <I>grid</I> method, which takes an instance of a <I>Graphics</I> class. The dimensions of the clip rectangle are extracted from a <I>Rectangle</I> instance using the <I>width</I> and <I>height</I> members of the <I>Rectangle</I> class. The <I>width</I> and <I>height</I> are stored as integers in units of pixels.</P>
<!-- CODE SNIP //-->
<PRE>
7. static void grid(Graphics g) {
8. Rectangle r = g.getClipRect();
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>Geometry</I> class is a custom class used by DiffCAD to store global instances. <I>Grid_spacing</I> is an <I>int</I> whose units are pixels.</P>
<!-- CODE //-->
<PRE>
9. int grid_spacing = Geometry.grid_width.getValue();
10. int w = r.width;
11. int h = r.height;
12. for (int x = 0; x < w; x = x + grid_spacing)
13. {g.drawLine(x,0,x,h);}
14. for (int y = 0; y < h; y = y + grid_spacing)
15. {g.drawLine(0,y,w,y);}
16. }
17. }
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="113-116.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="120-125.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 + -