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
      Hiya,

      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));
      g2d.fillRect(0,0,100,100);

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

      g2d.setRenderingHints(renderingHints);
      g2d.setFont(new Font("Lucida",Font.BOLD,14));
      g2d.setColor(Color.BLACK);
      g2d.drawString("SQOWTGBC",10,30);

      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.

      [http://sphotos-b.ak.fbcdn.net/hphotos-ak-ash4/269344_590137484330481_704825569_n.jpg]
      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.
      :)
      Penny
      x

      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();
          bufferG2D.translate(r.x,r.y);
          Composite c=bufferG2D.getComposite();
          bufferG2D.setComposite(clearComposite);
          bufferG2D.fill(r);
          bufferG2D.setComposite(c);
          textLayout.draw(bufferG2D,_x,_y);
          bufferG2D.translate(-r.x,-r.y);
          _g2d.drawImage(buffer,r.x,r.y,null);
          }

          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.

          Regards
          Penny
          • 2. Re: I think this is a bug with rendering a string to a graphics2d
            gimbal2
            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.