如何使用 JavaFX 创建一个圆形?
圆是一条形成闭环的线,其上每一点到一个中心点的距离都是确定的。圆由其圆心和半径来定义,半径指的是从圆心到圆上任意一点的距离。
在 JavaFX 中,圆由 javafx.scene.shape.Circle 类表示。此类包含三个属性,即:
centerX – 此属性表示圆形的中心点的 x 坐标,您可以使用 setCenterX() 方法为该属性设置值。
centerY – 此属性表示圆形的中心点的 y 坐标,您可以使用 setCenterY() 方法为该属性设置值。
radius – 圆形的半径,以像素为单位,您可以使用 setRadius() 方法为该属性设置值。
要创建圆形,您需要:
实例化 Circle 类。
使用设置方法设置必需的属性,或将其作为构造函数的参数传递。
将创建的节点(形状)添加到 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.Circle;
public class DrawingCircle extends Application {
public void start(Stage stage) {
//Drawing a Circle
Circle circle = new Circle();
//Setting the properties of the circle
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
circle.setRadius(100.0f);
//Setting other properties
circle.setFill(Color.DARKCYAN);
circle.setStrokeWidth(8.0);
circle.setStroke(Color.DARKSLATEGREY);
//Setting the Scene
Group root = new Group(circle);
Scene scene = new Scene(root, 595, 300, Color.BEIGE);
stage.setTitle("Drawing a Circle");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP