Javafx 简明教程

JavaFX - ArcTo Path Object

Path 元素 Arc 用于从当前位置绘制一条到指定坐标点处的弧形路径。

The Path Element Arc is used to draw an arc to a point in the specified coordinates from the current position.

它由名为 ArcTo 的类表示。此类属于 javafx.scene.shape 包。

It is represented by a class named ArcTo. This class belongs to the package javafx.scene.shape.

此类具有 4 个双数据类型的属性,即 −

This class has 4 properties of the double datatype namely −

  1. X − The x coordinate of the center of the arc.

  2. Y − The y coordinate of the center of the arc.

  3. radiusX − The width of the full ellipse of which the current arc is a part of.

  4. radiusY − The height of the full ellipse of which the current arc is a part of.

要绘制路径元素弧线,需要将值传递给这些属性。在实例化时,可以通过按相同顺序将它们传递给此类的构造函数来完成此操作;或使用它们各自的 setter 方法。

To draw the Path element arc, you need to pass values to these properties This can be done by passing them to the constructor of this class, in the same order, at the time of instantiation; Or, by using their respective setter methods.

Steps to draw PathElement Arc

要在 JavaFX 中将弧线绘制到从当前位置到指定点的弧线,请按照以下步骤操作。

To draw an arc to a specified point from the current position in JavaFX, follow the steps given below.

Step 1: Creating a Path Object

通过在 Application 类的 start() 方法中实例化 Path 类来创建 Path 对象,如下所示。

Create a Path object by instantiating the Path class inside the start() method of Application class as shown below.

public class ClassName extends Application {
   @Override
   public void start(Stage primaryStage) throws Exception {
      //Creating a Path object
      Path path = new Path();
   }
}

Step 2: Create a Path

创建 MoveTo 路径元素,并将 XY 坐标设置到该行的起点,坐标为 (100, 150)。可以使用类 MoveTo 的方法 setX()setY() 来完成,如下所示。

Create the MoveTo path element and set the XY coordinates to the starting point of the line to the coordinates (100, 150). This can be done using the methods setX() and setY() of the class MoveTo as shown below.

//Moving to the starting point
MoveTo moveTo = new MoveTo();
moveTo.setX(100.0f);
moveTo.setY(150.0f);

Step 3: Creating an Object of the Class ArcTo

通过实例化名为 ArcTo 的类来创建路径元素二次贝塞尔曲线,该类属于包 javafx.scene.shape ,如下所示 −

Create the path element quadratic curve by instantiating the class named ArcTo, which belongs to the package javafx.scene.shape as shown below −

//Creating an object of the class ArcTo
ArcTo arcTo = new ArcTo()

Step 4: Setting Properties to the Arc Element

指定椭圆的中心(此弧线的一部分)的 x、y 坐标。然后,可以使用它们各自的 setter 方法指定 radiusX、radiusY、开始角度和弧线的长度,如下所示。

Specify the x, y coordinates of the center of the ellipse (of which this arc is a part of). Then you can specify the radiusX, radiusY, start angle, and length of the arc using their respective setter methods as shown below.

//setting properties of the path element arc
arcTo.setX(300.0);
arcTo.setY(50.0);

arcTo.setRadiusX(50.0);
arcTo.setRadiusY(50.0);

Step 5: Adding Elements to the Observable List of Path Class

将以前步骤中创建的路径元素 MoveToarcTo 添加到 Path 类的可观察列表中,如下所示 −

Add the path elements MoveTo and arcTo, created in the previous steps to the observable list of the Path class as follows −

//Adding the path elements to Observable list of the Path class
path.getElements().add(moveTo);
path.getElements().add(cubicCurveTo);

Step 6: Launching Application

创建 Arc 路径对象后,请按以下给定步骤正确启动应用程序 −

Once the Arc path object is created, follow the given steps below to launch the application properly −

  1. Firstly, instantiate the class named Scene by passing the Group object as a parameter value to its constructor. To this constructor, you can also pass dimensions of the application screen as optional parameters.

  2. Then, set the title to the stage using the setTitle() method of the Stage class.

  3. Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  4. Display the contents of the scene using the method named show().

  5. Lastly, the application is launched with the help of the launch() method.

Example

以下是使用 JavaFX 的 Path 类绘制从当前点到指定位置的弧线的程序。将此代码保存在名为 ArcExample.java 的文件中。

Following is a program that draws an arc from the current point to a specified position using the class Path of JavaFX. Save this code in a file with the name ArcExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;

public class ArcExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating an object of the class Path
      Path path = new Path();

      //Moving to the starting point
      MoveTo moveTo = new MoveTo();
      moveTo.setX(250.0);
      moveTo.setY(250.0);

      //Instantiating the arcTo class
      ArcTo arcTo = new ArcTo();

      //setting properties of the path element arc
      arcTo.setX(300.0);
      arcTo.setY(50.0);

      arcTo.setRadiusX(50.0);
      arcTo.setRadiusY(50.0);

      //Adding the path elements to Observable list of the Path class
      path.getElements().add(moveTo);
      path.getElements().add(arcTo);

      //Creating a Group object
      Group root = new Group(path);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Drawing an arc through a path");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample

执行上述程序后,将生成一个 JavaFX 窗口,其中显示一个弧线,该弧线从当前位置绘制到指定点,如下所示。

On executing, the above program generates a JavaFX window displaying an arc, which is drawn from the current position to the specified point, as shown below.

drawing arc path

Example

让我们尝试在 JavaFX 中绘制一个 ArcTo 路径以创建一个摆锤对象,该对象有一个在 Arc 路径上往复运动的圆形摆。将此代码保存在名为 ArcToAnimation.java 的文件中。

Let us try to draw an ArcTo Path in JavaFX to create a pendulum object with a circular pendulum moving to and fro on an Arc Path. Save this code in a file with the name ArcToAnimation.java.

import javafx.application.Application;
import javafx.animation.PathTransition;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.*;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.util.Duration;

public class ArcToAnimation extends Application {
   @Override
   public void start(Stage stage) {
      // Drawing a circle
      Circle circle = new Circle(300.0f, 50.0f, 40.0f);

      //Creating an object of the class Path
      Path path = new Path();

      //Moving to the starting point
      MoveTo moveTo = new MoveTo();
      moveTo.setX(250.0);
      moveTo.setY(250.0);

      //Instantiating the arcTo class
      ArcTo arcTo = new ArcTo();

      //setting properties of the path element arc
      arcTo.setX(300.0);
      arcTo.setY(50.0);

      arcTo.setRadiusX(50.0);
      arcTo.setRadiusY(50.0);

      //Adding the path elements to Observable list of the Path class
      path.getElements().add(moveTo);
      path.getElements().add(arcTo);

      //Creating a path transition
      PathTransition pathTransition = new PathTransition();

      //Setting the duration of the path transition
      pathTransition.setDuration(Duration.millis(1000));

      //Setting the node
      pathTransition.setNode(circle);

      //Setting the path
      pathTransition.setPath(path);

      //Setting the orientation of the path
      pathTransition.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);

      //Setting the cycle count for the transition
      pathTransition.setCycleCount(50);

      //Setting auto reverse value to true
      pathTransition.setAutoReverse(true);

      //Playing the animation
      pathTransition.play();

      //Creating a Group object
      Group root = new Group();
      root.getChildren().addAll(circle, path);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Drawing an arc through a path");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ArcExample

执行上述程序后,将生成一个 JavaFX 窗口,其中显示了一个摆锤路径,如下所示。

On executing, the above program generates a JavaFX window displaying a pendulum path as shown below.

arcto pendulum