Documentation

The Java™ Tutorials
Hide TOC
Drawing Geometric Primitives绘制几何图元
Trail: 2D Graphics
Lesson: Working with Geometry

Drawing Geometric Primitives绘制几何图元

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;
    ...
}

Point

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.FloatPoint2D.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, Point2D.Double().要创建坐标为0,0的点,请使用默认构造函数Point2D.Double()
You can use the setLocation method to set the position of the point as follows:可以使用setLocation方法设置点的位置,如下所示:

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类具有计算当前点与具有给定坐标的点之间的距离或两点之间距离的方法。

Line直线

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.FloatLine2D.Double子类以Float和Double精度指定直线。For example:例如:

// draw Line2D.Double
g2.draw(new Line2D.Double(x1, y1, x2, y2));

Line

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类的构造函数指定直线的端点,如下所示:

Use the Stroke object in the Graphics2D class to define the stroke for the line path.使用Graphics2D类中的描边对象定义直线路径的描边。

Curves曲线

The java.awt.geom package enables you to create a quadratic or cubic curve segment.使用java.awt.geom包可以创建二次或三次曲线段。

Quadratic Curve Segment二次曲线段

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.FloatQuadCurve2D.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);

Quadratic parametric curve segment

Cubic Curve Segment三次曲线段

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.FloatCubicCurve2D.Double子类以Float和Double精度指定一条三次曲线。

The CubicCurve2D class has similar methods for setting the curve as the QuadraticCurve2Dclass, 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);
Cubic Curve Segment

Rectangle矩形

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.FloatRectangle2D.Double子类在Float和Double精度中指定一个矩形。For example:例如:

// draw Rectangle2D.Double
g2.draw(new Rectangle2D.Double(x, y,
                               rectwidth,
                               rectheight));

Rectangle

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.FloatRoundRectangle2D.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));

Rounded Rectangle

Ellipse椭圆

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.FloatEllipse2D.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));

Ellipse

Arc

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.FloatArc2D.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类定义以下三种类型的圆弧,由此类中相应的常量表示:OPENPIECHORD

Arc

Several methods set the size and parameters of the arc:有几种方法可以设置圆弧的大小和参数:

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));

Arc

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规范。


Previous page: Working with Geometry
Next page: Drawing Arbitrary Shapes