下面列出了怎么用javafx.scene.shape.ArcTo的API类实例代码及写法,或者点击链接到github查看源代码。
public static Node createIconContent() {
Path path = new Path();
path.getElements().addAll(
new MoveTo(25, 25),
new HLineTo(45),
new ArcTo(20, 20, 0, 80, 25, true, true)
);
path.setStroke(Color.web("#b9c0c5"));
path.setStrokeWidth(5);
path.getStrokeDashArray().addAll(15d,15d);
path.setFill(null);
javafx.scene.effect.InnerShadow effect = new javafx.scene.effect.InnerShadow();
effect.setOffsetX(1);
effect.setOffsetY(1);
effect.setRadius(3);
effect.setColor(Color.rgb(0,0,0,0.6));
path.setEffect(effect);
return path;
}
public static Node createIconContent() {
Path path = new Path();
path.getElements().addAll(
new MoveTo(25, 25),
new HLineTo(45),
new ArcTo(20, 20, 0, 80, 25, true, true)
);
path.setStroke(Color.web("#b9c0c5"));
path.setStrokeWidth(5);
path.getStrokeDashArray().addAll(15d,15d);
path.setFill(null);
javafx.scene.effect.InnerShadow effect = new javafx.scene.effect.InnerShadow();
effect.setOffsetX(1);
effect.setOffsetY(1);
effect.setRadius(3);
effect.setColor(Color.rgb(0,0,0,0.6));
path.setEffect(effect);
return path;
}
/**
* Draws the arrow-head for its current size and position values.
*/
public void draw() {
getElements().clear();
getElements().add(new MoveTo(x, y + length / 2));
getElements().add(new LineTo(x + width / 2, y - length / 2));
if (radius > 0) {
final ArcTo arcTo = new ArcTo();
arcTo.setX(x - width / 2);
arcTo.setY(y - length / 2);
arcTo.setRadiusX(radius);
arcTo.setRadiusY(radius);
arcTo.setSweepFlag(true);
getElements().add(arcTo);
} else {
getElements().add(new LineTo(x - width / 2, y - length / 2));
}
getElements().add(new ClosePath());
}
private static Path processPath(final List<String> PATH_LIST, final PathReader READER) {
final Path PATH = new Path();
PATH.setFillRule(FillRule.EVEN_ODD);
while (!PATH_LIST.isEmpty()) {
if ("M".equals(READER.read())) {
PATH.getElements().add(new MoveTo(READER.nextX(), READER.nextY()));
} else if ("L".equals(READER.read())) {
PATH.getElements().add(new LineTo(READER.nextX(), READER.nextY()));
} else if ("C".equals(READER.read())) {
PATH.getElements().add(new CubicCurveTo(READER.nextX(), READER.nextY(), READER.nextX(), READER.nextY(), READER.nextX(), READER.nextY()));
} else if ("Q".equals(READER.read())) {
PATH.getElements().add(new QuadCurveTo(READER.nextX(), READER.nextY(), READER.nextX(), READER.nextY()));
} else if ("H".equals(READER.read())) {
PATH.getElements().add(new HLineTo(READER.nextX()));
} else if ("L".equals(READER.read())) {
PATH.getElements().add(new VLineTo(READER.nextY()));
} else if ("A".equals(READER.read())) {
PATH.getElements().add(new ArcTo(READER.nextX(), READER.nextY(), 0, READER.nextX(), READER.nextY(), false, false));
} else if ("Z".equals(READER.read())) {
PATH.getElements().add(new ClosePath());
}
}
return PATH;
}
private Path createSegment(final double START_ANGLE, final double END_ANGLE, final double INNER_RADIUS, final double OUTER_RADIUS, final Color FILL, final Color STROKE, final TreeNode NODE) {
double startAngleRad = Math.toRadians(START_ANGLE + 90);
double endAngleRad = Math.toRadians(END_ANGLE + 90);
boolean largeAngle = Math.abs(END_ANGLE - START_ANGLE) > 180.0;
double x1 = centerX + INNER_RADIUS * Math.sin(startAngleRad);
double y1 = centerY - INNER_RADIUS * Math.cos(startAngleRad);
double x2 = centerX + OUTER_RADIUS * Math.sin(startAngleRad);
double y2 = centerY - OUTER_RADIUS * Math.cos(startAngleRad);
double x3 = centerX + OUTER_RADIUS * Math.sin(endAngleRad);
double y3 = centerY - OUTER_RADIUS * Math.cos(endAngleRad);
double x4 = centerX + INNER_RADIUS * Math.sin(endAngleRad);
double y4 = centerY - INNER_RADIUS * Math.cos(endAngleRad);
MoveTo moveTo1 = new MoveTo(x1, y1);
LineTo lineTo2 = new LineTo(x2, y2);
ArcTo arcTo3 = new ArcTo(OUTER_RADIUS, OUTER_RADIUS, 0, x3, y3, largeAngle, true);
LineTo lineTo4 = new LineTo(x4, y4);
ArcTo arcTo1 = new ArcTo(INNER_RADIUS, INNER_RADIUS, 0, x1, y1, largeAngle, false);
Path path = new Path(moveTo1, lineTo2, arcTo3, lineTo4, arcTo1);
path.setFill(FILL);
path.setStroke(STROKE);
String tooltipText = new StringBuilder(NODE.getData().getName()).append("\n").append(String.format(Locale.US, formatString, NODE.getData().getValue())).toString();
Tooltip.install(path, new Tooltip(tooltipText));
path.setOnMousePressed(new WeakEventHandler<>(e -> NODE.getTreeRoot().fireTreeNodeEvent(new TreeNodeEvent(NODE, EventType.NODE_SELECTED))));
return path;
}
public PathSample() {
super(180,90);
// Create path shape - square
Path path1 = new Path();
path1.getElements().addAll(
new MoveTo(25, 25),
new HLineTo(65),
new VLineTo(65),
new LineTo(25, 65),
new ClosePath()
);
path1.setFill(null);
path1.setStroke(Color.RED);
path1.setStrokeWidth(2);
// Create path shape - curves
Path path2 = new Path();
path2.getElements().addAll(
new MoveTo(100, 45),
new CubicCurveTo(120, 20, 130, 80, 140, 45),
new QuadCurveTo(150, 0, 160, 45),
new ArcTo(20, 40, 0, 180, 45, true, true)
);
path2.setFill(null);
path2.setStroke(Color.DODGERBLUE);
path2.setStrokeWidth(2);
// show the path shapes;
getChildren().add(new Group(path1, path2));
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Path 1 Stroke", path1.strokeProperty()),
new SimplePropertySheet.PropDesc("Path 2 Stroke", path2.strokeProperty())
);
// END REMOVE ME
}
public PathSample() {
super(180,90);
// Create path shape - square
Path path1 = new Path();
path1.getElements().addAll(
new MoveTo(25, 25),
new HLineTo(65),
new VLineTo(65),
new LineTo(25, 65),
new ClosePath()
);
path1.setFill(null);
path1.setStroke(Color.RED);
path1.setStrokeWidth(2);
// Create path shape - curves
Path path2 = new Path();
path2.getElements().addAll(
new MoveTo(100, 45),
new CubicCurveTo(120, 20, 130, 80, 140, 45),
new QuadCurveTo(150, 0, 160, 45),
new ArcTo(20, 40, 0, 180, 45, true, true)
);
path2.setFill(null);
path2.setStroke(Color.DODGERBLUE);
path2.setStrokeWidth(2);
// show the path shapes;
getChildren().add(new Group(path1, path2));
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Path 1 Stroke", path1.strokeProperty()),
new SimplePropertySheet.PropDesc("Path 2 Stroke", path2.strokeProperty())
);
// END REMOVE ME
}
private Path createSegment(final double START_ANGLE, final double END_ANGLE, final double INNER_RADIUS, final double OUTER_RADIUS, final Paint FILL, final Color STROKE, final TreeNode NODE) {
double startAngleRad = Math.toRadians(START_ANGLE + 90);
double endAngleRad = Math.toRadians(END_ANGLE + 90);
boolean largeAngle = Math.abs(END_ANGLE - START_ANGLE) > 180.0;
double x1 = centerX + INNER_RADIUS * Math.sin(startAngleRad);
double y1 = centerY - INNER_RADIUS * Math.cos(startAngleRad);
double x2 = centerX + OUTER_RADIUS * Math.sin(startAngleRad);
double y2 = centerY - OUTER_RADIUS * Math.cos(startAngleRad);
double x3 = centerX + OUTER_RADIUS * Math.sin(endAngleRad);
double y3 = centerY - OUTER_RADIUS * Math.cos(endAngleRad);
double x4 = centerX + INNER_RADIUS * Math.sin(endAngleRad);
double y4 = centerY - INNER_RADIUS * Math.cos(endAngleRad);
MoveTo moveTo1 = new MoveTo(x1, y1);
LineTo lineTo2 = new LineTo(x2, y2);
ArcTo arcTo3 = new ArcTo(OUTER_RADIUS, OUTER_RADIUS, 0, x3, y3, largeAngle, true);
LineTo lineTo4 = new LineTo(x4, y4);
ArcTo arcTo1 = new ArcTo(INNER_RADIUS, INNER_RADIUS, 0, x1, y1, largeAngle, false);
Path path = new Path(moveTo1, lineTo2, arcTo3, lineTo4, arcTo1);
path.setFill(FILL);
path.setStroke(STROKE);
String tooltipText = new StringBuilder(NODE.getItem().getName()).append("\n").append(String.format(Locale.US, formatString, NODE.getItem().getValue())).toString();
Tooltip.install(path, new Tooltip(tooltipText));
path.setOnMousePressed(new WeakEventHandler<>(e -> NODE.getTreeRoot().fireTreeNodeEvent(new TreeNodeEvent(NODE, EventType.NODE_SELECTED))));
return path;
}
private Path createSegment(final double START_ANGLE, final double END_ANGLE, final double INNER_RADIUS, final double OUTER_RADIUS, final Color FILL, final Color STROKE, final TreeNode<ChartData> NODE) {
double startAngleRad = Math.toRadians(START_ANGLE + 90);
double endAngleRad = Math.toRadians(END_ANGLE + 90);
boolean largeAngle = Math.abs(END_ANGLE - START_ANGLE) > 180.0;
double x1 = centerX + INNER_RADIUS * Math.sin(startAngleRad);
double y1 = centerY - INNER_RADIUS * Math.cos(startAngleRad);
double x2 = centerX + OUTER_RADIUS * Math.sin(startAngleRad);
double y2 = centerY - OUTER_RADIUS * Math.cos(startAngleRad);
double x3 = centerX + OUTER_RADIUS * Math.sin(endAngleRad);
double y3 = centerY - OUTER_RADIUS * Math.cos(endAngleRad);
double x4 = centerX + INNER_RADIUS * Math.sin(endAngleRad);
double y4 = centerY - INNER_RADIUS * Math.cos(endAngleRad);
MoveTo moveTo1 = new MoveTo(x1, y1);
LineTo lineTo2 = new LineTo(x2, y2);
ArcTo arcTo3 = new ArcTo(OUTER_RADIUS, OUTER_RADIUS, 0, x3, y3, largeAngle, true);
LineTo lineTo4 = new LineTo(x4, y4);
ArcTo arcTo1 = new ArcTo(INNER_RADIUS, INNER_RADIUS, 0, x1, y1, largeAngle, false);
Path path = new Path(moveTo1, lineTo2, arcTo3, lineTo4, arcTo1);
path.setFill(FILL);
path.setStroke(STROKE);
String tooltipText = new StringBuilder(NODE.getItem().getName()).append("\n").append(String.format(Locale.US, formatString, NODE.getItem().getValue())).toString();
Tooltip.install(path, new Tooltip(tooltipText));
path.setOnMousePressed(new WeakEventHandler<>(e -> NODE.getTreeRoot().fireTreeNodeEvent(new TreeNodeEvent(NODE, EventType.NODE_SELECTED))));
return path;
}
/** arc from current position, using the specified center coordinates, radius, and angle */
public void arcRel(double xc, double yc, double radius, double angle)
{
// arcTo seems to fail if sweep angle is greater than 360
if(angle >= FX.TWO_PI)
{
angle = FX.TWO_PI - 0.0000001;
}
else if(angle <= -FX.TWO_PI)
{
angle = - FX.TWO_PI + 0.0000001;
}
Point2D p = currentPos();
double a = Math.atan2(yc + yorigin - p.getY(), p.getX() - xc - xorigin);
double b = a - angle;
double xe = xorigin + xc + radius * Math.cos(b);
double ye = yorigin - yc - radius * Math.sin(b);
// arcTo sweep is explained here:
// https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/ArcTo.html
boolean large = (angle >= Math.PI);
boolean sweep = (angle > 0);
add(new ArcTo(radius, radius, 0, xe, ye, large, sweep));
}
/**
* Adds a circular detour arc to the path, to the given position.
*
* @param x the final x position of the arc
* @param y the final y position of the arc
*/
private void addArcTo(final double x, final double y) {
final ArcTo arcTo = new ArcTo();
arcTo.setRadiusX(DETOUR_RADIUS);
arcTo.setRadiusY(DETOUR_RADIUS);
arcTo.setSweepFlag(sign > 0);
arcTo.setX(GeometryUtils.moveOffPixel(x));
arcTo.setY(GeometryUtils.moveOffPixel(y));
getPathElements().add(arcTo);
}
private void initGraphics() {
// Set initial size
if (Double.compare(gauge.getPrefWidth(), 0.0) <= 0 || Double.compare(gauge.getPrefHeight(), 0.0) <= 0 ||
Double.compare(gauge.getWidth(), 0.0) <= 0 || Double.compare(gauge.getHeight(), 0.0) <= 0) {
if (gauge.getPrefWidth() > 0 && gauge.getPrefHeight() > 0) {
gauge.setPrefSize(gauge.getPrefWidth(), gauge.getPrefHeight());
} else {
gauge.setPrefSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
}
}
unitText = new Text(gauge.getUnit());
unitText.setTextOrigin(VPos.CENTER);
unitText.setFill(gauge.getUnitColor());
Helper.enableNode(unitText, !gauge.getUnit().isEmpty());
titleText = new Text(gauge.getTitle());
titleText.setTextOrigin(VPos.CENTER);
titleText.setFill(gauge.getTitleColor());
Helper.enableNode(titleText, !gauge.getTitle().isEmpty());
valueText = new Text(formatNumber(gauge.getLocale(), gauge.getFormatString(), gauge.getDecimals(), gauge.getCurrentValue()));
valueText.setTextOrigin(VPos.CENTER);
valueText.setFill(gauge.getValueColor());
Helper.enableNode(valueText, gauge.isValueVisible());
minValue = gauge.getMinValue();
minText = new Text(String.format(locale, otherFormatString, minValue));
minText.setTextOrigin(VPos.CENTER);
minText.setFill(gauge.getValueColor());
maxText = new Text(String.format(locale, otherFormatString, gauge.getMaxValue()));
maxText.setTextOrigin(VPos.CENTER);
maxText.setFill(gauge.getValueColor());
boolean tickLabelsVisible = gauge.getTickLabelsVisible();
Helper.enableNode(minText, tickLabelsVisible);
Helper.enableNode(maxText, tickLabelsVisible);
innerShadow = new InnerShadow(BlurType.TWO_PASS_BOX, Color.rgb(0, 0, 0, 0.3), 30.0, 0.0, 0.0, 10.0);
barBackgroundStart = new MoveTo(0, 0.675 * PREFERRED_HEIGHT);
barBackgroundOuterArc = new ArcTo(0.675 * PREFERRED_HEIGHT, 0.675 * PREFERRED_HEIGHT, 0, PREFERRED_WIDTH, 0.675 * PREFERRED_HEIGHT, true, true);
barBackgroundLineToInnerArc = new LineTo(0.72222 * PREFERRED_WIDTH, 0.675 * PREFERRED_HEIGHT);
barBackgroundInnerArc = new ArcTo(0.3 * PREFERRED_HEIGHT, 0.3 * PREFERRED_HEIGHT, 0, 0.27778 * PREFERRED_WIDTH, 0.675 * PREFERRED_HEIGHT, false, false);
barBackground = new Path();
barBackground.setFillRule(FillRule.EVEN_ODD);
barBackground.getElements().add(barBackgroundStart);
barBackground.getElements().add(barBackgroundOuterArc);
barBackground.getElements().add(barBackgroundLineToInnerArc);
barBackground.getElements().add(barBackgroundInnerArc);
barBackground.getElements().add(new ClosePath());
barBackground.setFill(gauge.getBarBackgroundColor());
barBackground.setStroke(gauge.getBorderPaint());
barBackground.setEffect(gauge.isShadowsEnabled() ? innerShadow : null);
dataBarStart = new MoveTo(0, 0.675 * PREFERRED_HEIGHT);
dataBarOuterArc = new ArcTo(0.675 * PREFERRED_HEIGHT, 0.675 * PREFERRED_HEIGHT, 0, 0, 0, false, true);
dataBarLineToInnerArc = new LineTo(0.27778 * PREFERRED_WIDTH, 0.675 * PREFERRED_HEIGHT);
dataBarInnerArc = new ArcTo(0.3 * PREFERRED_HEIGHT, 0.3 * PREFERRED_HEIGHT, 0, 0, 0, false, false);
dataBar = new Path();
dataBar.setFillRule(FillRule.EVEN_ODD);
dataBar.getElements().add(dataBarStart);
dataBar.getElements().add(dataBarOuterArc);
dataBar.getElements().add(dataBarLineToInnerArc);
dataBar.getElements().add(dataBarInnerArc);
dataBar.getElements().add(new ClosePath());
dataBar.setFill(gauge.getBarColor());
dataBar.setStroke(gauge.getBorderPaint());
dataBar.setEffect(gauge.isShadowsEnabled() ? innerShadow : null);
threshold = new Line();
threshold.setStrokeLineCap(StrokeLineCap.BUTT);
Helper.enableNode(threshold, gauge.isThresholdVisible());
thresholdText = new Text(String.format(locale, formatString, gauge.getThreshold()));
Helper.enableNode(thresholdText, gauge.isThresholdVisible());
pane = new Pane(unitText, titleText, valueText, minText, maxText, barBackground, dataBar, threshold, thresholdText);
pane.setBorder(new Border(new BorderStroke(gauge.getBorderPaint(), BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(gauge.getBorderWidth()))));
pane.setBackground(new Background(new BackgroundFill(gauge.getBackgroundPaint(), CornerRadii.EMPTY, Insets.EMPTY)));
getChildren().setAll(pane);
}
public PathBuilder arcTo(double x, double y, double rx, double ry, boolean sweep, boolean large) {
elements.add(new ArcTo(rx, ry, 0, x, y, large, sweep));
return this;
}
public static String convertPath(final Path PATH) {
final StringBuilder fxPath = new StringBuilder();
for (PathElement element : PATH.getElements()) {
if (MoveTo.class.equals(element.getClass())) {
fxPath.append("M ")
.append(((MoveTo) element).getX()).append(" ")
.append(((MoveTo) element).getY()).append(" ");
} else if (LineTo.class.equals(element.getClass())) {
fxPath.append("L ")
.append(((LineTo) element).getX()).append(" ")
.append(((LineTo) element).getY()).append(" ");
} else if (CubicCurveTo.class.equals(element.getClass())) {
fxPath.append("C ")
.append(((CubicCurveTo) element).getControlX1()).append(" ")
.append(((CubicCurveTo) element).getControlY1()).append(" ")
.append(((CubicCurveTo) element).getControlX2()).append(" ")
.append(((CubicCurveTo) element).getControlY2()).append(" ")
.append(((CubicCurveTo) element).getX()).append(" ")
.append(((CubicCurveTo) element).getY()).append(" ");
} else if (QuadCurveTo.class.equals(element.getClass())) {
fxPath.append("Q ")
.append(((QuadCurveTo) element).getControlX()).append(" ")
.append(((QuadCurveTo) element).getControlY()).append(" ")
.append(((QuadCurveTo) element).getX()).append(" ")
.append(((QuadCurveTo) element).getY()).append(" ");
} else if (ArcTo.class.equals(element.getClass())) {
fxPath.append("A ")
.append(((ArcTo) element).getX()).append(" ")
.append(((ArcTo) element).getY()).append(" ")
.append(((ArcTo) element).getRadiusX()).append(" ")
.append(((ArcTo) element).getRadiusY()).append(" ");
} else if (HLineTo.class.equals(element.getClass())) {
fxPath.append("H ")
.append(((HLineTo) element).getX()).append(" ");
} else if (VLineTo.class.equals(element.getClass())) {
fxPath.append("V ")
.append(((VLineTo) element).getY()).append(" ");
} else if (ClosePath.class.equals(element.getClass())) {
fxPath.append("Z");
}
}
return fxPath.toString();
}