📄 _chapter 7.htm
字号:
<li>
<p class="docList"><tt>void draw(Shape s)</tt></p>
<p class="docList">draws the outline of the given shape with the current
stroke.</li>
<li>
<p class="docList"><tt>void fill(Shape s)</tt></p>
<p class="docList">fills the interior of the given shape with the current
paint.</li>
</ul>
<h3 class="docSection1Title" id="c7s2">Shapes</h3>
<p class="docText">Here are some of the methods in the <tt>Graphics</tt> class
to draw shapes:</p>
<pre>drawLine
drawRectangle
drawRoundRect
draw3DRect
drawPolygon
drawPolyline
drawOval
drawArc
</pre>
<p class="docText">There are also corresponding <tt>fill</tt> methods. These
methods have been in the <tt>Graphics</tt> class ever since JDK 1.0. The Java 2D
API uses a completely different, object-oriented approach. Instead of methods,
there are classes:</p>
<pre>Line2D
Rectangle2D
RoundRectangle2D
Ellipse2D
Arc2D
QuadCurve2D
CubicCurve2D
GeneralPath
</pre>
<p class="docText">These classes all implement the <tt>Shape</tt> interface.</p>
<p class="docText">Finally, there is a <tt>Point2D</tt> class that describes a
point with an <span class="docEmphasis">x-</span> and a
<span class="docEmphasis">y-</span>coordinate. Points are useful to define
shapes, but they aren't themselves shapes.</p>
<p class="docText">To draw a shape, you first create an object of a class that
implements the <tt>Shape</tt> interface and then call the <tt>draw</tt> method
of the <tt>Graphics2D</tt> class.</p>
<p class="docText">The <tt>Line2D</tt>, <tt>Rectangle2D</tt>, <tt>
RoundRectangle2D</tt>, <tt>Ellipse2D</tt>, <tt>and Arc2D</tt> classes correspond
to the <tt>drawLine</tt>, <tt>drawRectangle</tt>, <tt>drawRoundRect</tt>, <tt>
drawOval</tt>, and <tt>drawArc</tt> methods. (The concept of a "3D rectangle"
has died the death that it so richly deserved梩here is no analog to the <tt>
draw3DRect</tt> method.) The Java 2D API supplies two additional classes:
quadratic and cubic curves. We discuss these shapes later in this section. There
is no <tt>Polygon2D</tt> class. Instead, the <tt>GeneralPath</tt> class
describes paths that are made up from lines, quadratic and cubic curves. You can
use a <tt>GeneralPath</tt> to describe a polygon; we show you how later in this
section.</p>
<p class="docText">The classes</p>
<pre>Rectangle2D
RoundRectangle2D
Ellipse2D
Arc2D
</pre>
<p class="docText">all inherit from a common superclass <tt>RectangularShape</tt>.
Admittedly, ellipses and arcs are not rectangular, but they have a
<span class="docEmphasis">bounding rectangle</span> (see
<a class="docLink" href="#ch07fig02">Figure 7-2</a>).</p>
<center>
<h5 id="ch07fig02" class="docFigureTitle">Figure 7-2. The bounding rectangle of an ellipse and
an arc</h5>
<p>
<img alt="graphics/07fig02.gif" src="07fig02.gif" border="0" width="500" height="137"></p>
</center>
<p class="docText">Each of the classes whose name ends in "2D" has two
subclasses for specifying coordinates as <tt>float</tt> or <tt>double</tt>
quantities. In Volume 1, you already encountered</p>
<pre>Rectangle2D.Float
Rectangle2D.Double
</pre>
<p class="docText">The same scheme is used for the other classes, such as</p>
<pre>Arc2D.Float
Arc2D.Double
</pre>
<p class="docText">Internally, all graphics classes use <tt>float</tt>
coordinates since <tt>float</tt> numbers use less storage space, and they have
sufficient precision for geometric computations. However, the Java programming
language makes it a bit more tedious to manipulate <tt>float</tt> numbers. For
that reason, most methods of the graphics classes use <tt>double</tt> parameters
and return values. Only when constructing a 2D object, you need to choose
between a constructor with <tt>float</tt> or <tt>double</tt> coordinates. For
example,</p>
<pre>Rectangle2D floatRect
= new Rectangle2D.Float(5F, 10F, 7.5F, 15F);
Rectangle2D doubleRect
= new Rectangle2D.Double(5, 10, 7.5, 15);
</pre>
<p class="docText">The <span class="docEmphasis"><tt>Xxx</tt></span><tt>2D.Float</tt>
and <span class="docEmphasis"><tt>Xxx</tt></span><tt>2D.Double</tt> classes are
subclasses of the <span class="docEmphasis"><tt>Xxx</tt></span><tt>2D</tt>
classes. After object construction, there is essentially no benefit in
remembering the subclass, and you can just store the constructed object in a
superclass variable, just like in the code example.</p>
<p class="docText">As you can see from the curious names, the
<span class="docEmphasis"><tt>Xxx</tt></span><tt>2D.Float</tt> and
<span class="docEmphasis"><tt>Xxx</tt></span><tt>2D.Double</tt> classes are also
inner classes of the <span class="docEmphasis"><tt>Xxx</tt></span><tt>2D</tt>
classes. That is just a minor syntactical convenience, to avoid an inflation of
outer class names.</p>
<p class="docText"><a class="docLink" href="#ch07fig03">Figure 7-3</a> shows the
relationships between the shape classes. However, the <tt>Double</tt> and <tt>
Float</tt> subclasses are omitted. Legacy classes from the pre-2D library are
marked with a gray fill.</p>
<center>
<h5 id="ch07fig03" class="docFigureTitle">Figure 7-3. Relationships between the shape classes</h5>
<p>
<img alt="graphics/07fig03.gif" src="07fig03.gif" border="0" width="500" height="366"></p>
</center>
<h4 class="docSection2Title" id="ch07lev2sec2">Using the Shape Classes</h4>
<p class="docText">You already saw how to use the <tt>Rectangle2D</tt>, <tt>
Ellipse2D</tt>, and <tt>Line2D</tt> classes in Chapter 7 of Volume 1. In this
section, you will learn how to work with the remaining 2D shapes.</p>
<p class="docText">For the <tt>RoundRectangle2D</tt> shape, you specify the top
left corner, width and height, and the x- and y-dimension of the corner area
that should be rounded (see <a class="docLink" href="#ch07fig04">Figure 7-4</a>).
For example, the call</p>
<center>
<h5 id="ch07fig04" class="docFigureTitle">Figure 7-4. Constructing a <tt>RoundRectangle2D</tt></h5>
<p>
<img alt="graphics/07fig04.gif" src="07fig04.gif" border="0" width="500" height="312"></p>
</center>
<pre>RoundRectangle2D r = new RoundRectangle2D.Double(150, 200,
100, 50, 20, 20);
</pre>
<p class="docText">produces a rounded rectangle with circles of radius 20 at
each of the corners.</p>
<p class="docText">To construct an arc, you specify the bounding box, followed
by the start angle and the angle swept out by the arc (see
<a class="docLink" href="#ch07fig05">Figure 7-5</a>) and the closure type, one
of <tt>Arc2D.OPEN</tt>, <tt>Arc2D.PIE</tt>, or <tt>Arc2D.CHORD</tt>.</p>
<center>
<h5 id="ch07fig05" class="docFigureTitle">Figure 7-5. Constructing an elliptical arc</h5>
<p>
<img alt="graphics/07fig05.gif" src="07fig05.gif" border="0" width="500" height="226"></p>
</center>
<pre>Arc2D a = new Arc2D(x, y, width, height,
startAngle, arcAngle, closureType);
</pre>
<p class="docText"><a class="docLink" href="#ch07fig06">Figure 7-6</a>
illustrates the arc types.</p>
<center>
<h5 id="ch07fig06" class="docFigureTitle">Figure 7-6. Arc types</h5>
<p>
<img alt="graphics/07fig06.gif" src="07fig06.gif" border="0" width="400" height="557"></p>
</center>
<p class="docText">However, the angles are not simply given in degrees, but they
are distorted such that a 45-degree angle denotes the diagonal position,
<span class="docEmphasis">even if width and height are not the same.</span> If
you draw circular arcs (for example in a pie chart), then you don't need to
worry about this. However, for elliptical arcs, be prepared for an adventure in
trigonometry梥ee the sidebar for details.</p>
<p class="docText">The Java 2D package supplies <span class="docEmphasis">
quadratic</span> and <span class="docEmphasis">cubic</span> curves. In this
chapter, we do not want to get into the mathematics of these curves. We suggest
you get a feel for how the curves look by running the program in
<a class="docLink" href="#ch07list01">Example 7-1</a>. As you can see in
<a class="docLink" href="#ch07fig07">Figures 7-7</a> and
<a class="docLink" href="#ch07fig08">7-8</a>, quadratic and cubic curves are
specified by two <span class="docEmphasis">end points</span> and one or two
<span class="docEmphasis">control points.</span> Moving the control points
changes the shape of the curves.</p>
<center>
<h5 id="ch07fig07" class="docFigureTitle">Figure 7-7. A quadratic curve</h5>
<p>
<img alt="graphics/07fig07.gif" src="07fig07.gif" border="0" width="290" height="245"></p>
</center><center>
<h5 id="ch07fig08" class="docFigureTitle">Figure 7-8. A cubic curve</h5>
<p>
<img alt="graphics/07fig08.gif" src="07fig08.gif" border="0" width="291" height="247"></p>
</center>
<table cellSpacing="0" width="90%" border="1">
<tr>
<td>
<h2 class="docSidebarTitle">Specifying Angles for Elliptical Arcs</h2>
<p class="docText">The algorithm for drawing elliptical arcs uses distorted
angles, which the caller must precompute. This sidebar tells you how. If you
belong to the large majority of programmers who never draw elliptical arcs,
just skip the sidebar. However, since the official documentation completely
glosses over this topic, we thought it is worth recording it to save those
who need this information a few hours of trigonometric agony.</p>
<p class="docText">You convert actual angles to distorted angles with the
following formula.</p>
<pre>distortedAngle = Math.atan2(Math.sin(angle) * width,
Math.cos(angle) * height);
</pre>
<p class="docText">Sometimes (such as in the example program at the end of
this section), you know an end point of the arc, or another point on the
line joining the center of the ellipse and that end point. In that case,
first compute</p>
<pre>dx = p.getX() - center.getX();
dy = p.getY() - center.getY();
</pre>
<p class="docText">Then, the distorted angle is</p>
<pre>distortedAngle = Math.atan2(-dy * width, dx * height);
</pre>
<p class="docText">(The minus sign in front of <tt>dy</tt> is necessary
because in the pixel coordinate system, the y-axis points downwards, which
leads to angle measurements that are clockwise, but you need to supply an
angle that is measured counterclockwise.)</p>
<p class="docText">Convert the result from radians to degrees:</p>
<pre>distortedAngle = Math.toDegrees(distortedAngle);
</pre>
<p class="docText">The result is a value between
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -