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

Method Shape.getBounds2D() incorrectly includes Bezier control points in bounding box

    XMLWordPrintable

Details

    • 2d
    • b21
    • x86
    • os_x

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      10.12.3 (16D32)

      A DESCRIPTION OF THE PROBLEM :
      When working with cubic and quadratic Bezier curves, the method Shape.getBounds2D() incorrectly includes Bezier control points in the bounding box computed. This can result in bounding box coordinates that are wildly outside the region occupied by the shape drawn by the curve.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run included test code. Note that the bounding box that's drawn around the Shape includes the Bezier control points used to define the Shape even though these points lie well outside the bounds of the Shape. The sample program demonstrates a smaller error than is possible, as Bezier control points can be located well away from the path being drawn.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect to get coordinates for a bounding box that's reasonably close to the Shape.
      ACTUAL -
      Perhaps I'm expecting too much but the documentation says that xx should return "a high precision and more accurate bounding box of the Shape than the getBounds method." The result I get does not seem very precise.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No crashes experienced.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.*;
      import java.awt.geom.Path2D;
      import java.awt.geom.Rectangle2D;

      /**
       * Test code to demonstrate how Bezier control points are incorrectly included in the
       * bounding box calculated by the method getBounds2D()
       */

      class BoundingTest extends JFrame {
        private transient Image offScr;
        private Dimension lastDim;

        BoundingTest () {
          setSize(200, 260);
          setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          setVisible(true);
        }

        public void paint (Graphics g) {
          Dimension d = getSize();
          if (offScr == null || (lastDim != null && (d.width != lastDim.width || d.height != lastDim.height)))
            offScr = createImage(d.width, d.height);
          lastDim = d;
          Graphics2D g2 = (Graphics2D) offScr.getGraphics();
          g2.setBackground(getBackground());
          g2.clearRect(0, 0, d.width, d.height);
          g2.setColor(Color.black);
          // Draw a cubic Bezier shape using Path2D.Double
          Path2D.Double path = new Path2D.Double();
          path.moveTo(40, 140);
          path.curveTo(40, 60, 160, 60, 160, 140);
          path.curveTo(160, 220, 40, 220, 40, 140);
          path.closePath();
          g2.draw(path);
          // Draw the bounding box computed for the path by getBounds2D()
          Rectangle2D bounds = path.getBounds2D();
          int x = (int) bounds.getMinX();
          int y = (int) bounds.getMinY();
          int wid = (int) bounds.getWidth();
          int hyt = (int) bounds.getHeight();
          g2.setColor(Color.blue);
          g2.drawRect(x, y, wid, hyt);
          // Draw the location of Bezier control points as small circles
          // Control points are: 40,60 - 160,60 - 160,220 - 40,220
          g2.setColor(Color.red);
          g2.drawOval(40-2, 60-3, 5, 5);
          g2.drawOval(160-2, 60-3, 5, 5);
          g2.drawOval(160-2, 220-3, 5, 5);
          g2.drawOval(40-2, 220-3, 5, 5);
          g.drawImage(offScr, 0, 0, this);
        }

        public static void main (String[] args) throws Exception {
          new BoundingTest();
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Still working to find one.

      Attachments

        Issue Links

          Activity

            People

              prr Philip Race
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: