首頁 > Java > java教程 > JavaFX提供的各種2D形狀有哪些?

JavaFX提供的各種2D形狀有哪些?

PHPz
發布: 2023-09-03 21:41:05
轉載
1376 人瀏覽過

以下是您可以使用 JavaFX 繪製的各種幾何形狀

  • - 一條線是連接兩點的幾何結構。 javafx.scene.shapeLine 類別表示 XY 平面中的一條線。

  • 矩形 - 矩形是一個四邊多邊形,具有兩對平行且並發的邊,所有內角均為直角。 javafx.scene。 Rectangle 類別表示 XY 平面中的矩形。

  • Circle strong> - 圓是一條形成閉環的線,其上的每個點距中心點都有固定的距離。 javafx.scene。 Circle 類別表示 XY 平面中的圓。

  • 橢圓 strong> - 橢圓由兩個點定義,每個點稱為焦點。如果取橢圓上的任一點,到焦點的距離總和是恆定的。 javafx.scene.Ellipse 的類別表示 XY 平面中的橢圓。

  • Polygon > - 由許多首尾相連的共面線段形成的封閉形狀稱為多邊形。 javafx.scene.Polygon 類別表示 XY 平面中的多邊形。

  • Polyline - 折線與多邊形相同,不同之處在於折線最終沒有關閉。或者,由一條或多條線段組成的連續線。 javafx.scene.Polyline 類別表示 XY 平面中的折線。

  • 三次曲線 strong> - 三次曲線是 XY 平面中的貝塞爾參數曲線,是 3 次曲線。 javafx.scene.CubicCurve 類別表示XY 中的三次曲線

  • ##QuadCurve - 二次曲線是XY 平面中的貝塞爾參數曲線,是2 次曲線。 javafx.scene.QuadCurve 類別表示 XY 平面中的四邊形曲線。

  • Arc - 弧是曲線的一部份。 javafx.scene.Arc 類別表示 XY 平面中的圓弧。

要建立所需的形狀,您需要需要 -

  • #實例化對應的類別。

  • 設定其屬性。

    li>
  • 將已建立的物件加入群組。

範例

以下JavaFX 範例示範了所有可用2D 形狀的建立−

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

import javafx.application.Application;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.stage.Stage;

import javafx.scene.shape.Arc;

import javafx.scene.shape.ArcType;

import javafx.scene.shape.Circle;

import javafx.scene.shape.CubicCurve;

import javafx.scene.shape.Ellipse;

import javafx.scene.shape.Line;

import javafx.scene.shape.Polygon;

import javafx.scene.shape.Polyline;

import javafx.scene.shape.QuadCurve;

import javafx.scene.shape.Rectangle;

import javafx.scene.text.Font;

import javafx.scene.text.FontPosture;

import javafx.scene.text.FontWeight;

import javafx.scene.text.Text;

public class JavaFXShapes extends Application {

   public void start(Stage stage) {

      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);

      Text cirText = new Text("Circle");

      cirText.setFont(font);

      cirText.setX(50);

      cirText.setY(130);

      Text rectText = new Text("Rectangle");

      rectText.setFont(font);

      rectText.setX(170);

      rectText.setY(130);

      Text ellipseText = new Text("Ellipse");

      ellipseText.setFont(font);

      ellipseText.setX(310);

      ellipseText.setY(130);

      Text polyText = new Text("Polygon");

      polyText.setFont(font);

      polyText.setX(425);

      polyText.setY(130);

      Text lineText = new Text("Line");

      lineText.setFont(font);

      lineText.setX(530);

      lineText.setY(130);

      Text polyLineText = new Text("Poly Line");

      polyLineText.setFont(font);

      polyLineText.setX(40);

      polyLineText.setY(260);

      Text cubicCurveText = new Text("Cubic Curve");

      cubicCurveText.setFont(font);

      cubicCurveText.setX(140);

      cubicCurveText.setY(260);

      Text quadCurveText = new Text("Quad Curve");

      quadCurveText.setFont(font);

      quadCurveText.setX(340);

      quadCurveText.setY(260);

      Text arcText = new Text("Arc");

      arcText.setFont(font);

      arcText.setX(490);

      arcText.setY(260);

      //Drawing a circle

      Circle circle = new Circle(75.0f, 65.0f, 40.0f );

      //Drawing a Rectangle

      Rectangle rect = new Rectangle(150, 30, 100, 65);

      //Drawing an ellipse

      Ellipse ellipse = new Ellipse(330, 60, 60, 35);

      //Drawing Polygon

      Polygon poly = new Polygon(410, 60, 430, 30, 470, 30, 490, 60, 470, 100, 430, 100 );

      //Drawing a Line

      Line line = new Line(540, 30, 540, 90);

      line.setStrokeWidth(5.0);

      //Drawing a Poly line

      Polyline polyLine = new Polyline(25, 210, 100, 210, 50, 180, 50, 230);

      polyLine.setStrokeWidth(5.0);

      //Drawing a cubic curve

      CubicCurve cubicCurve = new CubicCurve(150.0, 210.0, 200.0, 70.0, 200.0, 290.0, 270.0, 210.0);

      //Drawing Quadratic curve

      QuadCurve quadCurve = new QuadCurve(400.0, 200.0, 440.0, 250.0, 330.0, 170.0);

      //Drawing an arc

      Arc arc = new Arc(490, 240, 50, 80, 30, 70);

      arc.setType(ArcType.ROUND);

      //Setting the stage

      Group root = new Group(

      circle, ellipse, rect, poly, line,

      polyLine, cubicCurve, quadCurve, arc,

      cirText, rectText, ellipseText, polyText, lineText,

      polyLineText, cubicCurveText, quadCurveText, arcText);

      Scene scene = new Scene(root, 600, 300);

      stage.setTitle("2D shapes Example");

      stage.setScene(scene);

      stage.show();

   }

   public static void main(String args[]){

      launch(args);

   }

}

登入後複製

輸出

JavaFX提供的各種2D形狀有哪些?

以上是JavaFX提供的各種2D形狀有哪些?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板