Open In App

JavaFX | HLineTo Class

Last Updated : 20 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

HLineTo class is a part of JavaFX. HLineTo class creates a Horizontal line from the present position to specified x coordinate. HLineTo class inherits PathElement class.

Constructor of the class:

  1. HLineTo(): Creates an empty object of HLineTo.
  2. HLineTo(double x): Creates an object of HLineTo with specified value of x coordinate.

Commonly Used Methods:

Method Explanation
getX() Returns the value of x coordinate.
setX(double v) Sets the value of x coordinate.
toString() Returns the string representation of HLineTo object.
xProperty() Defines the X coordinate.

Below programs illustrate the use of HLineTo Class:

  • Java program to create a path and add HLineTo to it and display it:
    1. In this program, we will create a Path object named path.
    2. Create an HLineTo object with specified X coordinate.
    3. Then create a MoveTo object named moveto.
    4. Now add the MoveTo and HLineto object to the path.
    5. Add this path to Group object and add the group object to the scene and add the scene to the stage and call the show() function to display the final results.




    // Java program to create a path
    // and add HLineTo to it and display it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.geometry.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
    import javafx.scene.paint.*;
    import javafx.scene.*;
      
    public class HLineTo_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("HLineTo");
      
                // create HLineTo
                HLineTo HLineto = new HLineTo(200);
      
                // create moveto
                MoveTo moveto = new MoveTo(100, 100);
      
                // create a Path
                Path path = new Path(moveto, HLineto);
      
                // set fill for path
                path.setFill(Color.BLACK);
      
                // set stroke width
                path.setStrokeWidth(2);
      
                // create a Group
                Group group = new Group(path);
      
                // create a scene
                Scene scene = new Scene(group, 400, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  • Java program to create a path and add multiple HLineTo object to it and display it:
    1. In this program, we will create a Path object named path.
    2. Create three HLineTo object with specified X coordinate.
    3. Then create three MoveTo object named moveto, moveto_1, and moveto_2.
    4. Add all the MoveTo and HLineTo objects to the path in a order.
    5. After that add this path to Group object.
    6. Add the group object to scene and add the scene to the stage and call the show() function to display the final results.




    // Java program to create a path and add 
    // multiple HLineTo object to it and display it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.geometry.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
    import javafx.scene.paint.*;
    import javafx.scene.*;
      
    public class HLineTo_2 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("HLineTo");
      
                // create HLineTo
                HLineTo HLineto = new HLineTo(200);
                HLineTo HLineto_1 = new HLineTo(250);
                HLineTo HLineto_2 = new HLineTo(225);
      
                // create moveto
                MoveTo moveto = new MoveTo(100, 100);
                MoveTo moveto_1 = new MoveTo(150, 200);
                MoveTo moveto_2 = new MoveTo(200, 300);
      
                // create a Path
                Path path = new Path(moveto, HLineto, moveto_1, 
                               HLineto_1, moveto_2, HLineto_2);
      
                // set fill for path
                path.setFill(Color.BLACK);
      
                // set stroke width
                path.setStrokeWidth(2);
      
                // create a Group
                Group group = new Group(path);
      
                // create a scene
                Scene scene = new Scene(group, 400, 400);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

Note: The above programs might not run in an online IDE please use an offline compiler.

Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/HLineTo.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads