×

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in the space. All methods needed for this purpose are present in the javafx.scene.shape.Line class.

Properties and corresponding methods to create line:

1. startX

This property of the Line class allows us to define the starting point of line which is X co-ordinate and the setStartX() method helps to set it.

2. endX

This property of the Line class allows us to define the end point of line which is X co-ordinate and setEndX() method helps to set it.

3. startY

This property of the Line class allows us to define the starting point of line which is Y co-ordinate and the setStartY() method helps to set it.

4. endY

This property of the Line class allows us to define the end point of line which is Y co-ordinate and the setEndY() method help to set it.

JavaFX 2D Shape -Line:

Example:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;


  
public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Line newline = new Line(); 
    newline.setStartX(50); 
    newline.setStartY(50);
    newline.setEndX(50);
    newline.setEndY(250);   
         Group box = new Group(); 
         box.getChildren().add(newline);         
           Scene scene = new Scene( box, 600, 400 );  
           primaryStage.setScene(scene);  
           primaryStage.setTitle("JavaFX 2D shapes - Line Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Line in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Line.

Then we have created one class named LineUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a Line, a Group object is created which is then passed to the Scene class object.

The line is created with the help of an empty constructor and co-ordinates are set using the setters method for X and Y co-ordinates.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Line Example”. Also, it displays one line from point(50,50) to point(50,250).

JavaFX 2D Shape -Line

JavaFX 2D Shape Lines to form shapes:

Example:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;


  
public class LineUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
         Line newline1 = new Line (20, 70, 170, 70);      
         Line newline2 = new Line (20, 120, 170, 120);  
         Line newline3 = new Line (20, 70, 20, 120);  
         Line newline4 = new Line (170, 70, 170, 120);  
         Group box = new Group();  
         box.getChildren().addAll( newline1, newline2, newline3, newline4 );      
         Scene scene = new Scene ( box, 600, 400 );  
           primaryStage.setScene(scene);  
           primaryStage.setTitle( "JavaFX 2D shapes - Line forming shapes Example");  
           primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Lines forming shape in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.Group, javafx.scene.shape.Line.

Then we have created one class named LineUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a shape formed by Lines in JavaFX, a Group object is created which is then passed to the Scene class object.

Lines are created with constructor having startX, startY, endX, endY co-ordinates passed in it. Different 4 constructor of Line are created as,  Line newline1 = new Line( 20, 70, 170, 70), Line newline2 = new Line(20, 120, 170, 120), Line newline3 = new Line(20, 70, 20, 120), Line newline4 = new Line(170, 70, 170, 120) and passed to group object.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, " JavaFX 2D shapes - Line forming shapes Example". Also, it displays four lines forming one rectangle as per given X and Y coordinates.

JavaFX 2D Shape -Line

Related Topics

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to...

4 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Area Chart

In the JavaFX application, in order to create Area Chart, the AreaChart class is used. The Area Chart allows us to plot data along the XY plane. All methods needed...

3 minutes read.

JavaFX CheckBox

In the JavaFX application, to get the user information in the form of choices from the user, a form is created using checkboxes that allow the user to select two...

4 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Rectangle

In the JavaFX application, in order to draw a rectangle, the Rectangle class is used. The Rectangle allows us to join any four points with x and y coordinates in...

4 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.