Draw a straight line with the Line class in JavaFX - ProgrammerTech
Language:

Search

Draw a straight line with the Line class in JavaFX

  • Share this:
Draw a straight line with the Line class in JavaFX

Draw a line on the container Scene

Create a Line on the Stage model and to create a Line we will use the package:

scene.shape.Line

This package is used to create a Line and we will be using the root of the Group type. Class Line is used to obtain a straight line that can be displayed vertically, horizontally and diagonally. In the previous lessons we learned how to create a root of type VBox and it does not work with graphics, we will use . Instead, today the root is of the Group type, and this root is the one that works with the graphics. Today we will learn in a simple and easy lesson how to create a line and how to add it to the root and then add. Root to the Scene container and then show it on the model as we learned in the previous lessons. To create a line we use the class Line and then give it an object.

To determine the starting point of a straight line we use the functions setStartX() and setStartY().

To determine the starting point of a straight line we use the functions setEndX() and setEndY().

Example 1

package parrotsy;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class Parrotsy extends Application {  
    public void start(Stage stage) {
        Line line=new Line();
        line.setStartX(100.0);
        line.setStartY(50.0);
        line.setEndX(500.0);
        line.setEndY(50.0);
        Group root=new Group(line);
        Scene scene=new Scene(root,600,300);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

We have called several packages, including the new packages, which is the Group package, which is a special package in the root that deals with graphics. It is an alternative to the VBox package. The shape.Line package is the package for the line to be drawn. And we entered the function start and its object is stage, and within this function we wrote the program as follows:

We created the line class with the Line class and the object for this class is line, then we set the starting X coordinates via setStartX. And it starts from point 100 in the transverse path. We also set the starting point of Y on the vertical path from top to bottom. Then we will define the ends of X and Y, which is that X ends at the point 500 via setEndX, and we set the end point of Y at the point 50 and it remains constant. Then we added the root to the container with the Scene class and the scene object and set the area to 600,300. Then we add the container to the scene or the so-called model and then show this model via stage.show.

And in the main program we run the start function via launch(args) and when we run the previous code, the result will be like:

1-82.webp

Example 2

package parrotsy;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class Parrotsy extends Application {
    public void start(Stage stage) {
        Line line=new Line();
        line.setStartX(100.0);
        line.setStartY(50.0);
        line.setEndX(100.0);
        line.setEndY(300.0);
        Group root=new Group(line);
        Scene scene=new Scene(root,600,300);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

We have called several packages, including the new packages, which is the Group package, which is a root package that deals with graphics and is an alternative to the VBox package. The shape.Line package is the package for the line to be drawn. And we entered the function start and its object is stage and within this function we wrote the program as follows: We created the class of the line with the class Line and the object of this class is line. Then we set the start X coordinates via setStartX, which is to start from point 100 in the transverse path, and we also set the starting point Y in the vertical path from top to bottom.

Then we will define the limits of X and Y, which is that X ends at point 100 via setEndX and we set the end point of Y at point 300 and it remains constant. Then we added the root to the container with the Scene class and the scene object and set the area to 600,300. Then we add the container to the scene or the so-called model and then show this model via stage.show. And in the main program we run the start function via launch(args).

And when you run the previous code, the result will be like this.

2-75.webp

As you can see, we fixed the point X and changed the Y coordinates, so the line went from bottom to top. I mean, if you want the line to go from left to right, we fix point Y and change. In the X points, and if we want the line to go from the bottom to the top, we fix the X point and change the Y points.


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com