Home > Java > javaTutorial > In JavaFX, what are the different path elements?

In JavaFX, what are the different path elements?

王林
Release: 2023-08-28 12:53:20
forward
1059 people have browsed it

javafx.scene.shape The package provides some classes that you can use to draw various 2D shapes, but these are just primitive shapes such as lines, circles, polygons, ellipses, etc. etc... So if you want to draw complex custom shapes, you need to use the Path class.

Path Class

Path Class You can draw custom paths using this geometric outline that represents a shape.

To draw custom paths, JavaFX provides various path elements, all of which are available as classes in the javafx.scene.shape package.

  • LineTo - This class represents the path element line. It helps you draw a straight line from the current coordinates to the specified (new) coordinates.

  • HlineTo - This is the class horizontal line that represents a path element. It helps you draw a horizontal line from the current coordinates to the specified (new) coordinates.

  • VLineTo - This is the class vertical line that represents the path element. It helps you draw a vertical line from the current coordinates to the specified (new) coordinates.

  • QuadCurveTo - This class represents the path element quadratic curve. It helps you draw a quadratic curve from current coordinates to current coordinates Specify (new) coordinates.

  • CubicCurveTo - This class represents the path element cubic curve. It helps you draw a cubic curve from the current coordinates to the specified (new) coordinates.

  • ArcTo - This is the class Arc that represents the path element. It helps you draw an arc from the current coordinates to the specified (new) coordinates.

  • MoveTo - Using this class you can move a path from the current coordinates to the new coordinates.

Creating paths using path elements

The Path class contains an observable list that holds the current path. So, to draw a path -

  • #instantiate the required PathElement class.

  • Set the properties of each path using the setter method, or pass them as arguments to the constructor.

  • Instantiate the Path class.

  • Get the observable object using the getElements() method to get the list object of the Path created above.

  • Add all path element objects to the observable list in the desired order using the add() or addAll() method.

  • Finally, add the path to the Group object.

Note - You can also pass path elements to the constructor of the Path class.

Example

The following JavaFX example uses the LineTo path element to create a path−

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.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
public class PathElementsExample extends Application {
   public void start(Stage stage) {
      //Drawing the shape
      MoveTo moveTo = new MoveTo(208, 71);
      LineTo line1 = new LineTo(421, 161);
      LineTo line2 = new LineTo(226,232);
      LineTo line3 = new LineTo(332,52);
      LineTo line4 = new LineTo(369, 250);
      LineTo line5 = new LineTo(208, 71);
      //Creating a Path
      Path path = new Path(moveTo, line1, line2, line3, line4, line5);
      path.setFill(Color.DARKCYAN);
      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("Drawing an arc through a path");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}
Copy after login

Output

In JavaFX, what are the different path elements?

The above is the detailed content of In JavaFX, what are the different path elements?. 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