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发行说明。
The Java 2D API provides several classes that define common geometric objects such as points, lines, curves, and rectangles. Java2D API提供了几个类来定义常见的几何对象,如点、线、曲线和矩形。These geometry classes are part of the 这些几何体类是java.awt.geom
package.java.awt.geom
包的一部分。
The PathIterator
interface defines methods for retrieving elements from a path.PathIterator
接口定义了从路径检索元素的方法。
The Shape
interface provides a set of methods for describing and inspecting geometric path objects. Shape
界面提供了一组用于描述和检查几何路径对象的方法。This interface is implemented by the 此接口由GeneralPath
class and other geometry classes.GeneralPath
类和其他几何体类实现。
All examples represented in this section create geometries by using 本节中的所有示例都使用java.awt.geom
package and then render them by using the Graphics2D
class. java.awt.geom
包创建几何图形,然后使用Graphics2D
类进行渲染。To begin you obtain a 首先,获取Graphics2D
object, for example by casting the Graphics
parameter of the paint()
method.Graphics2D
对象,例如通过强制转换paint()
方法的Graphics
参数。
public void paint (Graphics g) { Graphics2D g2 = (Graphics2D) g; ... }
The Point
class creates a point representing a location in (x,y) coordinate space. Point
类创建一个点,表示(x, y)坐标空间中的位置。The subclasses 子类Point2D.Float
and Point2D.Double
provide correspondingly float and double precision for storing the coordinates of the point.Point2D.Float
和Point2D.Double
为存储点的坐标提供了相应的Float和Double精度。
//Create Point2D.Double Point2D.Double point = new Point2D.Double(x, y);
To create a point with the coordinates 0,0 you use the default constructor, 要创建坐标为0,0的点,请使用默认构造函数Point2D.Double()
.Point2D.Double()
。
You can use the 可以使用setLocation
method to set the position of the point as follows:setLocation
方法设置点的位置,如下所示:
setLocation(double x, double y)
– setLocation(Point2D p)
– Also, the 此外,Point2D
class has methods to calculate the distance between the current point and a point with given coordinates, or the distance between two points.Point2D
类具有计算当前点与具有给定坐标的点之间的距离或两点之间距离的方法。
The Line2D
class represents a line segment in (x, y) coordinate space. Line2D
类表示(x, y)坐标空间中的线段。The Line2D.Float
and Line2D.Double
subclasses specify lines in float and double precision. Line2D.Float
和Line2D.Double
子类以Float和Double精度指定直线。For example:例如:
// draw Line2D.Double g2.draw(new Line2D.Double(x1, y1, x2, y2));
This class includes several 这个类包括几个setLine()
methods to define the endpoints of the line.setLine()
方法来定义线的端点。
Alternatively, the endpoints of the line could be specified by using the constructor for the 或者,可以使用Line2D.Float
class as follows:Line2D.Float
类的构造函数指定直线的端点,如下所示:
Line2D.Float(float X1, float Y1, float X2, float Y2)
Line2D.Float(Point2D p1, Point2D p2)
Use the Stroke object in the 使用Graphics2D
class to define the stroke for the line path.Graphics2D
类中的描边对象定义直线路径的描边。
The 使用java.awt.geom
package enables you to create a quadratic or cubic curve segment.java.awt.geom
包可以创建二次或三次曲线段。
The QuadCurve2D
class implements the Shape
interface. QuadCurve2D
类实现Shape
接口。This class represents a quadratic parametric curve segment in (x, y) coordinate space. 此类表示(x,y)坐标空间中的二次参数曲线段。The QuadCurve2D.Float
and QuadCurve2D.Double
subclasses specify a quadratic curve in float and double precision.QuadCurve2D.Float
和QuadCurve2D.Double
子类以Float和Double精度指定二次曲线。
Several 有几种setCurve
methods are used to specify two endpoints and a control point of the curve, whose coordinates can be defined directly, by the coordinates of other points and by using a given array.setCurve
方法用于指定曲线的两个端点和一个控制点,其坐标可以通过其他点的坐标和使用给定数组直接定义。
A very useful method, 一种非常有用的方法setCurve(QuadCurve2D)
, sets the quadratic curve with the same endpoints and the control point as a supplied curve. setCurve(QuadCurve2D)
将具有相同端点和控制点的二次曲线设置为提供的曲线。For example:例如:
// create new QuadCurve2D.Float QuadCurve2D q = new QuadCurve2D.Float(); // draw QuadCurve2D.Float with set coordinates q.setCurve(x1, y1, ctrlx, ctrly, x2, y2); g2.draw(q);
The CubicCurve2D
class also implements the Shape
interface. CubicCurve2D
类还实现了Shape
接口。This class represents a cubic parametric curve segment in (x, y) coordinate space. 此类表示(x, y)坐标空间中的立方参数化曲线段。CubicCurve2D.Float
and CubicCurve2D.Double
subclasses specify a cubic curve in float and double precision.CubicCurve2D.Float
和CubicCurve2D.Double
子类以Float和Double精度指定一条三次曲线。
The CubicCurve2D
class has similar methods for setting the curve as the QuadraticCurve2D
class, except with a second control point. CubicCurve2D
类具有类似的方法将曲线设置为QuadraticCurve2D
类,但具有第二个控制点的情况除外。For example:例如:
// create new CubicCurve2D.Double CubicCurve2D c = new CubicCurve2D.Double(); // draw CubicCurve2D.Double with set coordinates c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); g2.draw(c);
Classes that specify primitives represented in the following example extend the 指定以下示例中表示的基本体的类扩展了RectangularShape
class, which implements the Shape
interface and adds a few methods of its own.RectangularShape
类,该类实现了Shape
接口并添加了一些自己的方法。
These methods enables you to get information about a shape's location and size, to examine the center point of a rectangle, and to set the bounds of the shape.这些方法使您能够获取有关形状位置和大小的信息,检查矩形的中心点,并设置形状的边界。
The Rectangle2D
class represents a rectangle defined by a location (x, y) and dimension (w x h). Rectangle2D
类表示由位置(x, y)
和尺寸(w x h)
定义的矩形。The Rectangle2D.Float
and Rectangle2D.Double
subclasses specify a rectangle in float and double precision. Rectangle2D.Float
和Rectangle2D.Double
子类在Float和Double精度中指定一个矩形。For example:例如:
// draw Rectangle2D.Double g2.draw(new Rectangle2D.Double(x, y, rectwidth, rectheight));
The RoundRectangle2D
class represents a rectangle with rounded corners defined by a location (x, y), a dimension (w x h), and the width and height of the corner arc. RoundRectangle2D
类表示具有圆角的矩形,圆角由位置(x, y)
、尺寸(w x h)
以及角弧的宽度和高度定义。The RoundRectangle2D.Float
and RoundRectangle2D.Double
subclasses specify a round rectangle in float and double precision.RoundRectangle2D.Float
和RoundRectangle2D.Double
子类在Float和Double精度中指定一个圆形矩形。
The rounded rectangle is specified with following parameters:圆角矩形由以下参数指定:
To set the location, size, and arcs of a 要设置RoundRectangle2D
object, use the method setRoundRect(double a, double y, double w, double h, double arcWidth, double arcHeight)
. RoundRectangle2D
对象的位置、大小和圆弧,请使用方法setRoundRect(double a, double y, double w, double h, double arcWidth, double arcHeight)
。For example:例如:
// draw RoundRectangle2D.Double g2.draw(new RoundRectangle2D.Double(x, y, rectwidth, rectheight, 10, 10));
The Ellipse2D
class represents an ellipse defined by a bounding rectangle. Ellipse2D
类表示由边界矩形定义的椭圆。The Ellipse2D.Float
and Ellipse2D.Double
subclasses specify an ellipse in float and double precision.Ellipse2D.Float
和Ellipse2D.Double
子类在Float和Double精度中指定椭圆。
Ellipse is fully defined by a location, a width and a height. 椭圆完全由位置、宽度和高度定义。For example:例如:
// draw Ellipse2D.Double g2.draw(new Ellipse2D.Double(x, y, rectwidth, rectheight));
To draw a piece of an ellipse, you use the 要绘制椭圆的一部分,请使用Arc2D
class. Arc2D
类。This class represents an arc defined by a bounding rectangle, a start angle, an angular extent, and a closure type. 此类表示由边界矩形、起始角度、角度范围和闭合类型定义的圆弧。The Arc2D.Float
and Arc2D.Double
subclasses specify an arc in float and double precision.Arc2D.Float
和Arc2D.Double
子类在Float和Double精度中指定圆弧。
The Arc2D
class defines the following three types of arcs, represented by corresponding constants in this class: OPEN, PIE and CHORD.Arc2D
类定义以下三种类型的圆弧,由此类中相应的常量表示:OPEN
、PIE
和CHORD
。
Several methods set the size and parameters of the arc:有几种方法可以设置圆弧的大小和参数:
Point2D
and Dimension2D
Point2D
和Dimension2D
Arc2D
Arc2D
Also, you can use the 此外,还可以使用setArcByCenter
method to specify an arc from a center point, given by its coordinates and a radius.setArcByCenter
方法指定从中心点开始的圆弧,由其坐标和半径给定。
// draw Arc2D.Double g2.draw(new Arc2D.Double(x, y, rectwidth, rectheight, 90, 135, Arc2D.OPEN));
The ShapesDemo2D.java
code example contains implementations of all described geometric primitives. ShapesDemo2D.java
代码示例包含所有描述的几何原语的实现。For more information about classes and methods represented in this section, see the 有关本节中表示的类和方法的更多信息,请参阅java.awt.geom
specification.java.awt.geom
规范。