JavaFX | LineTo class

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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("LineTo");// create LineToLineTo Lineto =newLineTo(200,200);// create movetoMoveTo moveto =newMoveTo(100,100);// create a PathPath path =newPath(moveto, Lineto);// set fill for pathpath.setFill(Color.BLACK);// set stroke widthpath.setStrokeWidth(2);// create a GroupGroup group =newGroup(path);// create a sceneScene scene =newScene(group,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("LineTo");// create LineToLineTo Lineto =newLineTo(300,200);LineTo Lineto1 =newLineTo(200,300);LineTo Lineto2 =newLineTo(100,200);LineTo Lineto3 =newLineTo(200,100);// create movetoMoveTo moveto =newMoveTo(200,100);// create a PathPath path =newPath(moveto, Lineto,Lineto1, Lineto2, Lineto3);// set fill for pathpath.setFill(Color.GREEN);// set stroke widthpath.setStrokeWidth(2);// create a GroupGroup group =newGroup(path);// create a sceneScene scene =newScene(group,400,400);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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



