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

Graphics2D.drawLine() does not work with simple transform and clip from JDK 9 and onward

XMLWordPrintable

    • 2d
    • 9
    • x86_64
    • windows_10

      ADDITIONAL SYSTEM INFORMATION :
      Running on Windows 10, Java 10.0.1.

      A DESCRIPTION OF THE PROBLEM :
      In some cases, a simple transform and clip region will cause the drawLine() method in Graphics2D not to work at all.

      The Graphics2D is created from a BufferedImage.createGraphics() call, the image type is TYPE_INT_RGB.

      This works fine on earlier versions of Java up to 1.8.0, but it is broken in Java 9 and Java 10.

      REGRESSION : Last worked in version 8u172

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a new BufferedImage of type TYPE_INT_RGB. Create a Graphics2D object to draw onto the image by calling BufferedImage.createGraphics().

      Fill the image with a white background, then apply a simple transform and clip.

      Set the color to black and call the drawLine() method in Graphics2D.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The line should appear at the given location on the image.
      ACTUAL -
      No line.

      The line shows up in Java 6, Java 7, Java 8, but not in Java 9.0.4 and not in Java 10.0.1.

      ---------- BEGIN SOURCE ----------
      package test;

      import java.awt.BasicStroke;
      import java.awt.Color;
      import java.awt.Graphics2D;
      import java.awt.geom.AffineTransform;
      import java.awt.geom.Rectangle2D;
      import java.awt.image.BufferedImage;
      import java.io.File;

      import javax.imageio.ImageIO;

      public class LineBug
      {
      public static void main(String [] args)
      {
      try
      {
      // Create image and get Graphics2D
      BufferedImage bi = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2 = bi.createGraphics();

      // Fill white and set basic properties
      g2.setColor(Color.white);
      g2.fillRect(0, 0, 1000, 1000);
      g2.setColor(Color.black);
      g2.setStroke(new BasicStroke(1));

      // Set transform and clip
      g2.setTransform(new AffineTransform(0.5, 0, 0, 0.5, 100, 100));
      g2.setClip(new Rectangle2D.Double(-1, 0, 2, 386));

      // Draw the line
      g2.drawLine(0, 0, 0, 385);

      // Write the output image
      ImageIO.write(bi, "png", new File("linebug.png"));
      }
      catch(Throwable t)
      {
      t.printStackTrace();
      }
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      No workaround found yet.

      FREQUENCY : always


        1. linebug11.png
          linebug11.png
          5 kB
        2. LineBug.java
          1.0 kB
        3. linebug_9.png
          linebug_9.png
          5 kB
        4. linebug_8u171.png
          linebug_8u171.png
          5 kB

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

              Created:
              Updated: