JavaFX有其他内置的形状,如:

  • Arc
  • Circle
  • CubicCurve
  • Ellipse
  • Line
  • Path
  • Polygon
  • Polyline
  • QuadCurve
  • Rectangle
  • SVGPath
  • Text

    以下代码显示了如何创建路径(Path)。
    ```java import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.effect.DropShadow; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Ellipse; import javafx.scene.shape.LineTo; import javafx.scene.shape.MoveTo; import javafx.scene.shape.Path; import javafx.stage.Stage;

public class Main extends Application {

  1. @Override
  2. public void start(Stage stage) throws Exception {
  3. Group group = new Group();
  4. Scene scene = new Scene(group);
  5. stage.setTitle("Checkbox Sample");
  6. stage.setWidth(230);
  7. stage.setHeight(120);
  8. Path path = new Path();
  9. path.getElements().add(new MoveTo(0.0f, 50.0f));
  10. path.getElements().add(new LineTo(100.0f, 100.0f));
  11. VBox vbox = new VBox();
  12. vbox.getChildren().addAll(path);
  13. vbox.setSpacing(5);
  14. HBox root = new HBox();
  15. root.getChildren().add(vbox);
  16. root.setSpacing(40);
  17. root.setPadding(new Insets(20, 10, 10, 20));
  18. group.getChildren().add(root);
  19. stage.setScene(scene);
  20. stage.show();
  21. }
  22. public static void main(String[] args) {
  23. launch(args);
  24. }

}

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23145762/1650298397425-b7a8e235-7988-4d14-a45f-d67b144090a7.png#clientId=u459dc24d-cbb4-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=214&id=u55b1af4c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=268&originWidth=409&originalType=binary&ratio=1&rotation=0&showTitle=false&size=3785&status=done&style=shadow&taskId=ua79a1579-c3c2-4939-a947-9f111c081ec&title=&width=327.2)<br />Path元素实际上从javafx.scene.shape.PathElement类扩展,它仅在Path对象的上下文中使用。<br />所以不能实例化一个LineTo类放在场景图中。使用To作为后缀的类是Path元素,而不是Shape节点。<br />例如,MoveTo和LineTo对象实例可添加到Path对象的Path元素中,而不可以添加到场景的形状。
  2. <a name="IehEI"></a>
  3. ## 示例-1
  4. 以下代码显示了如何将QuadCurveTo添加到路径。
  5. ```java
  6. import javafx.application.Application;
  7. import javafx.scene.Group;
  8. import javafx.scene.Scene;
  9. import javafx.scene.shape.MoveTo;
  10. import javafx.scene.shape.Path;
  11. import javafx.scene.shape.QuadCurveTo;
  12. import javafx.stage.Stage;
  13. public class Main extends Application {
  14. @Override
  15. public void start(Stage stage) {
  16. Group root = new Group();
  17. Scene scene = new Scene(root, 300, 150);
  18. stage.setScene(scene);
  19. stage.setTitle("曲线");
  20. Path path = new Path();
  21. MoveTo moveTo = new MoveTo();
  22. moveTo.setX(0.0f);
  23. moveTo.setY(50.0f);
  24. QuadCurveTo quadTo = new QuadCurveTo();
  25. quadTo.setControlX(25.0f);
  26. quadTo.setControlY(0.0f);
  27. quadTo.setX(50.0f);
  28. quadTo.setY(50.0f);
  29. path.getElements().add(moveTo);
  30. path.getElements().add(quadTo);
  31. root.getChildren().add(path);
  32. scene.setRoot(root);
  33. stage.show();
  34. }
  35. public static void main(String[] args) {
  36. launch(args);
  37. }
  38. }

image.png

实例-2

使用Path,MoveTo和CubicCurveTo创建曲线 -

  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.shape.CubicCurveTo;
  5. import javafx.scene.shape.MoveTo;
  6. import javafx.scene.shape.Path;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. @Override
  13. public void start(Stage stage) {
  14. stage.setTitle("ComboBoxSample");
  15. Scene scene = new Scene(new Group(), 450, 250);
  16. Path path = new Path();
  17. MoveTo moveTo = new MoveTo();
  18. moveTo.setX(0.0f);
  19. moveTo.setY(0.0f);
  20. CubicCurveTo cubicTo = new CubicCurveTo();
  21. cubicTo.setControlX1(0.0f);
  22. cubicTo.setControlY1(0.0f);
  23. cubicTo.setControlX2(100.0f);
  24. cubicTo.setControlY2(100.0f);
  25. cubicTo.setX(100.0f);
  26. cubicTo.setY(50.0f);
  27. path.getElements().add(moveTo);
  28. path.getElements().add(cubicTo);
  29. Group root = (Group) scene.getRoot();
  30. root.getChildren().add(path);
  31. stage.setScene(scene);
  32. stage.show();
  33. }
  34. }

image.png

实例-3

减去两个形状以创建路径,如下代码 -

  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.paint.Color;
  5. import javafx.scene.shape.*;
  6. import javafx.stage.Stage;
  7. public class Main extends Application {
  8. @Override
  9. public void start(Stage stage) throws Exception {
  10. stage.setTitle("Shapes");
  11. Group root = new Group();
  12. Scene scene = new Scene(root, 300, 300, Color.WHITE);
  13. Ellipse bigCircle = new Ellipse(100,100,50,75/2);
  14. bigCircle.setStrokeWidth(3);
  15. bigCircle.setStroke(Color.BLACK);
  16. bigCircle.setFill(Color.WHITE);
  17. Ellipse smallCircle = new Ellipse(100,100,35/2,25/2);
  18. Shape shape = Path.subtract(bigCircle, smallCircle);
  19. shape.setStrokeWidth(1);
  20. shape.setStroke(Color.BLACK);
  21. shape.setFill(Color.rgb(255, 200, 0));
  22. root.getChildren().add(shape);
  23. stage.setScene(scene);
  24. stage.show();
  25. }
  26. public static void main(String[] args) {
  27. launch(args);
  28. }
  29. }

image.png

实例-4

使用VLineTo创建垂直线,如下代码所示 -

  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.layout.VBox;
  5. import javafx.scene.shape.MoveTo;
  6. import javafx.scene.shape.Path;
  7. import javafx.scene.shape.VLineTo;
  8. import javafx.stage.Stage;
  9. public class Main extends Application {
  10. @Override
  11. public void start(final Stage stage) {
  12. stage.setTitle("HTML");
  13. stage.setWidth(500);
  14. stage.setHeight(500);
  15. Scene scene = new Scene(new Group());
  16. VBox root = new VBox();
  17. Path path = new Path();
  18. path.getElements().add(new MoveTo(50.0f, 0.0f));
  19. path.getElements().add(new VLineTo(50.0f));
  20. root.getChildren().addAll(path);
  21. scene.setRoot(root);
  22. stage.setScene(scene);
  23. stage.show();
  24. }
  25. public static void main(String[] args) {
  26. launch(args);
  27. }
  28. }

image.png