Java Code Examples for javafx.scene.shape.Polygon#getPoints()

The following examples show how to use javafx.scene.shape.Polygon#getPoints() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Helper.java    From OEE-Designer with MIT License 6 votes vote down vote up
public static final boolean isInPolygon(final double X, final double Y, final Polygon POLYGON) {
	List<Double> points = POLYGON.getPoints();
	int noOfPointsInPolygon = POLYGON.getPoints().size() / 2;
	double[] pointsX = new double[noOfPointsInPolygon];
	double[] pointsY = new double[noOfPointsInPolygon];
	int pointCounter = 0;
	for (int i = 0; i < points.size(); i++) {
		if (i % 2 == 0) {
			pointsX[i] = points.get(pointCounter);
		} else {
			pointsY[i] = points.get(pointCounter);
			pointCounter++;
		}
	}
	return isInPolygon(X, Y, noOfPointsInPolygon, pointsX, pointsY);
}
 
Example 2
Source File: Helper.java    From charts with Apache License 2.0 6 votes vote down vote up
public static final boolean isInPolygon(final double X, final double Y, final Polygon POLYGON) {
    List<Double> points              = POLYGON.getPoints();
    int          noOfPointsInPolygon = POLYGON.getPoints().size() / 2;
    double[]     pointsX             = new double[noOfPointsInPolygon];
    double[]     pointsY             = new double[noOfPointsInPolygon];
    int          pointCounter        = 0;
    for (int i = 0 ; i < points.size() ; i++) {
        if (i % 2 == 0) {
            pointsX[i] = points.get(pointCounter);
        } else {
            pointsY[i] = points.get(pointCounter);
            pointCounter++;
        }
    }
    return isInPolygon(X, Y, noOfPointsInPolygon, pointsX, pointsY);
}
 
Example 3
Source File: Helper.java    From tilesfx with Apache License 2.0 5 votes vote down vote up
public static final boolean isInPolygon(final double X, final double Y, final Polygon POLYGON) {
    List<Double> points              = POLYGON.getPoints();
    int          noOfPointsInPolygon = POLYGON.getPoints().size() / 2;
    double[]     pointsX             = new double[noOfPointsInPolygon];
    double[]     pointsY             = new double[noOfPointsInPolygon];
    int          pointCounter        = 0;

    for (int i = 0, size = points.size() ; i < size - 1 ; i += 2) {
        pointsX[pointCounter] = points.get(i);
        pointsY[pointCounter] = points.get(i + 1);
        pointCounter++;
    }
    return isInPolygon(X, Y, noOfPointsInPolygon, pointsX, pointsY);
}
 
Example 4
Source File: DotArrowShapeDecorations.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private static void setSide(Polygon polygon, String side) {
	// setting the side of a polygon based shape to left/right means to use
	// 0.0 instead of the negative/positive y coordinates
	ObservableList<Double> points = polygon.getPoints();
	for (int i = 1; i < points.size(); i += 2) {
		double yCoordinate = points.get(i);
		if (yCoordinate < 0 && side.equals("l") //$NON-NLS-1$
				|| yCoordinate > 0 && side.equals("r")) { //$NON-NLS-1$
			points.remove(i);
			points.add(i, 0.0);
		}
	}
}
 
Example 5
Source File: Exercise_15_20.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
/** Create a polygon */
private void drawTriangle(Pane pane, ArrayList<Circle> p) {
	Polygon polygon = new Polygon();
	pane.getChildren().add(polygon);
	ObservableList<Double> points = polygon.getPoints();
	for (int i = 0; i < p.size(); i++) {
		points.add(p.get(i).getCenterX());
		points.add(p.get(i).getCenterY());
	}
	polygon.setFill(Color.WHITE);
	polygon.setStroke(Color.BLACK);
}
 
Example 6
Source File: Exercise_15_14.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
	// Create a pane
	Pane pane = new Pane();

	// Create a polygon and set it properties
	Polygon polygon = new Polygon();
	pane.getChildren().add(polygon);
	ObservableList<Double> list = polygon.getPoints();
	list.addAll(40.0, 20.0, 70.0, 40.0, 60.0, 80.0, 45.0, 45.0, 20.0, 60.0);
	polygon.setFill(Color.WHITE);
	polygon.setStroke(Color.BLACK);

	// Create and register the handle
	pane.setOnMouseMoved(e -> {
		pane.getChildren().clear();
		Text text = new Text(e.getX(), e.getY(), "Mouse point is " +
			(polygon.contains(e.getX(), e.getY()) ? "inside " : "outside ") +
			"the polygon");
		pane.getChildren().addAll(polygon, text);
	});

	// Create a scene and place it in the stage
	Scene scene = new Scene(pane, 300, 150);
	primaryStage.setTitle("Exercise_15_14"); // Set the stage title
	primaryStage.setScene(scene); // Place the scene in the stage
	primaryStage.show(); // Display the stage
}
 
