Home > Java > javaTutorial > body text

How to create polyline using JavaFX?

WBOY
Release: 2023-08-27 14:41:14
forward
816 people have browsed it

Polyline is an open figure formed using n straight lines existing in the same plane. That is, a polyline is the same as a polygon, except that it is not closed. In JavaFX, polylines are represented by the javafx.scene.shape.PolyLine class.

To create a polygon, you need to -

  • instantiate this class.

  • Pass the start and end points of the line segments of the drawn polygon to this class by passing them as arguments to the constructor or using the getPoints() method as -

polygon.getPoints().addAll(new Double[]{ List of XY coordinates separated by commas });
Copy after login
  • Add the created node (shape) to the Group object.

Example

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Polyline;
public class DrawingPolyLine extends Application {
   public void start(Stage stage) {
      //Drawing a polygon
      Polyline poliline = new Polyline();
      //Setting the properties of the ellipse
      poliline.getPoints().addAll(new Double[]{
         150.0, 200.0, 410.0, 200.0, 250.0, 50.0, 250.0, 230.0 });
      //Setting other properties
      poliline.setStrokeWidth(8.0);
      poliline.setStroke(Color.DARKSLATEGREY);
      //Setting the Scene
      Group root = new Group(poliline);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("Drawing Polyline");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}
Copy after login

Output

How to create polyline using JavaFX?

The above is the detailed content of How to create polyline using JavaFX?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!