- Mastering JavaFX 10
- Sergey Grinev
- 335字
- 2021-06-25 21:21:55
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 5, Animation, 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:

- 通信網(wǎng)絡(luò)基礎(chǔ)與設(shè)備
- 5G承載網(wǎng)網(wǎng)絡(luò)規(guī)劃與組網(wǎng)設(shè)計(jì)
- 物聯(lián)網(wǎng)檢驗(yàn)檢測(cè)技術(shù)
- Django 2 by Example
- Learning QGIS 2.0
- 網(wǎng)絡(luò)創(chuàng)新指數(shù)研究
- 互聯(lián)網(wǎng)基礎(chǔ)資源技術(shù)與應(yīng)用發(fā)展態(tài)勢(shì)(2021—2023)
- 網(wǎng)絡(luò)安全技術(shù)與解決方案(修訂版)
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見(jiàn):2015年中國(guó)互聯(lián)網(wǎng)安全大會(huì)文集
- 中小型局域網(wǎng)組建、管理與維護(hù)實(shí)戰(zhàn)
- Spring 5.0 Projects
- 圖解手機(jī)元器件維修技巧
- 5G技術(shù)與標(biāo)準(zhǔn)
- 網(wǎng)絡(luò)安全應(yīng)急響應(yīng)技術(shù)實(shí)戰(zhàn)指南
- 語(yǔ)音信號(hào)處理及Blackfin DSP實(shí)現(xiàn)