Open In App

JavaFX | Polyline with examples

Last Updated : 31 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Polyline is part of the JavaFX library. Polyline is a set of connected points. Though Polyline is almost similar to the Polygon class, the only difference is polygon forms a closed area whereas the Polyline can form both closed and open area. Also, Polyline class extends shape class.

Constructors for class are:

  1. Polyline():Creates an empty instance of Polyline.
  2. Polyline(double… points): creates a new instance of polyline with given points

Commonly used methods:

method explanation
getPoints() gets the points of the polyline segments
toString() Returns a string representation of this Polyline object.

Below programs illustrate the use of Polyline:

  1. Program to create an open area of connected segments using polyline: This program creates a Polyline indicated by the name polyline( the coordinates of the points of the line segments to create an open area). The Polyline will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the polyline is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.




    // Java Program to create a open area
    // of connected segments using polyline
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Polyline;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    public class Polyline_0 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
            // set title for the stage
            stage.setTitle("creating Polyline");
      
            // points
            double points[] = { 20.0d, 20.0d, 40.0d, 240.0d, 60.0d,
                              180.0d, 80.0d, 200.0d, 100.0d, 90.0d };
      
            // create a polyline
            Polyline polyline = new Polyline(points);
      
            // create a Group
            Group group = new Group(polyline);
      
            // create a scene
            Scene scene = new Scene(group, 500, 300);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        public static void main(String args[])
        {
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  2. Program to create a closed area of connected segments using polyline: This program creates a Polyline indicated by the name polyline( the coordinates of the points of the line segments to create a closed area). The Polyline will be created inside a scene, which in turn will be hosted inside a stage. The function setTitle() is used to provide title to the stage. Then a Group is created, and the polyline is attached. The group is attached to the scene. Finally, the show() method is called to display the final results.




    // Java Program to create a closed area
    // of connected segments using polyline
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.*;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Polyline;
    import javafx.scene.control.*;
    import javafx.stage.Stage;
    import javafx.scene.Group;
    public class Polyline_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
            // set title for the stage
            stage.setTitle("creating Polyline");
      
            // points
            double points[] = { 20.0d, 20.0d, 40.0d, 240.0d, 60.0d,
                 180.0d, 80.0d, 200.0d, 100.0d, 90.0d, 20.0d, 20.0d };
      
            // create a polyline
            Polyline polyline = new Polyline(points);
      
            // create a Group
            Group group = new Group(polyline);
      
            // create a scene
            Scene scene = new Scene(group, 500, 300);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        public static void main(String args[])
        {
            // launch the application
            launch(args);
        }
    }

    
    

    Output:

  3. Note :The above programs might not run in an online IDE please use an offline converter.
    Reference:https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Polyline.html



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

Similar Reads