您好,欢迎访问一九零五行业门户网

如何在JavaFX中绘制几何2D形状?

一般来说,2d形状是可以在xy平面上绘制的几何图形,包括线条、矩形、圆等。
javafx.scene.shape包提供了各种类,每个类代表/定义了一个2d几何对象或对它们的操作。名为shape的类是javafx中所有2d形状的基类。
创建2d形状要使用javafx绘制2d几何形状,您需要:
实例化类 - 实例化相应的类。例如,如果要绘制一个圆,您需要实例化circle类,如下所示:
//drawing a circlecircle circle = new circle();
设置属性 - 使用其相应类的方法设置形状的属性。例如,要绘制一个圆,您需要中心和半径,您可以分别使用setcenterx()、setcentery()和setradius()方法来设置它们。
//setting the properties of the circlecircle.setcenterx(300.0f);circle.setcentery(135.0f);circle.setradius(100.0f);
将形状对象添加到组中 − 最后,将创建的形状作为参数传递给组的构造函数,如下所示:
group root = new group(circle);
exampleimport javafx.application.application;import javafx.scene.group;import javafx.scene.scene;import javafx.stage.stage;import javafx.scene.shape.circle;public class circleexample 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); //creating a group object group root = new group(circle); //creating a scene object scene scene = new scene(root, 600, 300); //setting title to the stage stage.settitle("drawing a circle"); //adding scene to the stage stage.setscene(scene); //displaying the contents of the stage stage.show(); } public static void main(string args[]){ launch(args); }}
输出
以上就是如何在javafx中绘制几何2d形状?的详细内容。
其它类似信息

推荐信息