JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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