官术网_书友最值得收藏!

Curves

Arc is a piece of the ellipse. It has the extra startAngle and length properties compared to Ellipse. Both these parameters are measured in degrees, ranging from 0 to 360.

The following is an example of an ellipse and a similar arc with a length of 180 degrees:

// chapter2/shapes/ArcAndEllipse.java
Ellipse ellipse = new Ellipse();
ellipse.setRadiusX(60);
ellipse.setRadiusY(40);
ellipse.setFill(Color.DARKGREY);

Arc arc = new Arc();
arc.setRadiusX(60);
arc.setRadiusY(40);
arc.setFill(Color.DARKGREY);
arc.setStartAngle(45);
arc.setLength(180);

QuadCurve and CubicCurve represent quadratic and cubic Bezier parametric curves. This is a popular method for modeling a smooth curve. 

To draw a QuadCurve, you need to set a start point, an end point, and a control point for the curve. After that, JavaFX will draw a curve by shifting tangent with vertexes on the lines from the start point to the control point, and from the control point to the end point. 

It's easier than it sounds—luckily, we have a powerful JavaFX API, so I've created a small animation, demonstrating how it works. In the next screenshot, the gray area is a QuadCurve, the two black lines connect the start, end, and control points, and the red line is a moving tangent:

The actual sample is animated, but I'll provide only the curve code for it; see Chapter 5Animation, for the details about the animation API. Take a look at the following code snippet:

// chapter2/shapes/AnimatedQuadCurve.java
QuadCurve quad = new QuadCurve();
quad.setStartX(50);
quad.setStartY(200);
quad.setEndX(250);
quad.setEndY(200);
quad.setControlX(275);
quad.setControlY(20);
quad.setFill(Color.DARKGRAY);

// two lines connecting start, end and control points
Polyline lines = new Polyline(
quad.getStartX(), quad.getStartY(),
quad.getControlX(), quad.getControlY(),
quad.getEndX(), quad.getEndY());

// bold tangent line
Line tangent = new Line(quad.getStartX(), quad.getStartY(), quad.getControlX(), quad.getControlY());
tangent.setStroke(Color.RED);
tangent.setStrokeWidth(2);

CubicCurve is a step up from QuadCurve and has two control points:

CubicCurve cubic = new CubicCurve();
cubic.setStartX(50.0);
cubic.setStartY(200.0);
cubic.setControlX1(75.0);
cubic.setControlY1(30.0);
cubic.setControlX2(135.0);
cubic.setControlY2(170.0);
cubic.setEndX(250.0);
cubic.setEndY(190.0);
cubic.setFill(Color.DARKGRAY);

Polyline lines = new Polyline(
cubic.getStartX(), cubic.getStartY(),
cubic.getControlX1(), cubic.getControlY1(),
cubic.getControlX2(), cubic.getControlY2(),
cubic.getEndX(), cubic.getEndY());

By having two control points you can create a shape with an uneven curve:

主站蜘蛛池模板: 喀喇| 南皮县| 宝丰县| 仲巴县| 惠州市| 平塘县| 棋牌| 景泰县| 博兴县| 盐亭县| 宜州市| 泽库县| 陕西省| 格尔木市| 疏附县| 玛多县| 钦州市| 青州市| 南溪县| 东平县| 林西县| 鄯善县| 兰考县| 顺义区| 云南省| 西乌珠穆沁旗| 武胜县| 仁怀市| 三河市| 灵武市| 怀宁县| 宣恩县| 桃园市| 日喀则市| 镇原县| 玉山县| 隆尧县| 公主岭市| 湖州市| 鄂托克前旗| 同德县|