2016-02-27 203 views
1

如何使用JavaFX創建三角形? 我已經嘗試過這些,但我不知道如何準確地解決它:如何使用JavaFX創建三角形?

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

回答

3

更換triangle.getPoints().setAll .. with 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

0

Javadoc for Polygon

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

看起來你缺少new Double[] {...}

+0

我剛纔添加它謝謝:)但是你能告訴我這是什麼這些值是指? – SamyLouail