📄 graphics rendering.htm
字号:
<TD><A name=51872>Ends unclosed subpaths with a square projection that
extends beyond the end of the segment to a distance equal to half the
line width.</A><BR></TR></TBODY></TABLE>
<P>To set the endcap style, create a <CODE>BasicStroke</CODE> object with the
desired attribute. The following example sets the stroke width to 12 points
and endcap style is set to <CODE>CAP_ROUND</CODE>.
<P><PRE><HR>
wideStroke = new BasicStroke(12.0, BasicStroke.CAP_ROUND);
</PRE><PRE> g2.setStroke(roundStroke);
<HR></PRE><A name=51899>
<H4>10.2.2.3 <IMG src="Graphics Rendering.files/space.gif">Join
Style</H4></A><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#51903">Table
10-2</A> lists the join style attributes. These attributes affect the
appearance of line junctions.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B><A name=51903><I>Table 10-2 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Join Styles
</A></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=51909>Appearance </A>
<TH><A name=51911>Attribute </A>
<TH><A name=51913>Description </A>
<TR vAlign=top>
<TD><A name=51915>
<HR>
<CENTER><IMG
src="Graphics Rendering.files/Graphics.doc.anc4.gif"></CENTER>
<HR>
</A><BR>
<TD><A name=51917>JOIN_BEVEL</A><BR>
<TD><A name=51944>Joins path segments by connecting the outer corners of
their wide outlines with a straight segment.</A><BR>
<TR vAlign=top>
<TD><A name=51921>
<HR>
<CENTER><IMG
src="Graphics Rendering.files/Graphics.doc.anc5.gif"></CENTER>
<HR>
</A><BR>
<TD><A name=51923>JOIN_ROUND</A><BR>
<TD><A name=51978>Joins path segments by rounding off the corner at a
radius of half the line width.</A><BR>
<TR vAlign=top>
<TD><A name=51927>
<HR>
<CENTER><IMG
src="Graphics Rendering.files/Graphics.doc.anc6.gif"></CENTER>
<HR>
</A><BR>
<TD><A name=51929>JOIN_MITER</A><BR>
<TD><A name=51985>Joins path segments by extending their outside edges
until they meet.</A><BR></TR></TBODY></TABLE>
<P>To set the join style, create a <CODE>BasicStroke</CODE> object with the
desired attribute. The following example sets the stroke width to 12 points,
an endcap style of <CODE>CAP_ROUND</CODE>, and a join style of
<CODE>JOIN_ROUND</CODE>.
<P><PRE><HR>
wideStroke = new BasicStroke(12.0, BasicStroke.CAP_ROUND,
</PRE><PRE> BasicStroke.JOIN_ROUND);
</PRE><PRE> g2.setStroke(roundStroke);
<HR></PRE><A name=51991>
<H4>10.2.2.4 <IMG src="Graphics Rendering.files/space.gif">Stroke
Style</H4></A>The stroke style is defined by two parameters:
<P>
<UL>
<LI><CODE>dash</CODE> - an array that represents the dashing pattern.
Alternating elements in the array represent the dash size and the size of
the space between dashes. Element 0 represents the first dash, element 1
represents the first space.
<P></P></LI></UL>
<UL>
<LI><CODE>dash_phase</CODE> - an offset that defines where the dashing
pattern starts.
<P></P></LI></UL><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52134">Listing
10-1</A> shows a code sample in which two different dashing patterns are
created. In the first pattern, the size of the dashes and the space between
them is constant. The second pattern uses a six-element array to define the
dashing pattern. The two dash patterns are shown in <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52035">Figure
10-2</A>.
<P><CAPTION><FONT size=-1><B><A name=52134>
<CENTER><FONT size=-1><B><I>Listing 10-1 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Example Stroke
Styles</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="3" colspan="1"><PRE> // Define the first dashed line.
float dash1[] = {10.0f};
BasicStroke bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="3"
colspan="1"><PRE> g2.setStroke(bs);
Line2D line = new Line2D.Float(20.0f, 10.0f, 100.0f, 10.0f);
g2.draw(line);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD><PRE> // Define the second dashed line.
float[] dash2 = {6.0f, 4.0f, 2.0f, 4.0f, 2.0f, 4.0f};
bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash2, 0.0f);
g2.setStroke(bs);
g2.draw(line);
</PRE>
<HR>
<P><A name=52034>
<HR>
<CENTER><IMG src="Graphics Rendering.files/Graphics.doc.anc7.gif"></CENTER>
<HR>
</A><A name=52035>
<CENTER><FONT size=-1><B><I>Figure 10-2 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Example Stroke
Styles</B></FONT></CENTER></A>
<P><A name=52119>
<H4>10.2.2.5 <IMG src="Graphics Rendering.files/space.gif">Fill
Styles</H4></A>The <CODE>Paint</CODE> attribute in the <CODE>Graphics2D</CODE>
context defines the fill color or pattern used when text and
<CODE>Shape</CODE>s are rendered.
<P><A name=52121>
<H5>Filling a Shape with a Gradient</H5></A>The <CODE>GradientPaint</CODE>
class allows a shape to be filled with a gradient of one color to another.
When creating a <CODE>GradientPaint</CODE> object, you specify a beginning
position and color, and an ending position and color. The fill color changes
proportionally from one color to the other along the line connecting the two
positions, as shown in <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52622">Figure
10-3</A>.
<P>In all three stars, the gradient line extends from point P1 to point P2. In
the middle star, all of the points along the gradient line extending to the
left of P1 take the beginning color and the points to the right of P2 take the
ending color.
<P><A name=52621>
<HR>
<CENTER><IMG src="Graphics Rendering.files/Graphics.doc.anc8.gif"></CENTER>
<HR>
</A><A name=52622>
<CENTER><FONT size=-1><B><I>Figure 10-3 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Filling a Shape with a
Gradient</B></FONT></CENTER></A>
<P>To fill a shape with a gradient of one color to another:
<P>
<UL>1. Create a <CODE>GradientPaint</CODE> object
<P>2. Call <CODE>Graphics2D.setPaint</CODE>
<P>3. Create the <CODE>Shape</CODE> object
<P>4. Call <CODE>Graphics2D.fill(shape)</CODE>
<P></P></UL><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52145">Listing
10-2</A> shows sample code in which a rectangle is filled with a blue-green
gradient.
<P><CAPTION><FONT size=-1><B><A name=52145>
<CENTER><FONT size=-1><B><I>Listing 10-2 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Example Filling a
Rectangle with a Gradient</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="4" colspan="1"><PRE> GradientPaint gp = new GradientPaint(50.0f, 50.0f, Color.blue,
50.0f, 250.0f, Color.green);
g2.setPaint(gp);
g2.fillRect(50, 50, 200, 200);
</PRE><TR valign="top"><TR valign="top"><TR valign="top">
<HR>
<P><A name=52161>
<H5>Filling a Shape with a Texture</H5></A>The <CODE>TexturePaint</CODE> class
allows you to fill a shape with a repeating pattern. When you create a
<CODE>TexturePaint</CODE>, you specify a <CODE>BufferedImage</CODE> to use as
the pattern. You also pass the constructor a rectangle to define the
repetition frequency of the pattern.
<P>To fill a shape with a texture:
<P>
<UL>1. Create a <CODE>TexturePaint</CODE> object
<P>2. Call <CODE>Graphics2D.setPaint</CODE>
<P>3. Create the <CODE>Shape</CODE>
<P>4. Call <CODE>Graphics2D.fill(shape)</CODE>
<P></P></UL><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Graphics.doc.html#52179">Listing
10-3</A> shows sample code in which a shape is filled with texture.
<P><CAPTION><FONT size=-1><B><A name=52179>
<CENTER><FONT size=-1><B><I>Listing 10-3 </I><IMG
src="Graphics Rendering.files/sm-blank.gif" border=0> Example Filling a Shape
with Texture</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="4" colspan="1"><PRE> // Create a buffered image texture patch of size 5 X 5.
BufferedImage bi = new BufferedImage(5, 5,
BufferedImage.TYPE_INT_RGB);
Graphics2D big bi.createGraphics();
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD><PRE> // Render into the BufferedImage graphics to create the texture.
big.setColor(Color.green);
big.fillRect(0, 0, 5, 5);
big.setColor(Color.lightGray);
big.fillOval(0, 0, 5, 5);
</PRE><TR valign="top"><TD><PRE> // Create a texture paint from the buffered image.
Rectangle r = new Rectangle(0, 0, 5, 5);
TexturePaint tp = new
TexturePaint(bi, r, TexturePaint.NEAREST_NEIGHBOR);
</PRE><TR valign="top"><TD><PRE> // Add the texture paint to the graphics context.
g2.setPaint(tp);
</PRE><TR valign="top"><TD><PRE> // Create and render a rectangle filled with the texture.
g2.fillRect(0, 0, 200, 200);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -