5

Are there any nice libraries to render text in an image for Java?

Java has a 2d text library, http://java.sun.com/docs/books/tutorial/2d/text/index.html but not sure if theres a better library to use.

JavaRocky
  • 19,203
  • 31
  • 89
  • 110

2 Answers2

9

Here's a method to draw text on an image:

public void displayText(BufferedImage image, String text, Font font, int x, int y){
   Graphics2d g = image.createGraphics();
   g.setFont(font);
   g.drawString(text, x, y);
   g.dispose();
}
Jay Askren
  • 10,282
  • 14
  • 53
  • 75
4

It depends on what you want to do. Java2D is a fairly rich environment for text, as seen in the Fonts tab of the demo, located in the demo/jfc/Java2D folder of the Demos and Samples; source code for the font demos may be found in the enclosed src.zip. Building on this foundation, I've had good results using the text utilities in JCommon, now part of JFreeChart 1.5. As you are annotating images, a basic example is shown here, and you may want to look into using AlphaComposite, also previewed in the Java2D demo.

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045