如何使用JavaFX创建三角形?

时间:2016-02-27 20:22:12

标签: java user-interface javafx javafx-8 polygon

如何使用JavaFX创建三角形? 我已经尝试了这些,但我不知道如何解决它:

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

2 个答案:

答案 0 :(得分:5)

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

说明:

你是加3点,x0 = 50,y0 = 50然后x0 = 60,y0 = 60然后x0 = 20,y0 = 40,那些是三角形的顶点......

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

enter image description here

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

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

答案 1 :(得分:0)

来自Javadoc for Polygon

config.vm.provision :chef_solo do |chef|
  chef.roles_path = "chef/roles"
  chef.cookbooks_path = ["chef/site-cookbooks", "chef/cookbooks"]
  chef.add_role "my-fancy-role"
end

看起来你错过了Polygon polygon = new Polygon(); polygon.getPoints().addAll(new Double[]{ 0.0, 0.0, 20.0, 10.0, 10.0, 20.0 });