Documentation

The Java™ Tutorials
Hide TOC
Geometric Primitives几何图元
Trail: 2D Graphics
Lesson: Overview of the Java 2D API Concepts

Geometric Primitives几何图元

The Java 2D API provides a useful set of standard shapes such as points, lines, rectangles, arcs, ellipses, and curves. Java2D API提供了一组有用的标准形状,如点、线、矩形、圆弧、椭圆和曲线。The most important package to define common geometric primitives is the java.awt.geom package. 定义通用几何原语的最重要的包是java.awt.geom包。Arbitrary shapes can be represented by combinations of straight geometric primitives.任意形状可以由直线几何图元的组合表示。

The Shape interface represents a geometric shape, which has an outline and an interior. Shape接口表示具有轮廓和内部的几何形状。This interface provides a common set of methods for describing and inspecting two-dimensional geometric objects and supports curved line segments and multiple sub-shapes. 此接口提供了一组用于描述和检查二维几何对象的通用方法,并支持曲线段和多个子形状。The Graphics class supports only straight line segments. Graphics类仅支持直线段。The Shape interface can support curves segments.Shape接口可以支持曲线段。

For more details about how to draw and fill shapes, see the Working with Geometry lesson.有关如何绘制和填充形状的详细信息,请参阅使用几何体课程。

Points

The Point2D class defines a point representing a location in (x, y) coordinate space. Point2D类定义了一个点,表示(x, y)坐标空间中的位置。The term “point” in the Java 2D API is not the same as a pixel. 在Java 2D API中,这一术语“点”与像素并不相同。A point has no area, does not contain a color, and cannot be rendered.点没有区域,不包含颜色,并且无法渲染。

Points are used to create other shapes. 点用于创建其他形状。ThePoint2D class also includes a method for calculating the distance between two points.Point2D类还包括计算两点之间距离的方法。

Lines直线

The Line2D class is an abstract class that represents a line. Line2D类是表示线的抽象类。A line's coordinates can be retrieved as double. 直线的坐标可以双精度检索。The Line2D class includes several methods for setting a line's endpoints.Line2D类包括多个用于设置直线端点的方法。

You can also create a straight line segment by using the GeneralPath class described below.还可以使用下面描述的GeneralPath类创建直线段。

Rectangular Shapes矩形

The Rectangle2D, RoundRectangle2D, Arc2D, and Ellipse2D primitives are all derived from the RectangularShape class. Rectangle2DRoundRectangle2DArc2DEllipse2D原语都是从RectangleShape类派生的。This class defines methods for Shape objects that can be described by a rectangular bounding box. 此类定义可由矩形边界框描述的Shape对象的方法。The geometry of a RectangularShape object can be extrapolated from a rectangle that completely encloses the outline of the Shape.RectangularShape对象的几何图形可以从完全包围形状轮廓的矩形中推断出来。

Rectangular shape

Quadratic and Cubic Curves二次曲线和三次曲线

The QuadCurve2D enables you to create quadratic parametric curve segments. 使用QuadCurve2D可以创建二次参数化曲线段。A quadratic curve is defined by two endpoints and one control point.二次曲线由两个端点和一个控制点定义。

The CubicCurve2D class enables you to create cubic parametric curve segments. CubicCurve2D类可用于创建立方参数化曲线段。A cubic curve is defined by two endpoints and two control points. 三次曲线由两个端点和两个控制点定义。The following are examples of quadratic and cubic curves. 以下是二次曲线和三次曲线的示例。See Stroking and Filling Graphics Primitives for implementations of cubic and quadratic curves.有关三次曲线和二次曲线的实现,请参阅笔划和填充图形原语

This figure represents a quadratic curve.这个数字代表一条二次曲线。

Quadratic parametric curve

This figure represents a cubic curve.这个数字代表一条三次曲线。

Cubic parametric curve

Arbitrary Shapes任意形状

The GeneralPath class enables you to construct an arbitrary shape by specifying a series of positions along the shape's boundary. GeneralPath类允许您通过沿形状边界指定一系列位置来构造任意形状。These positions can be connected by line segments, quadratic curves, or cubic (Bézier) curves. 这些位置可以通过线段、二次曲线或三次(贝赛尔)曲线连接。The following shape can be created with three line segments and a cubic curve. 可以使用三条线段和一条三次曲线创建以下形状。See Stroking and Filling Graphics Primitivesfor more information about the implementation of this shape.有关此形状实现的详细信息,请参阅笔划和填充图形原语

This figure represents a polyshape created by using the GeneralPath class

Areas面积

With the Area class, you can perform boolean operations, such as union, intersection, and subtraction, on any two Shape objects. 使用Area类,可以对任意两个Shape对象执行布尔运算,例如并集、交集和减法。This technique, often referred to as constructive area geometry, enables you to quickly create complex Shape objects without having to describe each line segment or curve.此技术通常称为构造面积几何体,使您能够快速创建复杂Shape对象,而无需描述每条线段或曲线。


Previous page: Java 2D Rendering
Next page: Text