📄 _chapter 7.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 7</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<ul></ul>
<table width="100%" border="1" bgcolor="#EBEBFF">
<tr>
<td width="5%" align="left" valign="middle"><a href="_chapter%206.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td>
<td align="center" valign="middle"><a class="docLink" href="Front%20matter.htm">CONTENTS</a></td>
<td width="5%" align="right" valign="middle"><a href="_chapter%208.htm"><img src="Rarrow.gif" width="17" height="19" border="0"></a></td>
</tr>
</table>
<h2 class="docChapterTitle">Chapter 7. Advanced AWT</h2>
<ul>
<li>
<p class="docList"><a class="docLink" href="#c7s1">The Rendering Pipeline</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s2">Shapes</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s3">Areas</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s4">Strokes</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s5">Paint</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s6">Coordinate transformations</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s7">Clipping</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s8">Transparency and Composition</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s9">Rendering Hints</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s10">Reading and Writing Images</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s11">Image Manipulation</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s12">Printing</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s13">The Clipboard</a></li>
<li>
<p class="docList"><a class="docLink" href="#c7s14">Drag and Drop</a></li>
</ul>
<p class="docText">In Volume 1, you have seen how to use the methods of the <tt>
Graphics</tt> class to create simple drawings. Those methods are sufficient for
simple applets and applications, but they fall short for complex shapes or when
you require complete control over the appearance of the graphics. The Java 2D
API is a more recent class library that you can use to produce high-quality
drawings. In this chapter, we give you an overview of that API.</p>
<p class="docText">We then turn to the topic of printing and show how you can
implement printing capabilities into your programs.</p>
<p class="docText">Finally, we cover two techniques for transferring data
between programs: the system clipboard and the drag-and-drop mechanism. You can
use these techniques to transfer data between two Java applications, or between
a Java application and a native program.</p>
<h3 class="docSection1Title" id="c7s1">The Rendering Pipeline</h3>
<p class="docText">The original JDK 1.0 had a very simple mechanism for drawing
shapes. You select color and paint mode, and call methods of the <tt>Graphics</tt>
class such as <tt>drawRect</tt> or <tt>fillOval</tt>. The Java 2D API supports
many more options.</p>
<ul>
<li>
<p class="docList">You can easily produce a wide variety of
<span class="docEmphasis">shapes.</span></li>
<li>
<p class="docList">You have control over the <span class="docEmphasis">stroke,</span>
the pen that traces shape boundaries.</li>
<li>
<p class="docList">You can <span class="docEmphasis">fill</span> shapes with
solid colors, varying hues, and repeating patterns.</li>
<li>
<p class="docList">You can use <span class="docEmphasis">transformations</span>
to move, scale, rotate, or stretch shapes.</li>
<li>
<p class="docList">You can <span class="docEmphasis">clip</span> shapes to
restrict them to arbitrary areas.</li>
<li>
<p class="docList">You can select <span class="docEmphasis">composition rules</span>
to describe how to combine the pixels of a new shape with existing pixels.</li>
<li>
<p class="docList">You can give <span class="docEmphasis">rendering hints</span>
to make trade-offs between speed and drawing quality.</li>
</ul>
<p class="docText">To draw a shape, you go through the following steps:</p>
<ol class="docList">
<li value="1">
<p class="docList">Obtain an object of the <tt>Graphics2D</tt> class. This
class is a subclass of the <tt>Graphics</tt> class. If you use a version of
the JDK that is enabled for Java 2D technology, methods such as <tt>paint</tt>
and <tt>paintComponent</tt> automatically receive an object of the <tt>
Graphics2D</tt> class. Simply use a cast, as follows:</p>
<pre>public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
. . .
}
</pre>
</li>
<li value="2">
<p class="docList">Use the <tt>setRenderingHints</tt> method to set rendering
hints: trade-offs between speed and drawing quality.</p>
<pre>RenderingHints hints = . . .;
g2.setRenderingHints(hints);
</pre>
</li>
<li value="3">
<p class="docList">Use the <tt>setStroke</tt> method to set the
<span class="docEmphasis">stroke.</span> The stroke is used to draw the
outline of the shape. You can select the thickness and choose among solid and
dotted lines.</p>
<pre>Stroke stroke = . . .;
g2.setStroke(stroke);
</pre>
</li>
<li value="4">
<p class="docList">Use the <tt>setPaint</tt> method to set the
<span class="docEmphasis">paint.</span> The paint is used to fill areas such
as the stroke path or the interior of a shape. You can create solid color
paint, paint with changing hues, or tiled fill patterns.</p>
<pre>Paint paint = . . .;
g2.setPaint(paint);
</pre>
</li>
<li value="5">
<p class="docList">Use the <tt>setClip</tt> method to set the clipping region.</p>
<pre>Shape clip = . . .;
g2.clip(clip);
</pre>
</li>
<li value="6">
<p class="docList">Use the <tt>setTransform</tt> method to set a
<span class="docEmphasis">transformation</span> from user space to device
space. You use transformations if it is easier for you to define your shapes
in a custom coordinate system than by using pixel coordinates.</p>
<pre>AffineTransform transform = . . .;
g2.transform(transform);
</pre>
</li>
<li value="7">
<p class="docList">Use the <tt>setComposite</tt> method to set a
<span class="docEmphasis">composition rule</span> that describes how to
combine the new pixels with the existing pixels.</p>
<pre>Composite composite = . . .;
g2.setComposite(composite);
</pre>
</li>
<li value="8">
<p class="docList">Create a shape. The Java 2D API supplies many shape objects
and methods to combine shapes.</p>
<pre>Shape shape = . . .;
</pre>
</li>
<li value="9">
<p class="docList">Draw or fill the shape. If you draw the shape, its outline
is stroked. If you fill the shape, the interior is painted.</p>
<pre>g2.draw(shape);
g2.fill(shape);
</pre>
</li>
</ol>
<p class="docText">Of course, in many practical circumstances, you don't need
all these steps. There are reasonable defaults for the settings of the 2D
graphics context. You only need to change the settings if you want to change the
defaults.</p>
<p class="docText">In the following sections, you will see how to describe
shapes, strokes, paints, transformations, and composition rules.</p>
<p class="docText">The various <tt>set</tt> methods simply set the state of the
2D graphics context. They don't cause any drawing. Similarly, when you construct
<tt>Shape</tt> objects, no drawing takes place. A shape is only rendered when
you call <tt>draw</tt> or <tt>fill</tt>. At that time, the new shape is computed
in a <span class="docEmphasis">rendering pipeline</span> (see
<a class="docLink" href="#ch07fig01">Figure 7-1</a>).</p>
<center>
<h5 id="ch07fig01" class="docFigureTitle">Figure 7-1. The rendering pipeline</h5>
<p>
<img alt="graphics/07fig01.gif" src="07fig01.gif" border="0" width="500" height="112"></p>
</center>
<p class="docText">In the rendering pipeline, the following steps take place to
render a shape.</p>
<ol class="docList">
<li value="1">
<p class="docList">The path of the shape is stroked.</li>
<li value="2">
<p class="docList">The shape is transformed.</li>
<li value="3">
<p class="docList">The shape is clipped. If there is no intersection between
the shape and the clipping area, then the process stops.</li>
<li value="4">
<p class="docList">The remainder of the shape after clipping is filled.</li>
<li value="5">
<p class="docList">The pixels of the filled shape are composed with the
existing pixels.(In <a class="docLink" href="#ch07fig01">Figure 7-1</a>, the
circle is part of the existing pixels, and the cup shape is superimposed over
it.)</li>
</ol>
<p class="docText">In the next section, you will see how to define shapes. Then,
we turn to the 2D graphics context settings.</p>
<h4 class="docSection2Title" id="ch07lev2sec1"><tt>java.awt.Graphics2D</tt></h4>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -