JavaFX | Line with examples

14 Apr, 2021
Line is a part of JavaFX. The Line class represents a line in a 2D space.Constructor for the class:  
Line(): Creates a new instance for lineLine(double startX, double startY, double endX, double endY): Creates a new Line with specified starting and ending point
Line(): Creates a new instance for line
Line(double startX, double startY, double endX, double endY): Creates a new Line with specified starting and ending point
Commonly Used Methods: 
 
Below programs illustrate the Line class: 
Java program to create a line with starting and ending coordinates passed as arguments: This program creates a Line indicated by the name line( start point and the end point is passed as arguments). The Line will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the line is attached. The group is attached to the scene. Finally, the show() method is called to display the final results. 
Java program to create a line with starting and ending coordinates passed as arguments: This program creates a Line indicated by the name line( start point and the end point is passed as arguments). The Line will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the line is attached. The group is attached to the scene. Finally, the show() method is called to display the final results. 
Java
// Java program to create a line with starting// and ending coordinates passed as argumentsimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.shape.DrawMode;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Line;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.Group; public class line_0 extends Application {     // launch the application    public void start(Stage stage)    {                 // set title for the stage        stage.setTitle("creating line");         // create a line        Line line = new Line(10.0f, 10.0f, 200.0f, 140.0f);         // create a Group        Group group = new Group(line);         // translate the line to a position        line.setTranslateX(100);        line.setTranslateY(100);         // create a scene        Scene scene = new Scene(group, 500, 300);         // set the scene        stage.setScene(scene);         stage.show();    }     // Main Method    public static void main(String args[])    {        // launch the application        launch(args);    }}
Output:  
Output:  
 Java program to create a line with starting and ending coordinates set using function setStartX(), setStartY()setEndX(), setEndY() function: This program creates a Line indicated by the name line(start point and end point is set using setEndX(), setEndY(), setStartX(), setStartY() function). The Line will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the line is attached.The group is attached to the scene. Finally, the show() method is called to display the final results. 
 
Java program to create a line with starting and ending coordinates set using function setStartX(), setStartY()setEndX(), setEndY() function: This program creates a Line indicated by the name line(start point and end point is set using setEndX(), setEndY(), setStartX(), setStartY() function). The Line will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the line is attached.The group is attached to the scene. Finally, the show() method is called to display the final results. 
Java
// Java program to create a line with starting// and ending coordinates set using function// setStartX(), setStartY() setEndX(),// setEndY() functionimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.shape.DrawMode;import javafx.scene.layout.*;import javafx.event.ActionEvent;import javafx.scene.shape.Line;import javafx.scene.control.*;import javafx.stage.Stage;import javafx.scene.Group; public class line_1 extends Application {     // launch the application    public void start(Stage stage)    {        // set title for the stage        stage.setTitle("creating line");         // create a line        Line line = new Line();         // set starting position        line.setStartX(10.0f);        line.setStartY(10.0f);         // set ending position        line.setEndX(140.0f);        line.setEndY(140.0f);         // create a Group        Group group = new Group(line);         // translate the line to a position        line.setTranslateX(100);        line.setTranslateY(100);         // create a scene        Scene scene = new Scene(group, 500, 300);         // set the scene        stage.setScene(scene);         stage.show();    }     // Main Method    public static void main(String args[])    {                 // launch the application        launch(args);    }}
Output: 
Output: 
Note: The above programs might not run in an online IDE. Please use an offline compiler.Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Line.html 
sweetyty
JavaFX
Java Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Factory method design pattern in Java
Java Program to Remove Duplicate Elements From the Array
Java program to count the occurrence of each character in a string using Hashmap
Iterate through List in Java
How to Iterate HashMap in Java?
Iterate Over the Characters of a String in Java
Traverse Through a HashMap in Java
Program to print ASCII Value of a character
Remove first and last character of a string in Java
How to Convert Char to String in Java?