JavaFX Line

本文概述

  • 如何创建线?
  • 物产
通常, “直线”可以定义为在X-Y坐标平面中连接两个点(X1, Y1)和(X2, Y2)的几何结构。 JavaFX允许开发人员在JavaFX应用程序的GUI上创建代码行。 JavaFX库提供Line类, 它是javafx.scene.shape包的一部分。
如何创建线? 请按照以下说明创建线。
  • 实例化类javafx.scene.shape.Line。
  • 设置类对象的必需属性。
  • 将类对象添加到组中
物产 线类包含以下描述的各种属性。
属性 描述 设置方法
endX 线终点的X坐标 setEndX(Double)
endY 线终点的y坐标 setEndY(Double)
startX 直线起点的x坐标 setStartX(Double)
startY 直线起点的y坐标 setStartY(Double)
范例1:
package application; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.Group; import javafx.scene.shape.Line; import javafx.stage.Stage; public class LineDrawingExamples extends Application{ @Override public void start(Stage primaryStage) throws Exception {// TODO Auto-generated method stubLine line = new Line(); //instantiating Line class line.setStartX(0); //setting starting X point of Lineline.setStartY(0); //setting starting Y point of Line line.setEndX(100); //setting ending X point of Line line.setEndY(200); //setting ending Y point of Line Group root = new Group(); //Creating a Grouproot.getChildren().add(line); //adding the class object //to the groupScene scene = new Scene(root, 300, 300); primaryStage.setScene(scene); primaryStage.setTitle("Line Example"); primaryStage.show(); } public static void main(String[] args) {launch(args); }}

输出:
JavaFX Line

文章图片
示例2:创建多行
package application; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.stage.Stage; public class LineDrawingExamples extends Application{ public static void main(String[] args) {launch(args); } @Override public void start(Stage primaryStage) throws Exception {// TODO Auto-generated method stubprimaryStage.setTitle("Line Drawing Examples"); Line line1 = new Line(10, 50, 150, 50); //Line(startX, startY, endX, endY) Line line2 = new Line(10, 100, 150, 100); Line line3 = new Line(10, 50, 10, 100); Line line4 = new Line(150, 50, 150, 100); Group root = new Group(); root.getChildren().addAll(line1, line2, line3, line4); Scene scene = new Scene (root, 300, 200, Color.GREEN); primaryStage.setScene(scene); primaryStage.show(); }}

【JavaFX Line】输出:
JavaFX Line

文章图片

    推荐阅读