下面列出了怎么用javafx.scene.shape.Arc的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void initializeParts() {
double center = ARTBOARD_HEIGHT * 0.5;
border = new Circle(center, center, center);
border.getStyleClass().add("border");
pieSlice = new Arc(center, center, center - 1, center - 1, 90, 0);
pieSlice.getStyleClass().add("pieSlice");
pieSlice.setType(ArcType.ROUND);
valueField = new TextField();
valueField.relocate(ARTBOARD_HEIGHT + 5, 2);
valueField.setPrefWidth(ARTBOARD_WIDTH - ARTBOARD_HEIGHT - 5);
valueField.getStyleClass().add("valueField");
// always needed
drawingPane = new Pane();
drawingPane.setMaxSize(ARTBOARD_WIDTH, ARTBOARD_HEIGHT);
drawingPane.setMinSize(ARTBOARD_WIDTH, ARTBOARD_HEIGHT);
drawingPane.setPrefSize(ARTBOARD_WIDTH, ARTBOARD_HEIGHT);
}
@Override protected void handleEvents(final String EVENT_TYPE) {
super.handleEvents(EVENT_TYPE);
if ("VISIBILITY".equals(EVENT_TYPE)) {
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
Helper.enableNode(text, tile.isTextVisible());
Helper.enableNode(dateText, tile.isDateVisible());
Helper.enableNode(second, tile.isSecondsVisible());
Helper.enableNode(sectionsPane, tile.getSectionsVisible());
} else if ("SECTION".equals(EVENT_TYPE)) {
sectionMap.clear();
for (TimeSection section : tile.getTimeSections()) { sectionMap.put(section, new Arc()); }
sectionsPane.getChildren().setAll(sectionMap.values());
resize();
redraw();
}
}
public CenterArc() {
fillColor = Color.color(Math.random(), Math.random(), Math.random());
strokeColor = Color.YELLOW;
width = 120;
height = 120;
x = (Game.getInstance().getRender().getWidth() - width) / 2;
y = (Game.getInstance().getRender().getHeight() - height) / 2;
for (int i = 0; i < 4; i++) {
Arc arc = new Arc(x, y, width, height, i * 90, 30);
arc.setType(ArcType.ROUND);
shapes.add(arc);
}
effect = new GaussianBlur();
start();
}
public ArcSample() {
super(180,90);
// Simple red filled arc
Arc arc1 = new Arc(45,60,45,45,40,100);
arc1.setFill(Color.RED);
// Blue stroked arc
Arc arc2 = new Arc(155,60,45,45,40,100);
arc2.setStroke(Color.DODGERBLUE);
arc2.setFill(null);
// Create a group to show all the arcs);
getChildren().add(new Group(arc1,arc2));
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Arc 1 Fill", arc1.fillProperty()),
new SimplePropertySheet.PropDesc("Arc 1 Start Angle", arc1.startAngleProperty(), 0d, 360d),
new SimplePropertySheet.PropDesc("Arc 1 Length", arc1.lengthProperty(), 0d, 360d),
new SimplePropertySheet.PropDesc("Arc 2 Stroke", arc2.strokeProperty()),
new SimplePropertySheet.PropDesc("Arc 2 Stroke Width", arc2.strokeWidthProperty(), 1d, 5d),
new SimplePropertySheet.PropDesc("Arc 2 Radius X", arc2.radiusXProperty(), 0d, 50d),
new SimplePropertySheet.PropDesc("Arc 2 Radius Y", arc2.radiusYProperty(), 0d, 50d)
);
// END REMOVE ME
}
public ArcSample() {
super(180,90);
// Simple red filled arc
Arc arc1 = new Arc(45,60,45,45,40,100);
arc1.setFill(Color.RED);
// Blue stroked arc
Arc arc2 = new Arc(155,60,45,45,40,100);
arc2.setStroke(Color.DODGERBLUE);
arc2.setFill(null);
// Create a group to show all the arcs);
getChildren().add(new Group(arc1,arc2));
// REMOVE ME
setControls(
new SimplePropertySheet.PropDesc("Arc 1 Fill", arc1.fillProperty()),
new SimplePropertySheet.PropDesc("Arc 1 Start Angle", arc1.startAngleProperty(), 0d, 360d),
new SimplePropertySheet.PropDesc("Arc 1 Length", arc1.lengthProperty(), 0d, 360d),
new SimplePropertySheet.PropDesc("Arc 2 Stroke", arc2.strokeProperty()),
new SimplePropertySheet.PropDesc("Arc 2 Stroke Width", arc2.strokeWidthProperty(), 1d, 5d),
new SimplePropertySheet.PropDesc("Arc 2 Radius X", arc2.radiusXProperty(), 0d, 50d),
new SimplePropertySheet.PropDesc("Arc 2 Radius Y", arc2.radiusYProperty(), 0d, 50d)
);
// END REMOVE ME
}
@Override protected void handleEvents(final String EVENT_TYPE) {
super.handleEvents(EVENT_TYPE);
if ("VISIBILITY".equals(EVENT_TYPE)) {
Helper.enableNode(titleText, !tile.getTitle().isEmpty());
Helper.enableNode(valueText, tile.isValueVisible());
Helper.enableNode(sectionPane, tile.getSectionsVisible());
Helper.enableNode(thresholdRect, tile.isThresholdVisible());
Helper.enableNode(thresholdText, tile.isThresholdVisible());
Helper.enableNode(unitFlow, !tile.getUnit().isEmpty());
sectionsVisible = tile.getSectionsVisible();
} else if ("SECTION".equals(EVENT_TYPE)) {
sections = tile.getSections();
sectionMap.clear();
for(Section section : sections) { sectionMap.put(section, new Arc()); }
} else if ("ALERT".equals(EVENT_TYPE)) {
Helper.enableNode(valueText, tile.isValueVisible() && !tile.isAlert());
Helper.enableNode(unitText, tile.isValueVisible() && !tile.isAlert());
Helper.enableNode(alertIcon, tile.isAlert());
alertTooltip.setText(tile.getAlertMessage());
}
}
public TileKpiSkin(Gauge gauge) {
super(gauge);
if (gauge.isAutoScale()) gauge.calcAutoScale();
angleRange = Helper.clamp(90.0, 180.0, gauge.getAngleRange());
oldValue = gauge.getValue();
minValue = gauge.getMinValue();
threshold = gauge.getThreshold();
thresholdColor = gauge.getThresholdColor();
range = gauge.getRange();
angleStep = angleRange / range;
formatString = new StringBuilder("%.").append(Integer.toString(gauge.getDecimals())).append("f").toString();
locale = gauge.getLocale();
sectionsVisible = gauge.getSectionsVisible();
highlightSections = gauge.isHighlightSections();
sections = gauge.getSections();
sectionMap = new HashMap<>(sections.size());
currentValueListener = o -> rotateNeedle(gauge.getCurrentValue());
for(Section section : sections) { sectionMap.put(section, new Arc()); }
initGraphics();
registerListeners();
rotateNeedle(gauge.getCurrentValue());
}
private void drawSections() {
if (sections.isEmpty()) return;
sectionLayer.getChildren().clear();
double centerX = width * 0.5;
double centerY = height * 0.85;
double barRadius = height * 0.54210526;
double barWidth = width * 0.28472222;
List<Arc> sectionBars = new ArrayList<>(sections.size());
for (Section section : sections) {
Arc sectionBar = new Arc(centerX, centerY, barRadius, barRadius, angleRange * 0.5 + 90 - (section.getStart() * angleStep), -((section.getStop() - section.getStart()) - minValue) * angleStep);
sectionBar.setType(ArcType.OPEN);
sectionBar.setStroke(section.getColor());
sectionBar.setStrokeWidth(barWidth);
sectionBar.setStrokeLineCap(StrokeLineCap.BUTT);
sectionBar.setFill(null);
Tooltip sectionTooltip = new Tooltip(new StringBuilder(section.getText()).append("\n").append(String.format(Locale.US, "%.2f", section.getStart())).append(" - ").append(String.format(Locale.US, "%.2f", section.getStop())).toString());
sectionTooltip.setTextAlignment(TextAlignment.CENTER);
Tooltip.install(sectionBar, sectionTooltip);
sectionBars.add(sectionBar);
}
sectionLayer.getChildren().addAll(sectionBars);
}
/** Add four arcs to a pane and place them in a stack pane */
private void getBlades() {
double angle = 0;
for (int i = 0; i < 4; i++) {
arc = new Arc();
arc.centerXProperty().bind(widthProperty().divide(2));
arc.centerYProperty().bind(heightProperty().divide(2));
arc.radiusXProperty().bind(circle.radiusProperty().multiply(.90));
arc.radiusYProperty().bind(circle.radiusProperty().multiply(.90));
arc.setStartAngle(angle + 90);
arc.setLength(50);
arc.setFill(Color.BLACK);
arc.setType(ArcType.ROUND);
paneForBlades.getChildren().add(arc);
angle += 90;
}
}
/** Add four arcs to a pane and place them in a stack pane */
private void getBlades() {
double angle = 0;
for (int i = 0; i < 4; i++) {
arc = new Arc();
arc.centerXProperty().bind(widthProperty().divide(2));
arc.centerYProperty().bind(heightProperty().divide(2));
arc.radiusXProperty().bind(circle.radiusProperty().multiply(.90));
arc.radiusYProperty().bind(circle.radiusProperty().multiply(.90));
arc.setStartAngle(angle + 90);
arc.setLength(50);
arc.setFill(Color.BLACK);
arc.setType(ArcType.ROUND);
paneForBlades.getChildren().add(arc);
angle += 90;
}
}
@Override
public void initializeParts() {
double center = ARTBOARD_SIZE * 0.5;
int width = 15;
double radius = center - width;
backgroundCircle = new Circle(center, center, radius);
backgroundCircle.getStyleClass().add("backgroundCircle");
bar = new Arc(center, center, radius, radius, 90.0, 0.0);
bar.getStyleClass().add("bar");
bar.setType(ArcType.OPEN);
thumb = new Circle(center, center + center - width, 13);
thumb.getStyleClass().add("thumb");
valueDisplay = createCenteredText(center, center, "valueDisplay");
ticks = createTicks(center, center, 60, 360.0, 6, 33, 0, "tick");
tickLabels = new ArrayList<>();
int labelCount = 8;
for (int i = 0; i < labelCount; i++) {
double r = 95;
double nextAngle = i * 360.0 / labelCount;
Point2D p = pointOnCircle(center, center, r, nextAngle);
Text tickLabel = createCenteredText(p.getX(), p.getY(), "tickLabel");
tickLabels.add(tickLabel);
}
updateTickLabels();
drawingPane = new Pane();
drawingPane.getStyleClass().add("numberRange");
drawingPane.setMaxSize(ARTBOARD_SIZE, ARTBOARD_SIZE);
drawingPane.setMinSize(ARTBOARD_SIZE, ARTBOARD_SIZE);
drawingPane.setPrefSize(ARTBOARD_SIZE, ARTBOARD_SIZE);
}
private void initializeParts() {
double cx = ARTBOARD_WIDTH * 0.5;
separator = new Line(25, 15, 225, 15);
separator.getStyleClass().add("separator");
separator.setStrokeLineCap(StrokeLineCap.ROUND);
titleLabel = createCenteredText(cx, 19, "title");
titleLabel.setTextOrigin(VPos.TOP);
valueLabel = createCenteredText(cx, 150, "value");
unitLabel = createCenteredText(cx, 188, "unit");
unitLabel.setTextOrigin(VPos.TOP);
barBackground = new Circle(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, RADIUS);
barBackground.getStyleClass().add("barBackground");
bar = new Arc(CIRCLE_CENTER_X, CIRCLE_CENTER_Y, RADIUS, RADIUS, START_ANGLE, 0);
bar.getStyleClass().add("bar");
bar.setType(ArcType.OPEN);
thumb = new Circle(7);
thumb.getStyleClass().add("thumb");
// always needed
drawingPane = new Pane();
drawingPane.setMaxSize(ARTBOARD_WIDTH, ARTBOARD_HEIGHT);
drawingPane.setMinSize(ARTBOARD_WIDTH, ARTBOARD_HEIGHT);
drawingPane.setPrefSize(ARTBOARD_WIDTH, ARTBOARD_HEIGHT);
}
@ParameterizedTest
@EnumSource(ArcStyle.class)
void testClipArrowExists(final ArcStyle style) {
assumeTrue(style.supportArrow());
model.setArcStyle(style);
model.setArrowStyle(ArrowStyle.RIGHT_ARROW, 0);
WaitForAsyncUtils.waitForFxEvents();
assertTrue(view.border.getClip() instanceof Arc);
}
@Override
public void update() {
if (status != ObjectStatus.ANIMATION)
return;
for (int i = 0; i < shapes.size(); i++) {
Arc arc = (Arc) shapes.get(i);
arc.setStartAngle(arc.getStartAngle() + 8);
}
}
public static Node createIconContent() {
Arc arc = new Arc(57,57,45,45,40,100);
arc.setStroke(Color.web("#b9c0c5"));
arc.setStrokeWidth(5);
arc.getStrokeDashArray().addAll(15d,15d);
arc.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));
arc.setEffect(effect);
return arc;
}
public static Node createIconContent() {
Arc arc = new Arc(57,57,45,45,40,100);
arc.setStroke(Color.web("#b9c0c5"));
arc.setStrokeWidth(5);
arc.getStrokeDashArray().addAll(15d,15d);
arc.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));
arc.setEffect(effect);
return arc;
}
@Override
public Arc createJFXNode() throws Exception
{
final Arc arc = new Arc();
updateColors();
updateAngles();
return arc;
}
@ParameterizedTest
@EnumSource(ArcStyle.class)
void testClipArrowSameType(final ArcStyle style) {
assumeTrue(style.supportArrow());
model.setArcStyle(style);
model.setArrowStyle(ArrowStyle.RIGHT_ARROW, 0);
WaitForAsyncUtils.waitForFxEvents();
assertEquals(((Arc) view.border.getClip()).getType(), style.getJFXStyle());
}
@ParameterizedTest
@MethodSource("getArcsToTest")
void testSetAngleStartMainBorderLength(final Function<ViewCircleArc, Arc> fct) {
final Arc arc = fct.apply(view);
final double length = arc.getLength();
final double angle = model.getAngleStart() + Math.PI / 4d;
model.setAngleStart(angle);
WaitForAsyncUtils.waitForFxEvents();
assertNotEquals(arc.getLength(), length);
assertEquals(Math.toDegrees(model.getAngleEnd() - model.getAngleStart()), arc.getLength(), 0.0001);
}
@Override protected void handleEvents(final String EVENT_TYPE) {
super.handleEvents(EVENT_TYPE);
if ("RECALC".equals(EVENT_TYPE)) {
angleRange = Helper.clamp(90.0, 180.0, gauge.getAngleRange());
minValue = gauge.getMinValue();
threshold = gauge.getThreshold();
range = gauge.getRange();
angleStep = angleRange / range;
highlightSections = gauge.isHighlightSections();
redraw();
rotateNeedle(gauge.getCurrentValue());
} else if ("VISIBILITY".equals(EVENT_TYPE)) {
enableNode(titleText, !gauge.getTitle().isEmpty());
enableNode(valueText, gauge.isValueVisible());
enableNode(sectionPane, gauge.getSectionsVisible());
enableNode(thresholdRect, gauge.isThresholdVisible());
enableNode(thresholdText, gauge.isThresholdVisible());
enableNode(unitText, !gauge.getUnit().isEmpty());
sectionsVisible = gauge.getSectionsVisible();
} else if ("SECTION".equals(EVENT_TYPE)) {
sections = gauge.getSections();
sectionMap.clear();
for(Section section : sections) { sectionMap.put(section, new Arc()); }
} else if ("ALERT".equals(EVENT_TYPE)) {
enableNode(valueText, gauge.isValueVisible() && !gauge.isAlert());
enableNode(unitText, gauge.isValueVisible() && !gauge.isAlert());
enableNode(alertIcon, gauge.isAlert());
alertTooltip.setText(gauge.getAlertMessage());
}
}
@ParameterizedTest
@MethodSource("getArcsToTest")
void testSetAngleStartMainBorder(final Function<ViewCircleArc, Arc> fct) {
final Arc arc = fct.apply(view);
final double startAngle = arc.getStartAngle();
final double angle = model.getAngleStart() + Math.PI / 4d;
model.setAngleStart(angle);
WaitForAsyncUtils.waitForFxEvents();
assertNotEquals(arc.getStartAngle(), startAngle);
assertEquals(Math.toDegrees(angle), arc.getStartAngle(), 0.00001);
}
@Override
public Scene createScene() {
HBox hbox = new HBox();
VBox col1 = new VBox();
VBox col2 = new VBox();
HBox.setMargin(col1, new Insets(10.0));
HBox.setMargin(col2, new Insets(10.0));
hbox.getChildren().addAll(col1, col2);
HBox.setHgrow(col1, Priority.ALWAYS);
HBox.setHgrow(col2, Priority.ALWAYS);
col1.getChildren().addAll(new Button("JavaFX Button 1"),
shape(new Arc(0, 0, 50, 50, 15, 120) {
{
setType(ArcType.ROUND);
}
}, 0.52, 0.49, 0.15), createButtonAdapter("SWT Button 1"));
col2.getChildren().addAll(
shape(new Rectangle(0, 0, 100, 50), 0.49, 0.36, 0.20),
createButtonAdapter("SWT Button 2"),
shape(new Rectangle(0, 0, 100, 100) {
{
setArcHeight(20);
setArcWidth(20);
}
}, 0.87, 0.83, 0.49), new Button("JavaFX Button 2"));
return new Scene(hbox, 400, 300);
}
private static void setSide(Shape shape, String side) {
if (shape instanceof Polygon) {
setSide((Polygon) shape, side);
} else if (shape instanceof Arc) {
setSide((Arc) shape, side);
}
}
/**
* Converts the given JavaFX {@link Arc} to a
* {@link org.eclipse.gef.geometry.planar.Arc}.
*
* @param arc
* The JavaFX {@link Arc} to convert.
* @return The newly created {@link org.eclipse.gef.geometry.planar.Arc}
* that describes the given {@link Arc}.
*/
public static org.eclipse.gef.geometry.planar.Arc toArc(Arc arc) {
return new org.eclipse.gef.geometry.planar.Arc(
arc.getCenterX() - arc.getRadiusX(),
arc.getCenterY() - arc.getRadiusY(),
arc.getRadiusX() + arc.getRadiusX(),
arc.getRadiusY() + arc.getRadiusY(),
Angle.fromDeg(arc.getStartAngle()),
Angle.fromDeg(arc.getLength()));
}
/**
* Returns an {@link IGeometry} that describes the geometric outline of the
* given {@link Shape}, i.e. excluding the stroke.
* <p>
* The conversion is supported for the following {@link Shape}s:
* <ul>
* <li>{@link Arc}
* <li>{@link Circle}
* <li>{@link CubicCurve}
* <li>{@link Ellipse}
* <li>{@link Line}
* <li>{@link Path}
* <li>{@link Polygon}
* <li>{@link Polyline}
* <li>{@link QuadCurve}
* <li>{@link Rectangle}
* </ul>
* The following {@link Shape}s cannot be converted, yet:
* <ul>
* <li>{@link Text}
* <li>{@link SVGPath}
* </ul>
*
* @param visual
* The {@link Shape} for which an {@link IGeometry} is
* determined.
* @return The newly created {@link IGeometry} that best describes the
* geometric outline of the given {@link Shape}.
* @throws IllegalStateException
* if the given {@link Shape} is not supported.
*/
public static IGeometry toGeometry(Shape visual) {
if (visual instanceof Arc) {
return toArc((Arc) visual);
} else if (visual instanceof Circle) {
return toEllipse((Circle) visual);
} else if (visual instanceof CubicCurve) {
return toCubicCurve((CubicCurve) visual);
} else if (visual instanceof Ellipse) {
return toEllipse((Ellipse) visual);
} else if (visual instanceof Line) {
return toLine((Line) visual);
} else if (visual instanceof Path) {
return toPath((Path) visual);
} else if (visual instanceof Polygon) {
return toPolygon((Polygon) visual);
} else if (visual instanceof Polyline) {
return toPolyline((Polyline) visual);
} else if (visual instanceof QuadCurve) {
QuadCurve quad = (QuadCurve) visual;
return toQuadraticCurve(quad);
} else if (visual instanceof Rectangle) {
Rectangle rect = (Rectangle) visual;
if (rect.getArcWidth() == 0 && rect.getArcHeight() == 0) {
// corners are not rounded => normal rectangle is sufficient
return toRectangle(rect);
}
return toRoundedRectangle((Rectangle) visual);
} else {
// Text and SVGPath shapes are currently not supported
throw new IllegalStateException(
"Cannot compute geometric outline for Shape of type <"
+ visual.getClass() + ">.");
}
}
@ParameterizedTest
@EnumSource(ArcStyle.class)
void testClipArrowSameStartEndNotEmptyClip(final ArcStyle style) {
assumeTrue(style.supportArrow());
model.setArcStyle(style);
model.setAngleStart(0d);
model.setAngleEnd(0d);
model.setArrowLength(0d);
model.setArrowStyle(ArrowStyle.RIGHT_ARROW, 0);
model.setArrowStyle(ArrowStyle.RIGHT_DBLE_ARROW, -1);
WaitForAsyncUtils.waitForFxEvents();
final Arc clip = (Arc) view.border.getClip();
assertNotEquals(clip.getLength(), 0d);
}
@Test
void testTicknessChangeDbleSep() {
final Arc arc = cloneArc(view.border);
model.setHasDbleBord(true);
model.setDbleBordSep(model.getDbleBordSep() * 2d);
WaitForAsyncUtils.waitForFxEvents();
assertNotEquals(arc.getRadiusX(), view.border.getRadiusX());
assertNotEquals(arc.getRadiusY(), view.border.getRadiusY());
}
@Override
public boolean contains(Edge pEdge, Point pPoint)
{
boolean result = super.contains(pEdge, pPoint);
if (getShape(pEdge) instanceof Arc)
{
Arc arc = (Arc) getShape(pEdge);
arc.setRadiusX(arc.getRadiusX() + 2 * MAX_DISTANCE);
arc.setRadiusY(arc.getRadiusY() + 2 * MAX_DISTANCE);
result = arc.contains(pPoint.getX(), pPoint.getY());
}
return result;
}
/** Animate fan blades */
protected void spinFan() {
ObservableList<Node> list = paneForBlades.getChildren();
for (int i = 0; i < list.size(); i++) {
Arc a = (Arc)list.get(i);
a.setStartAngle(a.getStartAngle() + startAngle);
}
}
/** Animate fan blades */
protected void spinFan() {
ObservableList<Node> list = paneForBlades.getChildren();
for (Node n: list) {
((Arc)n).setStartAngle(((Arc)n).getStartAngle() + startAngle);
}
}