0

I am Appending Text on images. How to specify the Line spacing between two lines while appending on the text on the image?

EDIT:- I want to Control Line Height of the text.

final BufferedImage image = ImageIO.read(new File(Background));
Graphics g = image.getGraphics();
java.awt.Font a=new java.awt.Font("AndaleWTJ", java.awt.Font.PLAIN, 26);
g.setFont(a);
g.setColor(Color.RED);
FontMetrics fm = g.getFontMetrics();
drawString(g,Title,15, 84,402,199,171,1);
g.dispose();

public static void drawString(Graphics g, String s, int x, int y, int width,int red_val,int green_val, int blue_val)
{     FontMetrics fm = g.getFontMetrics();
      java.awt.Color c= new java.awt.Color(red_val, green_val, blue_val);
      g.setColor(c);
      int lineHeight = fm.getHeight();
      int curX = x;
      int curY = y;
  String[] words = s.split(" ");
        for (String word : words)
        {
                // Find out thw width of the word.
                int wordWidth = fm.stringWidth(word + " ");

                // If text exceeds the width, then move to next line.
                if (curX + wordWidth >= x + width)
                {
                        curY += lineHeight;
                        curX = x;
                }
                g.drawString(word, curX, curY);
                // Move over to the right for next word.
                curX += wordWidth;
        }
}
Community
  • 1
  • 1
Code Hungry
  • 3,930
  • 22
  • 67
  • 95
  • 1
    ..use a bigger `y` offset for `drawString(..)`. If that does not answer the question, post an [SSCCE](http://sscce.org/). – Andrew Thompson Dec 13 '12 at 14:00
  • Post the code you are using to output the lines on your image now. It is easier to comment on how to fix it than explaining it without an example. – tucuxi Dec 13 '12 at 14:23

2 Answers2

2

Now I am Passing value for lineHeight instead of int lineHeight = fm.getHeight();

 public static void drawString(Graphics g, String s, int x, int y, int width,int red_val,int green_val, int blue_val, int lineHeight )
    {     FontMetrics fm = g.getFontMetrics();
          java.awt.Color c= new java.awt.Color(red_val, green_val, blue_val);
          g.setColor(c);
          int curX = x;
          int curY = y;
      String[] words = s.split(" ");
            for (String word : words)
            {
                    // Find out thw width of the word.
                    int wordWidth = fm.stringWidth(word + " ");

                    // If text exceeds the width, then move to next line.
                    if (curX + wordWidth >= x + width)
                    {
                            curY += lineHeight;
                            curX = x;
                    }
                    g.drawString(word, curX, curY);
                    // Move over to the right for next word.
                    curX += wordWidth;
            }
    }
Code Hungry
  • 3,930
  • 22
  • 67
  • 95
  • Is this supposed to be an answer, or an expansion of the question? If it is not an answer, please edit it into the question. – Andrew Thompson Dec 14 '12 at 07:44
  • @AndrewThompson :- This is answer. Now I can adjust lineHeight as per my requirement. Thanks for your valuable feedback – Code Hungry Dec 14 '12 at 08:25
1

If a bigger y as @Andrew Thompson post in comment isn't enough flexible for you. This is what you are looking for http://docs.oracle.com/javase/tutorial/2d/text/drawmulstring.html

Jan Krakora
  • 2,500
  • 3
  • 25
  • 52