×

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 Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

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 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 Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 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 Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.

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 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 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 Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

4 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

4 minutes read.

JavaFX Translate Transition

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

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 Light Spot

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

4 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.