LineTo class is a part of JavaFX. LineTo class draws a line from the current position to specified x and y coordinate. LineTo class inherits PathElement class.
Constructor of the class:
- LineTo(): Creates a new LineTo object.
- LineTo(double x, double y): Creates a new LineTo object with specified x, y coordinates.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getX() | Returns the value of X coordinate. |
| getY() | Returns the value of Y coordinate. |
| setX(double v) | Sets the value of X coordinate. |
| setY(double v) | Sets the value of Y coordinate. |
| xProperty() | Defines the X coordinate. |
| yProperty() | Defines the Y coordinate. |
Below programs illustrate the use of LineTo Class:
- Java program to create a path and add LineTo object to it and display it:
- In this program, we will create a Path object named path.
- Create an HLineTo object with specified X and Y coordinate.
- Then create a MoveTo object named moveto.
- Now add the MoveTo and Lineto object to the path.
- Add this path to Group object and add the group object to the scene and add the scene to the stage and call the show() function to display the final results.
// Java program to create a path and// add LineTo object to it and display itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;importjavafx.scene.paint.*;importjavafx.scene.*;ÂÂpublicclassLineTo_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("LineTo");           Â// create LineTo           ÂLineTo Lineto =newLineTo(200,200);           Â// create moveto           ÂMoveTo moveto =newMoveTo(100,100);           Â// create a Path           ÂPath path =newPath(moveto, Lineto);           Â// set fill for path           Âpath.setFill(Color.BLACK);           Â// set stroke width           Âpath.setStrokeWidth(2);           Â// create a Group           ÂGroup group =newGroup(path);           Â// create a scene           ÂScene scene =newScene(group,400,300);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Java program to create a path and add multiple LineTo objects to it and display it:
- In this program, we will create a Path object named path.
- Create four LineTo object with specified X and Y coordinates names Lineto, Lineto1, Lineto2, Lineto3.
- Then create a MoveTo object named moveto.
- Now add the MoveTo and Lineto objects to the path.
- Add this path to Group object and add the group object to the scene and add the scene to the stage and call the show() function to display the final results.
// Java program to create a path and// add multiple LineTo objects to it// and display itimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.layout.*;importjavafx.scene.paint.*;importjavafx.scene.text.*;importjavafx.geometry.*;importjavafx.scene.layout.*;importjavafx.scene.shape.*;importjavafx.scene.paint.*;importjavafx.scene.*;ÂÂpublicclassLineTo_2extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Âtry{           Â// set title for the stage           Âstage.setTitle("LineTo");           Â// create LineTo           ÂLineTo Lineto =newLineTo(300,200);           ÂLineTo Lineto1 =newLineTo(200,300);           ÂLineTo Lineto2 =newLineTo(100,200);           ÂLineTo Lineto3 =newLineTo(200,100);           Â// create moveto           ÂMoveTo moveto =newMoveTo(200,100);           Â// create a Path           ÂPath path =newPath(moveto, Lineto,                    ÂLineto1, Lineto2, Lineto3);           Â// set fill for path           Âpath.setFill(Color.GREEN);           Â// set stroke width           Âpath.setStrokeWidth(2);           Â// create a Group           ÂGroup group =newGroup(path);           Â// create a scene           ÂScene scene =newScene(group,400,400);           Â// set the scene           Âstage.setScene(scene);           Âstage.show();       Â}       Âcatch(Exception e) {           ÂSystem.out.println(e.getMessage());       Â}   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}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/LineTo.html

