Thursday, July 4, 2024
HomeLanguagesJavaJavaFX | Polyline with examples

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments