The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
You have already learned how to draw most of shapes represented in the 您已经学习了如何绘制java.awt.geom
package. java.awt.geom
包中表示的大多数形状。To create more complicated geometry, such as polygons, polylines, or stars you use another class from this package, 要创建更复杂的几何体,例如多边形、多段线或星形,请使用此软件包中的另一个类GeneralPath
.GeneralPath
。
This class implements the 此类实现Shape
interface and represents a geometric path constructed from lines, and quadratic and cubic curves. Shape
接口,并表示由直线、二次曲线和三次曲线构造的几何路径。The three constructors in this class can create the 此类中的三个构造函数可以创建具有默认缠绕规则(GeneralPath
object with the default winding rule (WIND_NON_ZERO
), the given winding rule (WIND_NON_ZERO
or WIND_EVEN_ODD
), or the specified initial coordinate capacity. WIND_NON_ZERO
)、给定缠绕规则(WIND_NON_ZERO
或WIND_EVEN_ODD
)或指定初始坐标容量的GeneralPath
对象。The winding rule specifies how the interior of a path is determined.缠绕规则指定如何确定路径的内部。
public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; ... }
To create an empty 要创建空的GeneralPath
instance call new GeneralPath()
and then add segments to the shape by using the following methods:GeneralPath
实例,请调用new GeneralPath()
,然后使用以下方法将线段添加到形状中:
moveTo(float x, float y)
– lineTo(float x, float y)
– quadTo(float ctrlx, float ctrly, float x2, floaty2)
– curveTo(float ctrlx1, float ctrly1, float ctrlx2, float ctrly2, float x3, floaty3)
– closePath()
– The following example illustrates how to draw a polyline by using 以下示例说明了如何使用GeneralPath
:GeneralPath
绘制多段线:
// draw GeneralPath (polyline) int x2Points[] = {0, 100, 0, 100}; int y2Points[] = {0, 50, 50, 0}; GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD, x2Points.length); polyline.moveTo (x2Points[0], y2Points[0]); for (int index = 1; index < x2Points.length; index++) { polyline.lineTo(x2Points[index], y2Points[index]); }; g2.draw(polyline); |
![]() |
This example illustrates how to draw a polygon by using 此示例演示了如何使用GeneralPath
:GeneralPath
绘制多边形:
// draw GeneralPath (polygon) int x1Points[] = {0, 100, 0, 100}; int y1Points[] = {0, 50, 50, 0}; GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, x1Points.length); polygon.moveTo(x1Points[0], y1Points[0]); for (int index = 1; index < x1Points.length; index++) { polygon.lineTo(x1Points[index], y1Points[index]); }; polygon.closePath(); g2.draw(polygon); |
![]() |
Note that the only difference between two last code examples is the 请注意,最后两个代码示例之间的唯一区别是closePath()
method. closePath()
方法。This method makes a polygon from a polyline by drawing a straight line back to the coordinates of the last 此方法通过将直线绘制回上一个moveTo
.moveTo
的坐标,从多段线生成多边形。
To add a specific path to the end of your 要将特定路径添加到GeneralPath
object you use one of the append()
methods. GeneralPath
对象的末尾,可以使用append()
方法之一。The ShapesDemo2D.java
code example contains additional implementations of arbitrary shapes.ShapesDemo2D.java
代码示例包含任意形状的其他实现。