Example 7
Source File: Exercise_14_15.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
	// Create a stack pane
	StackPane stackPane = new StackPane();

	// Create a polygon and set its properties
	Polygon polygon = new Polygon();
	stackPane.getChildren().add(polygon);
	polygon.setFill(Color.RED);
	polygon.setRotate(20);
	ObservableList<Double> list = polygon.getPoints();

	final double WIDTH = 200, HEIGHT = 200;
	double centerX = WIDTH / 2, centerY = HEIGHT / 2;
	double radius = Math.min(WIDTH, HEIGHT) * 0.4;

	// Add points to polygon list
	for (int i = 0; i < 8; i++) {
	 	list.add(centerX + radius * Math.cos(2 * i * Math.PI / 8));
	 	list.add(centerY - radius * Math.sin(2 * i * Math.PI / 8));
	}

	// Create a text and set its properties
	Text text = new Text("STOP");
	text.setFont(Font.font("Times new Roman", FontWeight.BOLD, 40));
	text.setFill(Color.WHITE);
	stackPane.getChildren().add(text);

	// Create a scene and place it in the stage
	Scene scene = new Scene(stackPane, WIDTH, HEIGHT);
	primaryStage.setTitle("Exercise_14_15"); // Set the stage title
	primaryStage.setScene(scene); // Place the scene in the stage
	primaryStage.show(); // Display the stage
}
 
Example 8
Source File: ShapeConverter.java    From Enzo with Apache License 2.0 5 votes vote down vote up
public static String convertPolygon(final Polygon POLYGON) {
    final StringBuilder fxPath = new StringBuilder();
    final int           size   = POLYGON.getPoints().size();
    if (size % 2 == 0) {
        List<Double> coordinates     = POLYGON.getPoints();
        for (int i = 0 ; i < size ; i += 2) {
            fxPath.append(i == 0 ? "M " : "L ")
                  .append(coordinates.get(i)).append(" ").append(coordinates.get(i + 1)).append(" ");
        }
        fxPath.append("Z");
    }
    return fxPath.toString();
}
 
Example 9
Source File: Exercise_14_24.java    From Intro-to-Java-Programming with MIT License 4 votes vote down vote up
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
	// Create a scanner
	Scanner input = new Scanner(System.in);

	// 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();

	// Prompt the user to enter the coordinates of five points
	System.out.print("Enter five points: ");
	for (int i = 0; i < 8; i++) {
		list.add(input.nextDouble());
	}
	double x = input.nextDouble();
	double y = input.nextDouble();

	// 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"); // Set the stage title
	primaryStage.setScene(scene); // Place the scene in the stage
	primaryStage.show(); // Display the stage
}
 
Example 10
Source File: Exercise_14_25.java    From Intro-to-Java-Programming with MIT License 4 votes vote down vote up
public void start(Stage primaryStage) {
	// Create a pane
	Pane pane = new Pane();
	pane.setPadding(new Insets(10, 10, 10, 10));

	// Create a circle
	Circle circle = new Circle(60, 60, 40);
	circle.setFill(Color.WHITE);
	circle.setStroke(Color.BLACK);
	pane.getChildren().addAll(circle);

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

	// Generate random angles in radians between 0 and 2PI
	ArrayList<Double> angles = new ArrayList<>();
	for (int i = 0; angles.size() < 5; i++) {
		double angle = (Math.random() * (2 * Math.PI));
		if (!angles.contains(angle)) {
			angles.add(angle);
		}
	}

	// Sort angles clockwise
	java.util.Collections.sort(angles);

	// Get 5 points on the circle
	for (int i = 0; i < angles.size(); i++) {
		list.add(circle.getCenterX() + circle.getRadius() * 
			Math.cos(angles.get(i)));
		list.add(circle.getCenterY() - circle.getRadius() * 
			Math.sin(angles.get(i)));
	}

	// Create a scene and place it in the stage
	Scene scene = new Scene(pane);
	primaryStage.setTitle("Exercise_14_25"); // Set the stage title
	primaryStage.setScene(scene); // Place the scene in the stage
	primaryStage.show(); // Display the stage 
}