Wednesday, November 20, 2024
Google search engine
HomeLanguagesJavaJavaFX | ClosePath class

JavaFX | ClosePath class

ClosePath class is a part of JavaFX. ClosePath class closes the path by connecting the two unconnected ends of a specified path. ClosePath class inherits PathElement class.

Constructor of the class:

  • ClosePath() : Creates a new Closepath Object.

Commonly Used Method:

Method Explanation
toString() Returns the string representation of ClosePath Object.

Example: Java program to create a path and add multiple LineTo objects to it and close the path using ClosePath object and display it:

  • In this program, we will create a Path object named path.
  • Create four LineTo object with specified X and Y coordinates names Lineto, Lineto1, Lineto2, Lineto3.
  • Initially the path is not closed so we will close the path by adding a ClosePath object named close.
  • Then create a MoveTo object named moveto.
  • Now add the MoveTo and Lineto objects to the path.
  • 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 multiple 
// LineTo objects to it and close the path using 
// ClosePath object 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 closepath extends Application {
  
    // launch the application
    public void start(Stage stage)
    {
  
        try {
  
            // set title for the stage
            stage.setTitle("closePath");
  
            // create LineTo
            LineTo Lineto = new LineTo(300, 200);
            LineTo Lineto1 = new LineTo(200, 300);
            LineTo Lineto2 = new LineTo(100, 200);
            LineTo Lineto3 = new LineTo(150, 100);
  
            // create a closePath object
            ClosePath close = new ClosePath();
  
            // create moveto
            MoveTo moveto = new MoveTo(250, 100);
  
            // create a Path
            Path path = new Path(moveto, Lineto, Lineto1, 
                                Lineto2, Lineto3, close);
  
            // 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/ClosePath.html

RELATED ARTICLES

Most Popular

Recent Comments