📜  JavaFX Line

📅  最后修改于: 2020-10-14 01:07:05             🧑  作者: Mango

JavaFX Line

通常,“线”可以定义为在XY坐标平面上连接两个点(X1,Y1)和(X2,Y2)的几何结构。 JavaFX允许开发人员在JavaFX应用程序的GUI上创建代码行。 JavaFX库提供了Line类,它是javafx.scene.shape包的一部分。

如何创建线?

请按照以下说明创建线。

  • 实例化类javafx.scene.shape.Line
  • 设置类对象的必需属性。
  • 将类对象添加到组中

物产

线类包含以下描述的各种属性。

Property Description Setter Methods
endX The X coordinate of the end point of the line setEndX(Double)
endY The y coordinate of the end point of the line setEndY(Double)
startX The x coordinate of the starting point of the line setStartX(Double)
startY The y coordinate of the starting point of the line 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 stub
Line line = new Line(); //instantiating Line class 
line.setStartX(0); //setting starting X point of Line
line.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 Group
root.getChildren().add(line); //adding the class object //to the group
Scene scene = new Scene(root,300,300);
primaryStage.setScene(scene);
primaryStage.setTitle("Line Example");
primaryStage.show();

}
public static void main(String[] args) {
launch(args);
}

}

输出:

示例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 stub
primaryStage.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();
}

}

输出: