📅  最后修改于: 2020-10-14 01:07:05             🧑  作者: Mango
通常,“线”可以定义为在XY坐标平面上连接两个点(X1,Y1)和(X2,Y2)的几何结构。 JavaFX允许开发人员在JavaFX应用程序的GUI上创建代码行。 JavaFX库提供了Line类,它是javafx.scene.shape包的一部分。
请按照以下说明创建线。
线类包含以下描述的各种属性。
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) |
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);
}
}
输出:
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();
}
}
输出: