3 Replies Latest reply: Mar 31, 2009 12:07 PM by 843799 RSS

    TextLayout problem

      When I called TextLayout.draw repeatedly to draw texts on an image, only the first draw worked. The test code is shown below. The first field was drawn correctly (text + box). The other two fields only showed the bounding box but not the texts.

      When I replaced TextLayout.draw by Graphics2D.drawString method (the commented line in drawTextLayout, all three fields were drawn correctly (text + box).

      I am using JDK1.5 and JAI version 1.1.3.

      What have I missed?
      public class TestJAI {
           public static void main(String[] args) {
                Byte[] bandValues = new Byte[1];
                bandValues[0] = (byte)128;
                ParameterBlock params = new ParameterBlock();
                PlanarImage pi = JAI.create("constant", params);
                TiledImage ti = new TiledImage(pi, false);
                int x = 30;
                int y = 60;
                Font font = new Font("SanSerif", Font.BOLD, 18);
                // First field
                AttributedString as = new AttributedString("12345");
                as.addAttribute(TextAttribute.SIZE, 18f);
                as.addAttribute(TextAttribute.FOREGROUND, Color.white);
                drawTextLayout(as.getIterator(), ti.createGraphics(), x, y);
                // Second field
                as = new AttributedString("67890");
                as.addAttribute(TextAttribute.FONT, font);
                as.addAttribute(TextAttribute.FOREGROUND, Color.white);
                drawTextLayout(as.getIterator(), ti.createGraphics(), x+200, y);
                // Second field
                as = new AttributedString("11235813");
                as.addAttribute(TextAttribute.FONT, font);
                as.addAttribute(TextAttribute.FOREGROUND, Color.white);
                drawTextLayout(as.getIterator(), ti.createGraphics(), x, y+100);
                JAI.create("filestore", ti, "c:/dev/jaioutput.png", "PNG", null);
                JFrame frame = new JFrame();
                Container contentPane = frame.getContentPane();
                contentPane.setLayout(new BorderLayout());
                DisplayJAI dj = new DisplayJAI(ti);
                contentPane.add(new JScrollPane(dj), BorderLayout.CENTER);
                frame.setSize(360, 240);
           public static void drawTextLayout(AttributedCharacterIterator aci, Graphics2D g2d, float x, float y) {
                TextLayout layout = new TextLayout(aci, g2d.getFontRenderContext());
                layout.draw(g2d, x, y);
                //g2d.drawString(as.getIterator(), x, y);
                Rectangle2D bounds = layout.getBounds();
                bounds.setRect(bounds.getX()+x, bounds.getY()+y, bounds.getWidth(), bounds.getHeight());
        • 1. Re: TextLayout problem
          Any ideas please?

          I had the same problem when I tested LineBreakMeasurer following the sample code in the API doc. It displayed only the first two or three lines. I then modified the code to use Graphics2D.drawString(String, int, int) and all lines were displayed correctly.
          • 2. Re: TextLayout problem
            Nothing wrong with your code. The problem seems to be in combining TextLayout and a TiledImage . Drawing certain lengths of strings at certain positions causes text to be cut off as though there was an invisible border. And depending on where you place the string and how long it is, you can see a couple of these invisible borders. My only guess is that the tiling in the TiledImage is somehow interfering.
            • 3. Re: TextLayout problem
              I think you are right. I replaced TiledImage with BufferedImage and it was fine. Thanks.