JavaFX Polygon contains point

Description

JavaFX Polygon contains point

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
   @Override//from   www.j  ava  2  s  .  co  m
   public void start(Stage primaryStage) {

      // Create a pane
      Pane pane = new Pane();

      // Create a polygon
      Polygon polygon = new Polygon();
      polygon.setFill(Color.WHITE);
      polygon.setStroke(Color.BLACK);
      pane.getChildren().add(polygon);
      ObservableList<Double> list = polygon.getPoints();

      list.addAll(new Double(6), new Double(102));
      list.addAll(new Double(16), new Double(110));
      list.addAll(new Double(26), new Double(310));
      list.addAll(new Double(226), new Double(120));
      
  
      double x = 43;
      double y = 49;

      // Create a circle
      Circle circle = new Circle(x, y, 5);
      pane.getChildren().add(circle);

      // Create a Text
      Text text = new Text("        The point is " + 
         (polygon.contains(x, y) ? "" : "not ") + "inside the polygon  ");

      // Create a vbox
      VBox vbox = new VBox(5);
      vbox.getChildren().addAll(pane, text);

      // Create a Scene and place it in the stage
      Scene scene = new Scene(vbox);
      primaryStage.setTitle("Exercise_14_24");
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}



PreviousNext

Related