JavaFX | Polyline with examples

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 explanantion
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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    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



    My Personal Notes arrow_drop_up

    Second year Department of Information Technology Jadavpur University

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.