Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A line is known as the geometrical entity that connects the two points (X1, Y1) and (X2, Y2) in an X-Y coordinate plane. A JavaFX application's GUI can be customized by developers thanks to JavaFX. The class Line is a component of the javafx.scene. The JavaFX library provides a shape package. Basic shapes and colors are required in order to render graphics on the JavaFX scene.
A key basis class for all JavaFX scene graph nodes is the Node class. Thanks to this feature, any node can be transformed, translated, and given effects. The shape of the javafx.scene. The Node class has a descendent called Shape class. In JavaFX 8, all former JavaFX 2x Builder classes have been deprecated.
Basic shapes and colors are required in order to render graphics on the JavaFX scene. A key basis class for all JavaFX scene graph nodes is the Node class. Any node can be transformed, translated, and given effects thanks to this feature, the shape of the javafx.scene. The Node class has a descendent called Shape class. In JavaFX 8, all former JavaFX 2.x Builder classes have been deprecated.
How to create a line?
To make a Line, adhere to the directions below.
Create a JavaFX class instance.scene.shape.Line
Set the class object's necessary properties
Group the class object in
FX Lines
Lines are rendered using the screen coordinate system when they are drawn on the JavaFX scene graph (system). (0, 0) is located in the upper-left corner according to the screen's coordinate system. A point is moved along the x-axis by the x coordinate. The value of the y coordinate grows when a point is moved from top to bottom. Lines, circles, and rectangles are examples of scene graph objects that are derived classes of the Shape class in JavaFX.
All shape objects are capable of performing geometric operations like intersecting, union, and subtracting between two shaped regions.We'll employ JavaFX's javafx.scene.shape.Line class to create lines. We must enter a start (x, y) position and an end coordinate in order to generate a Line object.
When generating Line nodes, there are two ways to provide the start and end locations. The function Object() { [native code] } in the first method takes four parameters: startX, startY, endX, and endY. All of the parameters have double data types. The function Object() { [native code] } in the code below constructs a line with the start and end points (100, 10) and (10, 110).
Line line = new Line(100, 10, 10, 110);
The second way to generate a line node is by first creating an instance of the Line class with an empty function Object(), then setting each attribute using setter methods. The line object creation and setter methods used to determine a line's start and end points are demonstrated in the code below.
Line line = new Line();
line.setStartX(100);
line.setStartY(10);
line.setEndX(10);
line.setEndY(110);
Properties to create Line
startX 1 : The setStartX() method aids in setting the X-coordinate starting point of the line, which is defined by this property of the Line class.
endX : With the aid of the setEndX() method, we can define the line's X-coordinate endpoint using this attribute of the Line class.
startY 3: This Line class property allows us to specify the line's Y coordinate starting point, and the setStartY() method aids in setting it.
endY : The setEndY() method assists in setting the line's Y coordinate endpoint, which can be defined using this property of the Line class.
Example 1
Java program to create a line with starting and ending coordinates passed as arguments.
Code
//files to be imported
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Line;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
// Java program to create a line with starting and ending coordinates passed as arguments
public class line_0 extends Application {
// launch the application
public void start(Stage stage)
{
// set the title for stage
stage.setTitle("creating a line");
// creates a line
Line line = new Line(10.0f, 10.0f, 200.0f, 140.0f);
// creates a Group
Group group = new Group(line);
// translates the line to a position
line.setTranslateX(100);
line.setTranslateY(100);
// creates a scene
Scene scene = new Scene(group, 500, 300);
// sets the scene
stage.setScene(scene);
stage.show();
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output
Explanation
This program generates a Line with the name line as its only input (the start point and the endpoint is passed as arguments). The Line will be produced within a setting that is housed within a stage. The stage's title is provided through the setTitle() method. The line is then added after creating a Group. The group is associated with the location. The show() method is then used to display the outcome.
Frequently Asked Questions
What are lines?
A line is known as the geometrical entity that connects the two points (X1, Y1) and (X2, Y2) in an X-Y coordinate plane.
Will the above programs work in an online IDE?
No, the programs might not run in an online IDE, so it is advised to use an offline compiler.
What is the fill property used for?
It is used as a color to fill inside a shape.
Conclusion
This article extensively discussed the implementation and use of the JavaFX Line. We hope this blog has helped you gain more knowledge of JavaFX lines. Check out our articles on Coding Ninjas.