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 line
- Line(double startX, double startY, double endX, double endY): Creates a new Line with specified staring and ending point
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getEndX() | returns the x coordinate for the end point |
| getEndY() | returns the y coordinate for the end point |
| getStartX() | returns the x coordinate for the start point |
| getStartY() | returns the y coordinate for the start point |
| setEndX(double value) | sets the x coordinate for the end point |
| setEndY(double value) | sets the y coordinate for the end point |
| setStartX(double value) | sets the x coordinate for the start point |
| setStartY(double value) | sets the y coordinate for the start point |
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 argumentsimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.DrawMode;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.shape.Line;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;publicclassline_0extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating line");// create a lineLine line =newLine(10.0f,10.0f,200.0f,140.0f);// create a GroupGroup group =newGroup(line);// translate the line to a positionline.setTranslateX(100);line.setTranslateY(100);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:
- 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() functionimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.shape.DrawMode;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.scene.shape.Line;importjavafx.scene.control.*;importjavafx.stage.Stage;importjavafx.scene.Group;publicclassline_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){// set title for the stagestage.setTitle("creating line");// create a lineLine line =newLine();// set staring positionline.setStartX(10.0f);line.setStartY(10.0f);// set ending positionline.setEndX(140.0f);line.setEndY(140.0f);// create a GroupGroup group =newGroup(line);// translate the line to a positionline.setTranslateX(100);line.setTranslateY(100);// create a sceneScene scene =newScene(group,500,300);// set the scenestage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

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
Recommended Posts:
- JavaFX | Button with examples
- JavaFX | Rectangle and Rounded Rectangle with examples
- JavaFX | ComboBox with examples
- JavaFX | RadioButton with examples
- JavaFx | ColorPicker with examples
- JavaFX | Alert with examples
- JavaFX | DatePicker with examples
- JavaFX | ContextMenu with examples
- JavaFX | CheckMenuItem with examples
- JavaFX | Polygon with examples
- JavaFX | Circle with examples
- JavaFX | Polyline with examples
- JavaFX | Ellipse with examples
- JavaFX | Box with examples
- JavaFX | Cylinder with examples
- JavaFX | Sphere with examples
- JavaFX | QuadCurve with examples
- JavaFX | Arc with examples
- JavaFX | CubicCurve with examples
- JavaFX | PointLight with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

