这里有几个示例解决方案,一个使用shape subtraction 表示圆圈,另一个使用Arc。两个示例都使用场景图进行绘制。
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
public class DonutHole extends Application {
@Override
public void start(Stage stage) throws Exception {
// donut by shape subtraction.
Circle whole = new Circle(20, 20, 20);
Circle inside = new Circle(20, 20, 10);
Shape donutShape = Shape.subtract(whole, inside);
donutShape.setFill(Color.BLUE);
// donut by arc.
Arc donutArc = new Arc(60, 20, 10, 10, 0, 360);
donutArc.setStrokeWidth(10);
donutArc.setStrokeType(StrokeType.OUTSIDE);
donutArc.setStroke(Color.RED);
donutArc.setStrokeLineCap(StrokeLineCap.BUTT);
donutArc.setFill(null);
Scene scene = new Scene(new Group(donutShape, donutArc), Color.PALEGREEN);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
另一种解决方案也可以使用带有圆弧和线段的Path,但我没有在这里展示。如果你想要一个 3D 甜甜圈,你可以创建一个Torus。
这是另一个在 GraphicsContext 中使用 fillArc 的示例。
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
public class DonutHoleGraphics extends Application {
@Override
public void start(Stage stage) throws Exception {
Canvas canvas = new Canvas(40, 40);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setLineWidth(10);
gc.setStroke(Color.YELLOW);
gc.setLineCap(StrokeLineCap.BUTT);
gc.strokeArc(5, 5, 30, 30, 0, 360, ArcType.OPEN);
Scene scene = new Scene(new Group(canvas), Color.PALEGREEN);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
相关: