title: Java equivalent of some GD library calls
body: <p>There is a web site developed entirely in PHP with GD library that mainly deals with printing text over a random image.</p>
  
  <p>The text has to be printed in any of about 100 prescribed TTF fonts (residing in a directory near the .php files) in a dozen of prescribed colors and there is a requirement to specify location of the text according to any of several prescribed algorithms.</p>
  
  <p>This is solved by using imagettfbox() which returns geometry of the text, which is then mapped onto the image using imagettftext().</p>
  
  <p>What can I use in Java to achieve the same functionality with servlets/beans?
  I can put the TTF fonts anywhere I have to. Registering them in Windows is undesirable, but if that's the only option we'd go for it (currently they are not).</p>
  
  <p>@LexLythius:</p>
  
  <p>From your link and some other resources I pieced together a working solution to drawing text over graphics:</p>
  
  <pre><code>// file is pointed at the image
  BufferedImage loadedImage = ImageIO.read(file);
  // file is pointed at the TTF font
  Font font = Font.createFont(Font.TRUETYPE_FONT, file);
  Font drawFont = font.deriveFont(20); // use actual size in real life
  
  Graphics graphics = loadedImage.getGraphics();        
  FontMetrics metrics = graphics.getFontMetrics(drawFont);
  Dimension size = new Dimension(metrics.getHeight(), metrics.stringWidth("Hello, world!"));
  
  graphics.setFont(drawFont);
  graphics.setColor(new Color(128, 128, 128); // use actual RGB values in real life
  
  graphics.drawString("Hello, world!", 10,10); // use Dimension to calculate position in real life
  </code></pre>
  
  <p>What is remaining is displaying that image in a servlet and deciding what functionality goes where - to a servlet or bean.</p>