VLineTo Path Object

The path element VLineTo is used to draw a vertical line to a point in the specified coordinates from the current position.

It is represented by a class named VLineTo. This class belongs to the package javafx.scene.shape.

This class has a property of the double datatype namely −

  • Y − The y coordinate of the point to which a vertical is to be drawn from the current position.

To draw the path element vertical line, you need to pass a value to this property. This can be done either by passing it to the constructor of this class at the time of instantiation; Or, by using its respective setter methods.

Steps to draw PathElement Vertical Line

To draw a vertical line to a specified point from the current position in JavaFX, follow the steps given below.

Step 1: Creating a Class

Create a Java class and inherit the Application class of the package javafx.application and implement the start() method of this class. Then create a path object by instantiating the Path class as follows.

publicclassClassNameextendsApplication{@Overridepublicvoidstart(Stage primaryStage)throwsException{//Creating a Path object Path path =newPath()}}

Step 2: Create a Path

Create the MoveTo path element and set XY coordinates to the starting point of the line to the coordinates (100, 150). This can be done by using the methods setX() and setY() of the class MoveTo as shown below.

//Moving to the starting point MoveTo moveTo =newMoveTo(); 
moveTo.setX(100.0f); 
moveTo.setY(150.0f)

Step 3: Creating an object of the class VLineTo

Create the path element vertical line by instantiating the class named VLineTo, which belongs to the package javafx.scene.shape as follows.

//Creating an object of the class VLineTo  VLineTo vLineTo =newVLineTo();

Step 4:Setting Properties to the Element Vertical Line

Specify the coordinates of the point to which a vertical line is to be drawn from the current position. This can be done by setting the properties x and y using their respective setter methods as shown in the following code block.

//Setting the Properties of the vertical line element 
lineTo.setX(500.0f); 
lineTo.setY(150.0f);

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

Add the path elements MoveTo and VlineTo created in the previous steps to the observable list of the Path class as follows −

//Adding the path elements to Observable list of the Path class   
path.getElements().add(moveTo); 
path.getElements().add(VlineTo);

Step 6: Launching Application

Once the VLineTo path object is created, follow the given steps below to launch the application properly −

  • Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.
  • Then, set the title to the stage using the setTitle() method of the Stage class.
  • Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
  • Display the contents of the scene using the method named show().
  • Lastly, the application is launched with the help of the launch() method.

Example 1

Following is a program which draws a vertical line from the current point to a specified position using the class Path of JavaFX. Save this code in a file with the name − VLineToExample.java.

importjavafx.application.Application;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.stage.Stage;importjavafx.scene.shape.VLineTo;importjavafx.scene.shape.MoveTo;importjavafx.scene.shape.Path;publicclassVLineToExampleextendsApplication{@Overridepublicvoidstart(Stage stage){//Creating an object of the Path class Path path =newPath();//Moving to the starting point MoveTo moveTo =newMoveTo(); 
      moveTo.setX(100.0); 
      moveTo.setY(150.0);//Instantiating the VLineTo class VLineTo vLineTo =newVLineTo();//Setting the properties of the path element vertical line 
      vLineTo.setY(10.0);//Adding the path elements to Observable list of the Path class 
      path.getElements().add(moveTo); 
      path.getElements().add(vLineTo);//Creating a Group object  Group root =newGroup(path);//Creating a scene object Scene scene =newScene(root,600,300);//Setting title to the Stage 
      stage.setTitle("Drawing a vertical line");//Adding scene to the stage 
      stage.setScene(scene);//Displaying the contents of the stage 
      stage.show();}publicstaticvoidmain(String args[]){launch(args);}}

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

javac --module-path %PATH_TO_FX%--add-modules javafx.controls VLineToExample.java 
java --module-path %PATH_TO_FX%--add-modules javafx.controls VLineToExample

Output

On executing, the above program generates a JavaFX window displaying a vertical line, which is drawn from the current position to the specified point, as shown below.

Drawing Vertical Line

Example 2

Following is a program which draws a rectangle using both vertical lines and horizontal lines using the class Path of JavaFX. Save this code in a file with the name − VLineToRectangle.java.

importjavafx.application.Application;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.stage.Stage;importjavafx.scene.shape.HLineTo;importjavafx.scene.shape.LineTo;importjavafx.scene.shape.MoveTo;importjavafx.scene.shape.Path;publicclassVLineToRectangleextendsApplication{@Overridepublicvoidstart(Stage stage){//Creating an object of the Path class Path path =newPath();//Drawing a triangular pathMoveTo moveTo =newMoveTo(); 
      moveTo.setX(200.0); 
      moveTo.setY(150.0);HLineTo hLineTo =newHLineTo();                
      hLineTo.setX(100.0);MoveTo moveTo2 =newMoveTo(); 
      moveTo2.setX(100.0); 
      moveTo2.setY(150.0);LineTo lineTo =newLineTo();                
      lineTo.setX(150.0);
      lineTo.setY(50.0);MoveTo moveTo3 =newMoveTo(); 
      moveTo3.setX(150.0); 
      moveTo3.setY(50.0);LineTo lineTo2 =newLineTo();       
      lineTo2.setX(200.0);
      lineTo2.setY(150.0);//Adding the path elements to Observable list of the Path class 
      path.getElements().add(moveTo); 
      path.getElements().add(hLineTo);
      path.getElements().add(moveTo2); 
      path.getElements().add(lineTo);
      path.getElements().add(moveTo3); 
      path.getElements().add(lineTo2);//Creating a Group object  Group root =newGroup(path);//Creating a scene object Scene scene =newScene(root,600,300);//Setting title to the Stage 
      stage.setTitle("Drawing a Triangular Path");//Adding scene to the stage 
      stage.setScene(scene);//Displaying the contents of the stage 
      stage.show();}publicstaticvoidmain(String args[]){launch(args);}}

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

javac --module-path %PATH_TO_FX%--add-modules javafx.controls VLineToRectangle.java 
java --module-path %PATH_TO_FX%--add-modules javafx.controls VLineToRectangle

Output

On executing, the above program generates a JavaFX window displaying a rectangle.

VLineTo Rectangle

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *