如何使用 JavaFX 创建三角形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35674997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:59:30  来源:igfitidea点击:

How do I create a triangle using JavaFX?

javauser-interfacejavafxjavafx-8polygon

提问by Malik

How can I create a triangle using JavaFX? I have already tried these but I don't know how to fix it exactly :

如何使用 JavaFX 创建三角形?我已经尝试过这些,但我不知道如何准确修复它:

Polygon triangle = new Polygon();
triangle.getPoints().setAll(
   50, 50,
   60, 60,
   20, 40
);

采纳答案by ΦXoc? ? Пepeúpa ツ

Replace triangle.getPoints().setAll.. with triangle.getPoints().addAll(

triangle.getPoints().setAll..替换 为triangle.getPoints().addAll(

Explanation:

解释:

You are adding 3 points, the x0=50,y0=50 then the x0=60,y0=60 and then x0=20,y0=40, those are the vertices of the triangle...

您要添加 3 个点,x0=50,y0=50 然后 x0=60,y0=60 然后 x0=20,y0=40,这些是三角形的顶点...

this represents a triangle like this (Be careful of not plotting a line or a weird figure)

这代表一个像这样的三角形(小心不要绘制一条线或一个奇怪的图形)

enter image description here

在此处输入图片说明

The following snippet will generate a polygon like the image below.

以下代码段将生成如下图所示的多边形。

public class Main extends Application {
      @Override
      public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 260, 80);
        stage.setScene(scene);

        Group g = new Group();

        Polygon polygon = new Polygon();
        polygon.getPoints().addAll(new Double[]{
            0.0, 0.0,
            20.0, 10.0,
            10.0, 20.0 });

        g.getChildren().add(polygon);

        scene.setRoot(g);
        stage.show();
      }

      public static void main(String[] args) {
        launch(args);
      }
    }

enter image description here

在此处输入图片说明

回答by iboisver

From the Javadoc for Polygon:

来自PolygonJavadoc

Polygon polygon = new Polygon();
polygon.getPoints().addAll(new Double[]{
    0.0, 0.0,
    20.0, 10.0,
    10.0, 20.0 });

It looks like you are missing the new Double[] {...}

看起来你错过了 new Double[] {...}