如何使用 JavaFX 创建弧?

javaobject oriented programmingprogramming更新于 2025/10/9 21:32:17

一般来说,弧是曲线的一小段。在 JavaFX 中,它由 javafx.scene.shape.Arc 类表示。此类包含六个属性,它们是 −

  • centerX − 此属性表示弧中心的 x 坐标。您可以使用 setCenterX() 方法将值设置为此属性。

  • centerY − 此属性表示弧中心的 y 坐标。您可以使用 setCenterY() 方法将值设置为此属性。

  • radiusX − 此属性表示当前弧所属的完整椭圆的宽度。您可以使用 setRadiusX() 方法设置此属性的值。

  • radiusY − 此属性表示当前弧所属的完整椭圆的高度。您可以使用 setRadiusY() 方法设置此属性的值。

  • startAngle − 此属性表示弧的起始角度(以度为单位)。您可以使用 setStartAngle() 方法设置此属性的值。

  • length − 此属性表示弧的角度范围(以度为单位)。您可以使用 setLength() 方法将值设置为此属性。

要创建一个圆,您需要 −

  • 实例化此类。

  • 使用 setter 方法设置所需属性,或将它们作为参数绕过构造函数。

  • 将创建的节点(形状)添加到 Group 对象。

示例

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
public class DrawingArc extends Application {
   public void start(Stage stage) {
      //绘制三次曲线
      Arc arc = new Arc();
      //设置属性为三次曲线
      arc.setCenterX(280);
      arc.setCenterY(230);
      arc.setRadiusX(100);
      arc.setRadiusY(180);
      arc.setStartAngle(45);
      arc.setLength(100);
      arc.setType(ArcType.ROUND);
      //设置其他属性
      arc.setFill(Color.CHOCOLATE);
      arc.setStrokeWidth(8.0);
      arc.setStroke(Color.BROWN);
      //设置场景对象
      Group root = new Group(arc);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("Drawing arc");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

输出


相关文章


有用资源