SamyLouail SamyLouail - 1 year ago 179
Java Question

How do I create a triangle using JavaFX?

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

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

Answer Source

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


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

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 {
      public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 260, 80);

        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 });



      public static void main(String[] args) {

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download