Javafx 简明教程

JavaFX - 2D Shapes

在上一章中,我们看到了 JavaFX 的基本应用,其中我们学会了如何创建一个空窗口以及如何在 JavaFX 的 XY 平面上绘制线。除了线以外,我们还可以绘制其他一些 2D 形状。

In the previous chapter, we have seen the basic application of JavaFX, where we learnt how to create an empty window and how to draw a line on an XY plane of JavaFX. In addition to the line, we can also draw several other 2D shapes.

2D Shape

通常,2D 形状是可以绘制在 XY 平面上的几何图形,包括线、矩形、圆等。

In general, a 2D shape is a geometrical figure that can be drawn on the XY plane, these include Line, Rectangle, Circle, etc.

使用 JavaFX 库,你可以绘制:

Using the JavaFX library, you can draw −

  1. Predefined shapes such as Line, Rectangle, Circle, Ellipse, Polygon, Polyline, Cubic Curve, Quad Curve, Arc.

  2. Path elements such as MoveTO Path Element, Line, Horizontal Line, Vertical Line, Cubic Curve, Quadratic Curve, Arc.

  3. In addition to these, you can also draw a 2D shape by parsing SVG path.

每个上述 2D 形状均用一个类表示,所有这些类都属于包 javafx.scene.shape 。名为 Shape 的类是 JavaFX 中所有二维形状的基类。

Each of the above mentioned 2D shape is represented by a class and all these classes belongs to the package javafx.scene.shape. The class named Shape is the base class of all the 2-Dimensional shapes in JavaFX.

Creating a 2D Shape in JavaFX

要创建图表,你需要:

To create a chart, you need to −

  1. Instantiate the respective class of the required shape.

  2. Set the properties of the shape.

  3. Add the shape object to the group.

Instantiating the Respective Class

要创建 2 维度形状,首先你需要实例化其各自的类。例如,如果你要创建一个圆,你需要按如下方式实例化名为 Circle 的类:

To create a 2 Dimensional shape, first of all you need to instantiate its respective class. For example, if you want to create a circle you need to instantiate the class named Circle as follows −

Circle circle = new Circle();

Setting the Properties of the Shape

在实例化类之后,你需要使用 setter 方法为形状设置属性。例如,你可以使用以下 setter 方法设置 Circle 类中心的 X 和 Y 坐标及半径:

After instantiating the class, you need to set the properties for the shape using the setter methods. For example, you can set the X, Y coordinates of the center of the Circle class and its radius, using the following setter methods −

// Setting the Properties of a circle
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
circle.setRadius(100.0f);

Adding the Shape Object to the Group

最后,你需要将圆对象添加到组,如下所示将其传递为构造函数的参数。

Finally, you need to add the circle object to the group by passing it as a parameter of the constructor as shown below.

//Creating a Group object
Group root = new Group(circle);

Various 2D Shapes available in JavaFX

下表列出了 JavaFX 提供的各种形状(类)。

The following table gives you the list of various shapes (classes) provided by JavaFX.

S.No

Shape & Description

1

LineA line is a geometrical structure joining two point. The Line class of the package javafx.scene.shape represents a line in the XY plane.

2

RectangleIn general, a rectangle is a four-sided polygon that has two pairs of parallel and concurrent sides with all interior angles as right angles. In JavaFX, a Rectangle is represented by a class named Rectangle. This class belongs to the package javafx.scene.shape.

3

Rounded RectangleIn JavaFX, you can draw a rectangle either with sharp edges or with arched edges and The one with arched edges is known as a rounded rectangle.

4

CircleA circle is a line forming a closed loop, every point on which is a fixed distance from a centre point. In JavaFX, a circle is represented by a class named Circle. This class belongs to the package javafx.scene.shape.

5

EllipseAn ellipse is defined by two points, each called a focus. If any point on the ellipse is taken, the sum of the distances to the focus points is constant. The size of the ellipse is determined by the sum of these two distances. In JavaFX, an ellipse is represented by a class named Ellipse. This class belongs to the package javafx.scene.shape.

6

PolygonA closed shape formed by a number of coplanar line segments connected end to end. In JavaFX, a polygon is represented by a class named Polygon. This class belongs to the package javafx.scene.shape.

7

PolylineA polyline is same a polygon except that a polyline is not closed in the end. Or, continuous line composed of one or more line segments. In JavaFX, a Polyline is represented by a class named Polygon. This class belongs to the package javafx.scene.shape.

8

Cubic CurveA cubic curve is a Bezier parametric curve in the XY plane is a curve of degree 3. In JavaFX, a Cubic Curve is represented by a class named CubicCurve. This class belongs to the package javafx.scene.shape.

9

QuadCurveA quadratic curve is a Bezier parametric curve in the XY plane is a curve of degree 2. In JavaFX, a QuadCurve is represented by a class named QuadCurve. This class belongs to the package javafx.scene.shape.

10

ArcAn arc is part of a curve. In JavaFX, an arc is represented by a class named Arc. This class belongs to the package − javafx.scene.shape. Types Of ArcIn addition to this we can draw three types of arc’s Open, Chord, Round.

11

SVGPathIn JavaFX, we can construct images by parsing SVG paths. Such shapes are represented by the class named SVGPath. This class belongs to the package javafx.scene.shape. This class has a property named content of String datatype. This represents the SVG Path encoded string, from which the image should be drawn..

Properties of 2D Objects

对于所有 2 维度对象,你可以设置各种属性,如填充、描边、描边类型等。下一节将讨论 2D 对象的各种属性。

For all the 2-Dimensional objects, you can set various properties like fill, stroke, StrokeType, etc. The following section discusses various properties of 2D objects.

我们将在本教程的后面对这些属性进行详细了解。

We will learn about these properties in detail, in further sections of this tutorial.

Operations on 2D Objects

如果我们向一个组中添加多个形状,第一个形状就会被第二个形状重叠,如下所示。

If we add more than one shape to a group, the first shape is overlapped by the second one as shown below.

operations on 2d objects

除了转换(旋转、缩放、平移等)、过渡(动画),你还可以对 2D 对象执行三种操作:即 - Union, SubtractionIntersection

In addition to the transformations (rotate, scale, translate, etc.), transitions (animations), you can also perform three operations on 2D objects namely − Union, Subtraction and Intersection.

  1. Union Operation: This operation takes two or more shapes as inputs and returns the area occupied by them.

  2. Intersection Operation: This operation takes two or more shapes as inputs and returns the intersection area between them.

  3. Subtraction Operation: This operation takes two or more shapes as an input. Then, it returns the area of the first shape excluding the area overlapped by the second one.