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

SunGraphics2D.hitClip() can give wrong result for floating point scale

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • 9
    • 9
    • client-libs
    • None
    • 2d

      Run the following code to reproduce the issue:
      -------------------
      import java.awt.Graphics2D;
      import java.awt.Rectangle;
      import java.awt.image.BufferedImage;

      public class HitTest {

          public static void main(String[] args) {
              hitTest(1.0);
              hitTest(1.5);
          }

          static void hitTest(double scale) {
              BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);

              Graphics2D g = img.createGraphics();
              g.scale(scale, scale);
              Rectangle clip = new Rectangle(7, 6, 200, 200);
              Rectangle rect = new Rectangle(6, 6, 1, 200);
              g.setClip(clip);
              boolean hitClipTest = g.hitClip(rect.x, rect.y, rect.width, rect.height);
              g.dispose();
              if (hitClipTest) {
                  throw new RuntimeException(
                          String.format("Hit clip fails for scale: %f,"
                                  + "clip: %s, rect: %s", scale, clip, rect));
              }
          }
      }
      -------------------

      The result is:
      Exception in thread "main" java.lang.RuntimeException: Hit clip fails for scale: 1.500000,clip: java.awt.Rectangle[x=7,y=6,width=200,height=200], rect: java.awt.Rectangle[x=6,y=6,width=1,height=200]
      at java2d.HitTest.hitTest(HitTest.java:35)
      at java2d.HitTest.main(HitTest.java:20)

            alexsch Alexandr Scherbatiy
            alexsch Alexandr Scherbatiy
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: