How to Draw Line Javafx

What is 2D Shapes PathElement Horizontal Line?

The path detail HLineTo is used to draw a horizontal line to a point in the specific coordinates from the present day position.

It is represented through a class named HLineTo. This class belongs to the package javafx.scene.shape.

This class has a assets of the double datatype namely −

  • X − The x coordinate of the point to which a horizontal line is to be drawn from the present day position.

To draw a path detail horizontal line, you want to pass a value to this assets. this will be both carried out by using passing it to the constructor of this class, on the time of instantiation, as follows –

Or, by using their respective setter methods as shown below −

Steps to draw PathElement Horizontal Line

Observe the steps given under to draw a horizontal line to a specified point from the present day position in JavaFX.

Step 1: creating a class

Create a Java class and inherit the Application class of the package javafx.application and implement the start() technique of this class as follows.

Step 2: Instantiating the Path Class

Create the path class object as follows –

Step 3: Setting the Initial Point

Create the MoveTo path detail and set XY coordinates to starting point of the line to the coordinates (100, 150). this will be carried out by using using the techniques setX() and setY() of the class MoveTo as proven below.

Step 4: Creating an Object of the Class HLineTo

Create the path detail horizontal line through instantiating the class named HLineTo which belongs to the package javafx.scene.shape as shown below.

Step 5: Setting Properties to the Horizontal Line Element

Specify the x coordinate of the factor to which a horizontal line is to be drawn from the present day position. this may be completed by setting the property x, using the technique setX() of the HLine To class as shown below.

Step 6: Adding Elements to the Observable List of Path Class

Add the path elements MoveTo and HlineTo created in the previous steps to the observable list of the path class as shown below –

Step 7: Creating a Group Object

Create a group item by using instantiating the class named Group, which belongs to the package javafx.scene.

Pass the line (node) object created in the preceding step as a parameter to the constructor of the group class. This need to be done for you to add it to the group as shown under −

Step 8: Creating a Scene Object

Create a Scene through instantiating the class named Scene which belongs to the package javafx.scene. To this class, bypass the group item (root) created in the previous step.

In addition to the root object, you may also pass two double parameters representing peak and width of the screen along with the object of the group class as follows –

Step 9: Setting the Title of the Stage

You may set the title to the stage the use of the setTitle() technique of the stage class. The primaryStage is a stage object that is passed to the start technique of the scene class as a parameter.

The use of the primaryStage object, set the name of the scene as sample application as follows.

Step 10: Adding Scene to the Stage

You may upload a Scene object to the stage the usage of the technique setScene() of the class named stage. add the Scene object prepared inside the previous steps using this technique as proven below −

Step 11: Displaying the Contents of the Stage

Display the contents of the scene using the method named show() of the Stage class as follows.

Step 12: Launching the Application

Launch the JavaFX application through calling the static technique launch() of the Application class from the main technique as follows.

Example

Following is a program which draws a horizontal line from the present day point to a certain position using the class path of JavaFX. save this code in a record with the name − HLineToExample.java.

Collect and execute the saved java file from the command prompt using the following commands.

On executing, the above program generates a JavaFX window showing a horizontal line, which is drawn from the present day position to the desired point, as shown below.

2D Shapes PathElement Horizontal Line

How to Draw Line Javafx

Source: https://www.wisdomjobs.com/e-university/javafx-tutorial-1700/2d-shapes-pathelement-horizontal-line-22567.html

0 Response to "How to Draw Line Javafx"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel