Home  >  Article  >  Java  >  How to create path element arc in JavaFX?

How to create path element arc in JavaFX?

WBOY
WBOYforward
2023-08-26 22:53:16591browse

This class represents the path element arc. It helps you draw arcs from current coordinates to specified (new) coordinates.

Create a straight path element -

  • Instantiate the ArcTo class.

  • Use setter methods to set values ​​for the properties of this class, or bypass them in the constructor.

  • Instantiate the Path class.

  • Use getElements() to get the observable list object of the Path created above

  • Use add() Method adds the ArcTo object created above to the observable list.

  • Finally, add the path 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.ArcTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.VLineTo;
public class ArcToExample extends Application {
   public void start(Stage stage) {
      //Creating PathElement objects
      MoveTo moveTo = new MoveTo(490, 50);
      LineTo line1 = new LineTo(250, 250);
      //Instantiating the arcTo class
      ArcTo arcTo = new ArcTo();
      arcTo.setX(300.0);
      arcTo.setY(50.0);
      arcTo.setRadiusX(50.0);
      arcTo.setRadiusY(50.0);
      //Creating the HLineTo object
      VLineTo vLine = new VLineTo();
      vLine.setY(180);
      //Creating a Path
      Path path = new Path();
      path.getElements().addAll(moveTo, line1, arcTo, vLine);
      //Setting other properties
      path.setStrokeWidth(8.0);
      path.setStroke(Color.DARKSLATEGREY);  
      //Preparing the Stage object
      Group root = new Group(path);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("JavaFX Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

How to create path element arc in JavaFX?

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

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete