Updated March 20, 2023
Introduction on JavaFX Line
In JavaFX, line class denotes a line in a two-dimensional space. That is, it is a geometrical shape that connects 2 points in a coordinate plane X-Y. In order to create a line, package javafx.scene.shape is used. Syntax, properties, and programs to implement JavaFX Line will be discussed in the below sections.
Syntax
It can be created using two ways.
First Method: Create a line bypassing x and y coordinate values
Line l = new Line(x1, y1, x2, y2);
Where,
- x1 is the start point of x coordinate,
- y1 is the start point of y coordinate,
- x2 is the endpoint of x coordinate,
- y2 is the endpoint of y coordinate,
Second Method: Create a line and set x, y coordinate values using set methods
Line l = new Line();
setStartX( val );
setStartY( val );
setEndX( val );
setEndY( val );
Constructors of JavaFX Line
For JavaFX Line, two constructors are available. They are:
- Line(): A line instance will be created.
- Line(double x1, double y1, double x2, double y2): A line instance will be created on the coordinates x1, y1, x2, and y2.
Properties of JavaFX Line
It has 4 properties. They are :
- startX: start point of the x coordinate of the line
- startY: start point of the y coordinate of the line
- endX: end point of the x coordinate of the line
- endY: end point of the y coordinate of the line
Methods
The following are the commonly used methods.
- setStartY(double val): startY property’s value will be set.
- setStartX(double val): startX property’s value will be set.
- getStartY(): startY property’s value will be returned.
- getStartX(): startX property’s value will be returned.
- setEndY(double val): endY property’s value will be set.
- setEndX(double val): endX property’s value will be set.
- getEndY(): endY property’s value will be returned.
- getEndX(): endX property’s value will be returned.
- toString(): A string representation of the given Line object will be returned.
Examples to implement JavaFX Line
Now, let us see some of the programming examples.
Example #1
Program to display a simple Line by using setter methods
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
//class that extends application class
public class JavaFXLineExample extends Application{
//application starts at this point
@Override
public void start(Stage s) {
//Create a line
Line l = new Line();
//Set the x and y coordinates
l.setStartX(110.0);
l.setStartY(160.0);
l.setEndX(520.0);
l.setEndY(170.0);
//Create a Group
Group gp = new Group(l);
//Create a Scene
Scene sc = new Scene(gp, 600, 300);
//Set title
s.setTitle("JavaFX Line Example");
//Add the scene sc to the stage
s.setScene(sc);
//Display the results
s.show();
}
//main method
public static void main(String args[]){
launch(args);
}
}
Output:
A line will be created with values x1, y1, x2 and y2 as 110.0,160.0,520.0,170.0. Here, these values are set using setter methods such as setStartX(110.0), setStartY(160.0), setEndX(520.0), setEndY(170.0).
Example #2
Program to display a line bypassing x, y values in the constructor
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
//class that extends application class
public class JavaFXLineExample extends Application{
//application starts at this point
@Override
public void start(Stage s) {
//Create a line by passing x, y values in constructor
Line l = new Line(110.0,160.0,520.0,170.0);
//Create a Group
Group gp = new Group(l);
//Create a Scene
Scene sc = new Scene(gp, 600, 300);
//Set title
s.setTitle("JavaFX Line Example");
//Add the scene sc to the stage
s.setScene(sc);
//Display the results
s.show();
}
//main method
public static void main(String args[]){
launch(args);
}
}
Sample Output:
A line will be created with values x1, y1, x2 and y2 as 110.0,160.0,520.0,170.0. Unlike the above program, these values are passed as parameters in the constructor instead of setter methods.
Example #3
Program to create a rectangle with the help of four lines.
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
//class that extends application class
public class JavaFXLineExample extends Application{
//application starts at this point
@Override
public void start(Stage s) {
//Create line 1
Line l1 = new Line();
//Set the x and y coordinates of l1
l1.setStartX(20.0);
l1.setStartY(60.0);
l1.setEndX(160.0);
l1.setEndY(60.0);
//Create line 2
Line l2 = new Line();
//Set the x and y coordinates of l2
l2.setStartX(20.0);
l2.setStartY(110.0);
l2.setEndX(160.0);
l2.setEndY(110.0);
//Create line 3
Line l3 = new Line();
//Set the x and y coordinates of l3
l3.setStartX(20.0);
l3.setStartY(60.0);
l3.setEndX(20.0);
l3.setEndY(110.0);
//Create line 4
Line l4 = new Line();
//Set the x and y coordinates of l4
l4.setStartX(160.0);
l4.setStartY(60.0);
l4.setEndX(160.0);
l4.setEndY(110.0);
//Create a Group
Group gp = new Group();
gp.getChildren().addAll(l1,l2,l3,l4);
//Create a Scene
Scene sc = new Scene(gp, 600, 300);
//Set title
s.setTitle("JavaFX Line Example");
//Add the scene sc to the stage
s.setScene(sc);
//Display the results
s.show();
}
//main method
public static void main(String args[]){
launch(args);
}
}
Sample Output:
On executing the code, a rectangle will be displayed as we have created four lines that connect with the help of coordinates.
Example #4
Program to create a stroked line.
Code:
import java.awt.Color;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;
//class that extends application class
public class JavaFXLineExample extends Application{
//application starts at this point
@Override
public void start(Stage s) {
//Create line 1
Line l1 = new Line();
//Set the x and y coordinates of l1
l1.setStartX(20.0);
l1.setStartY(60.0);
l1.setEndX(160.0);
l1.setEndY(60.0);
l1.setStrokeWidth(8);
l1.getStrokeDashArray().addAll(16d, 6d, 16d, 16d, 21d);
l1.setStrokeDashOffset(8);
//Create a Group
Group gp = new Group();
gp.getChildren().add(l1);
//Create a Scene
Scene sc = new Scene(gp, 600, 300);
//Set title
s.setTitle("JavaFX Line Example");
//Add the scene sc to the stage
s.setScene(sc);
//Display the results
s.show();
}
//main method
public static void main(String args[]){
launch(args);
}
}
Sample Output
A stroked line with the spacing and width 8 will be created on executing the code.
Conclusion
JavaFX line is a geometrical shape that will be connected with the help of 2 points in a coordinate plane X-Y. More details on JavaFX line is clearly discussed in this document.
Recommended Articles
This is a guide to JavaFX Line. Here we discuss the Constructors, properties, and methods along with the programs to implement JavaFX Line. You may also have a look at the following articles to learn more –