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 it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.layout.*;
import
javafx.scene.paint.*;
import
javafx.scene.text.*;
import
javafx.geometry.*;
import
javafx.scene.layout.*;
import
javafx.scene.shape.*;
import
javafx.scene.paint.*;
import
javafx.scene.*;
Â
Âpublic
class
LineTo_1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set title for the stage
           Â
stage.setTitle(
"LineTo"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create LineTo
           Â
LineTo Lineto =
new
LineTo(
200
,
200
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create moveto
           Â
MoveTo moveto =
new
MoveTo(
100
,
100
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Path
           Â
Path path =
new
Path(moveto, Lineto);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set fill for path
           Â
path.setFill(Color.BLACK);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set stroke width
           Â
path.setStrokeWidth(
2
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Group
           Â
Group group =
new
Group(path);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a scene
           Â
Scene scene =
new
Scene(group,
400
,
300
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set the scene
           Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
stage.show();
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
catch
(Exception e) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
System.out.println(e.getMessage());
       Â
}
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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 it
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.scene.layout.*;
import
javafx.scene.paint.*;
import
javafx.scene.text.*;
import
javafx.geometry.*;
import
javafx.scene.layout.*;
import
javafx.scene.shape.*;
import
javafx.scene.paint.*;
import
javafx.scene.*;
Â
Âpublic
class
LineTo_2
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set title for the stage
           Â
stage.setTitle(
"LineTo"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create LineTo
           Â
LineTo Lineto =
new
LineTo(
300
,
200
);
           Â
LineTo Lineto1 =
new
LineTo(
200
,
300
);
           Â
LineTo Lineto2 =
new
LineTo(
100
,
200
);
           Â
LineTo Lineto3 =
new
LineTo(
200
,
100
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create moveto
           Â
MoveTo moveto =
new
MoveTo(
200
,
100
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Path
           Â
Path path =
new
Path(moveto, Lineto,Â
                    Â
Lineto1, Lineto2, Lineto3);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set fill for path
           Â
path.setFill(Color.GREEN);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set stroke width
           Â
path.setStrokeWidth(
2
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Group
           Â
Group group =
new
Group(path);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a scene
           Â
Scene scene =
new
Scene(group,
400
,
400
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set the scene
           Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
stage.show();
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
catch
(Exception e) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
System.out.println(e.getMessage());
       Â
}
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(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