📜  JavaFX 2D Shapes

📅  最后修改于: 2020-10-14 01:05:46             🧑  作者: Mango

JavaFX 2D形状

在某些应用程序中,我们需要向用户显示二维形状。但是,JavaFX提供了在屏幕上创建我们自己的2D形状的灵活性。

在我们的应用程序中,可以使用各种类来实现2D形状。所有这些类都位于javafx.scene.shape包中。

该软件包包含代表不同类型2D形状的类。类中有几种方法可以处理有关2D形状创建的坐标。

什么是2D形状?

通常,可以将二维形状定义为可以在由X和Y平面组成的坐标系上绘制的几何图形。但是,这与3D形状的不同之处在于2D形状的每个点始终由两个坐标(X,Y)组成。

使用JavaFX,我们可以创建2D形状,例如直线,矩形,圆形,椭圆形,多边形,三次曲线,四边形曲线,弧形等。javafx.scene.shape.Shape类是所有形状类的基类。

如何创建2D形状?

如前所述,每个形状都由包javafx.scene.shape的特定类表示。为了创建二维形状,需要遵循以下说明。

1.实例化各自的类:例如,Rectangle rect = new Rectangle()

2.使用实例设置器方法设置类的必需属性:例如,

rect.setX(10);
rect.setY(20); 
rect.setWidth(100);
rect.setHeight(100);

3.将类对象添加到“组”布局中:例如,

Group root = new Group(); 
root.getChildren().add(rect);

下表由JavaFX shape类及其描述组成。

Shape Description
Line In general, Line is the geometrical figure which joins two (X,Y) points on 2D coordinate system. In JavaFX, javafx.scene.shape.Line class needs to be instantiated in order to create lines.
Rectangle In general, Rectangle is the geometrical figure with two pairs of two equal sides and four right angles at their joint. In JavaFX, javafx.scene.shape.Rectangle class needs to be instantiated in order to create Rectangles.
Ellipse In general, ellipse can be defined as a curve with two focal points. The sum of the distances to the focal points are constant from each point of the ellipse. In JavaFX. javafx.scene.shape.Ellipse class needs to be instantiated in order to create Ellipse.
Arc Arc can be defined as the part of the circumference of the circle of ellipse. In JavaFX, javafx.scene.shape.Arc class needs to be instantiated in order to create Arcs.
Circle A circle is the special type of Ellipse having both the focal points at the same location. In JavaFX, Circle can be created by instantiating javafx.scene.shape.Circle class.
Polygon Polygon is a geometrical figure that can be created by joining the multiple Co-planner line segments. In JavaFX, javafx.scene.shape. Pollygon class needs to be instantiated in order to create polygon.
Cubic Curve A Cubic curve is a curve of degree 3 in the XY plane. In Javafx, javafx.scene.shape.CubicCurve class needs to be instantiated in order to create Cubic Curves.
Quad Curve A Quad Curve is a curve of degree 2 in the XY plane. In JavaFX, javafx.scene.shape.QuadCurve class needs to be instantiated in order to create QuadCurve.