2 Replies Latest reply: Mar 20, 2013 10:21 AM by gimbal2 RSS

    I think this is a bug with rendering a string to a graphics2d

    Penny 666

      The following lines of code create the rendering error I am experiencing, to which I have tested on several computers.

      BufferedImage bi=new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2d=bi.createGraphics();
      g2d.setColor(new Color(1f,0f,0f,0.5f));

      RenderingHints renderingHints=new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
      RenderingHints rh=new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      rh=new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION,RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

      g2d.setFont(new Font("Lucida",Font.BOLD,14));

      When the destination image has an alpha of 1 the text is rendered with the correct antialiasing. However, in this example where the alpha is 0.5 the text is rendered with unwanted artifacts.

      This image has JPG artifacts but the unwanted rendering artifacts are still visible (ie the brighter edges)

      I am wondering if this is not in fact an error but the intended result. If so, how to I configure the graphics object to render the antialiased text without the artifacts?

      I could not find where to submit a bug on the oracle website for the life of me so maybe if someone would like to explain how this is achieved I will be very grateful.

      I have spent several hours trying to find if this has been reported as a bug or if there is a solution posted online but to no avail.

      Edited by: 995038 on Mar 20, 2013 5:32 AM

      Edited by: 995038 on Mar 20, 2013 5:33 AM

      Edited by: 995038 on Mar 20, 2013 5:36 AM
        • 1. Re: I think this is a bug with rendering a string to a graphics2d
          Penny 666
          I have made a work around for the moment.

          This work around uses a BufferedImage to render the antialiased text first which does not create the artefacts and then draw the image of the text to where the text should appear. I dont think this is an acceptable solution because in the case where there could be 1000s of TextLayout objects the increased overhead would be unacceptable.

          private void drawTextLayout(Graphics2D g2d,float x,float _y) {
          Rectangle r=_g2d.getClipBounds();
          Composite c=bufferG2D.getComposite();

          the BufferedImage of bufferG2D would need to be large enough to cater for any size of text to be rendered to which I believe the size should be set to the screen size. This solution requires that there always be a _g2d clip assigned.

          • 2. Re: I think this is a bug with rendering a string to a graphics2d
            You should realize that you're using outdated tech that will have a very low priority to be fixed. Java2D is out, JavaFX 2 is in. If you want reliable results and an actual response to problems you may encounter, you should consider making the migration. That is already a consideration disregarding the bugs when you want to do heavy duty drawing operations; Java2D performance is unreliable at best.