Monday 26 October 2020

JavaFX: Polyline

 

Polyline is similar to Polygon, only difference is, Polyline is unclosed shape, whereas Polygon is a closed shape by default.

 

Polyline class is used to creates a polyline, defined by the array of the segment points.

 

How to close a polyline?

Just add last point as same as first point.

 

Polyline class provides below constructors to define Polyline widget.

 

public Polyline()

public Polyline(double... points)

 

Example

Polyline polyline1 = new Polyline();

Polyline polyline2 = new Polyline(15.0, 350.0, 150.0, 350.0, 75.0, 150.0);

 

FXML snippet to define a polyline.

 

<Polyline fx:id="polyline2"  fill="lightsalmon" stroke="black" strokeWidth="5"

         points="100.0, 50.0, 150.0, 50.0, 150.0, 100.0, 100.0, 150.0"/>

 

Find the below working application.

 

polylineDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.PolygonController">

	<Polyline fx:id="polyline1"  fill="lightgreen" stroke="black" strokeWidth="5"
		points="15.0, 350.0, 150.0, 350.0, 75.0, 150.0"/>
		
	<Polyline fx:id="polyline2"  fill="lightsalmon" stroke="black" strokeWidth="5"
		points="100.0, 50.0, 150.0, 50.0, 150.0, 100.0, 100.0, 150.0"/>
	
</Group>

 

PolylineController.java

package com.sample.app.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.shape.Polyline;

public class PolylineController implements Initializable {

	@FXML
	private Polyline polyline1;

	@FXML
	private Polyline polyline2;

	@Override
	public void initialize(URL location, ResourceBundle resources) {

	}

}

 

PolylineDemo.java

package com.sample.app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class PolylineDemo extends Application {

	private static final String FXML_FILE = "/polylineDemo.fxml";
	private static final String STAGE_TITLE = "Polyline Demo";

	public static void main(String args[]) {
		launch(args);

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 300, 500, Color.WHITE);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

  Output

 


 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment