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

Incorrect clipping when drawing to Graphics2D

    XMLWordPrintable

    Details

    • Subcomponent:
      2d
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10 Pro, Java 11.0.11

      But, the problem seems to happen on all versions of Java starting from Java 9 to Java 16.

      A DESCRIPTION OF THE PROBLEM :
      When some combinations of clipping regions and transforms are used in a Graphics2D object, incorrect clipping of drawing commands can occur.

      This can happen even when using simple transforms and using rectangles to clip and draw to Graphics2D. Given some values for these elements, it can happen that parts of the rectangle drawn are clipped incorrectly (i.e. missing whole sides of the rectangle on output).


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a BufferedImage and create a Graphics2D object from it.
      Set a transform such as [0.2 0 0 0.2 20 20}
      Set the clipping region to a rectangular region, larger than the rectangle to draw, i.e. [0, 0, 162, 147]
      Set a color to draw with, i.e. red
      Draw a rectangle within that region, i.e. [0, 0, 161, 146]
      Save the image

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      An image with a black background and a red rectangle
      ACTUAL -
      An image with a black background and an incomplete red rectangle. With the values above, the rectangle is missing the right and bottom edges. With other values, different edges of the rectangle can be missing.

      ---------- BEGIN SOURCE ----------
      import java.awt.Color;
      import java.awt.Graphics2D;
      import java.awt.geom.AffineTransform;
      import java.awt.image.BufferedImage;
      import java.io.File;

      import javax.imageio.ImageIO;

      public class ClipBug
      {
      private final static int IMG_WIDTH = 500;
      private final static int IMG_HEIGHT = 500;

      public static void main(String [] args) throws Exception
      {
      BufferedImage bi = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2 = bi.createGraphics();

      g2.setColor(Color.red);
      g2.transform(new AffineTransform(0.2, 0, 0, 0.2, 20, 20));

      g2.setClip(0, 0, 162, 147);
      g2.drawRect(0, 0, 161, 146);

      ImageIO.write(bi, "png", new File("c:/tmp/output.png"));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround found.

      FREQUENCY : always


        Attachments

        1. ClipBug.java
          0.7 kB
          Praveen Narayanaswamy
        2. output.png
          0.9 kB
          Praveen Narayanaswamy

          Activity

            People

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

              Dates

              Created:
              Updated: