The TextPainter java class
is intended for drawing a text across the single or multiple lines on the BufferedImage(s).
It can be very convenient for adding some text information to images, creation text-based watermarks etc.
TextPainter class in its basis uses Java 2D lays and supports some Java 2D API concepts such as Paint etc.
This java class actually represents a small superstructure above Java2D and uses only a small part of Java2D functionality.
TextPainter context contains the next attributes: font (instance of java.awt.Font),
foreground paint (instance of java.awt.Color, java.awt.GradientPaint or java.awt.TexturePaint),
backgroung color (instance of java.awt.Color),
outline paint (instance of java.awt.Color, java.awt.GradientPaint or java.awt.TexturePaint)
and antialiasing (on/off).
You can get and set these attributes using the corresponding constructors and getter/setter methods.
There are 2 main rendering methods (please consult TextPainter Java API for more options):
public BufferedImage renderString(String str);
public BufferedImage renderText(String text,
int width, boolean outline, boolean centralized);
The first method renders single string using the specified attributes from the TextPainter context.
Size of the returned BufferedImage object is equal to visual bounds of the specified string to render.
The second method renders line-wraped to the specified width text using the specified attributes from the TextPainter context.
Width of the returned BufferedImage object is equal to the specified width. Height is automatically extended to contain all text lines.
Below examples demostrate some TextPainter features (see examples/TextRendering,
examples/TextBanner, examples/ProductIcon1 and examples/ProductIcon2):
Hint: To render outlined text with better quality use BOLD font instances.
Hint: It is no secret that any published on a site text information
are not protected and can be easily grabbed by special scripts or even by copy/paste.
You can prevent the grabbing of any privacy information (passwords, e-mails, price lists,
financial reports etc.) by publishing such information as gif images.
With the help of the Gif4J PRO for Java such functionality can be added easily for short time.
Note: Sometimes you can get ugly rendered text on the other platform
while on your own platform text is rendered well.
Such problems are usually connected with failing one or another font.
You can get all supported fonts on the exact platform by calling getAllFonts()
method of the GraphicsEnvironment class:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font allfonts = ge.getAllFonts();