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

Graphics2D.drawString doesn't always work with Font derived from AffineTransform

XMLWordPrintable

    • 2d
    • b20
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      When I use an AffineTransform which includes both rotation and scaling to derive a new Font instance, and then use that Font instance to draw text to a Graphics2D, the text does not draw. It seems like I can get the text to draw if I reduce the scale, though (e.g. from 12 to 10).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a Font, derive a new Font using a rotated + scaled AffineTransform, draw text on a Graphics2D using that derived Font.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The text is drawn to the graphics.
      ACTUAL -
      The text is not drawn to the graphics.

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

      import javax.imageio.ImageIO;

      /**
       * The "TEST 2" text does not draw. NOTE: Reducing the AffineTransform scale
       * from 12 to 10 seems to allow it to draw?!?
       */
      public class DrawStringTest {

          public static void main(String[] args) throws Exception {

              AffineTransform at = AffineTransform.getRotateInstance(3 * Math.PI / 2);
              at.scale(12, 12);

              Font font1 = new Font("SansSerif", Font.PLAIN, 10);
              Font font2 = font1.deriveFont(at);

              BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_BYTE_GRAY);
              Graphics2D g2d = image.createGraphics();
              g2d.setColor(Color.WHITE);
              g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
              g2d.setColor(Color.BLACK);
              g2d.setFont(font1);
              g2d.drawString("TEST 1", 200, 400); // draws text
              g2d.setFont(font2);
              g2d.drawString("TEST 2", 200, 400); // does not draw text
              g2d.dispose();

              ImageIO.write(image, "png", new File("test.png"));
          }

      }

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

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: