Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8296375

Bounds of Polygon shape are overgrown

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Tested on Windows 11 with JDK 17.0.2 and JavaFX 17.0.2

      A DESCRIPTION OF THE PROBLEM :
      The calculated bounds of javafx.scene.shape.Polygon can be extremely overgrown. The issue appears when the shape is scaled up by a large value. After digging down in the JavaFX code, I believe the issue comes from an arbitrary augmentation of the shape bounding box here:
      https://github.com/openjdk/jfx/blob/master/modules/javafx.graphics/src/main/java/javafx/scene/shape/Shape.java#L1124-L1127
      It seems that the "0.5" augmentation assumes that no scale is applied to the shape.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The example source code can be run to demonstrate the issue. The example shows in a 2D view, a blue polygon, surrounded by a black rectangle that represents the computed bounds.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The bounds should be the tightest bounding box, i.e. touching the polygon left vertices.
      ACTUAL -
      The bounds is drastically bigger than the polygon.

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.scene.Group;
      import javafx.scene.Scene;
      import javafx.scene.layout.StackPane;
      import javafx.scene.paint.Color;
      import javafx.scene.shape.Polygon;
      import javafx.scene.shape.Rectangle;
      import javafx.scene.transform.Scale;
      import javafx.stage.Stage;

      public class PolygonBoundsApp extends Application
      {
         @Override
         public void start(Stage stage) throws Exception
         {
            StackPane root = new StackPane();
            Scene sc = new Scene(root, 600, 600);
            stage.setScene(sc);
            stage.show();

            int scale = 150;

            Polygon polygon = new Polygon(-0.1, -0.1, 0.1, -0.08, 0.1, 0.08, -0.1, 0.1);
            polygon.strokeProperty().set(Color.CADETBLUE);
            polygon.setStrokeWidth(1.5 / scale);
            polygon.fillProperty().set(null);

            Rectangle polygonBounds = new Rectangle();
            polygonBounds.setX(polygon.getBoundsInLocal().getMinX());
            polygonBounds.setY(polygon.getBoundsInLocal().getMinY());
            polygonBounds.setWidth(polygon.getBoundsInLocal().getWidth());
            polygonBounds.setHeight(polygon.getBoundsInLocal().getHeight());
            polygonBounds.strokeProperty().set(Color.BLACK);
            polygonBounds.setStrokeWidth(1.5 / scale);
            polygonBounds.fillProperty().set(null);

            Group group = new Group();
            group.getTransforms().add(new Scale(scale, scale));
            group.getChildren().add(polygon);
            group.getChildren().add(polygonBounds);
            root.getChildren().add(group);
         }

         public static void main(String[] args)
         {
            launch(args);
         }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I have not found a workaround for this issue at the moment.

      FREQUENCY : always


        1. Capture.PNG
          9 kB
          Praveen Narayanaswamy
        2. PolygonBoundsApp.java
          2 kB
          Praveen Narayanaswamy

            kcr Kevin Rushforth
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: