com.gif4j
Class TextPainter

java.lang.Object
  extended bycom.gif4j.TextPainter

public class TextPainter
extends java.lang.Object

This java class is intended for drawing the 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 (Watermark) etc.
TextPainter class in its basis uses Java 2D lays and supports some Java 2D API concepts such as Paint etc. This 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)
  • antialiasing (on/off)
  • You can get and set these attributes using the constructors and suite of getter/setter methods.

    Version:
    1.0
    Author:
    Gif4J Software - Java GIF image processing solutions
    See Also:
    BufferedImage

    Constructor Summary
    TextPainter(java.awt.Font font)
              Constructs TextPainter object with the specified Font and set by default foreground Paint attribute to Color.BLACK, outline Paint attribute to Color.WHITE, background Color attribute to full-TRANSPARENT_COLOR Color and anti-aliased to 'TRUE'.
    TextPainter(java.awt.Font font, boolean antialiased)
              Constructs TextPainter object with the specified Font and the specified anti-aliased attribute.
    TextPainter(java.awt.Font font, java.awt.Paint foregroundPaint, java.awt.Paint outlinePaint, java.awt.Color backgroundColor, boolean antialiased)
              Constructs TextPainter object with the specified Font attribute, foreground Paint attribute, outline Paint attribute, background Color attribute and anti-aliased attribute.
     
    Method Summary
     java.awt.Color getBackgroundColor()
              Returns the current background Color of the TextPainter context.
     java.awt.Font getFont()
              Gets the current font.
     java.awt.Paint getForegroundPaint()
              Returns the current foreground Paint of the TextPainter context.
     java.awt.Paint getOutlinePaint()
              Returns the current outline Paint of the TextPainter context.
     boolean isAntialiased()
              Gets the current antialiased attribute
     java.awt.image.BufferedImage renderString(java.lang.String str)
              Renders single string using the specified attributes from the TextPainter context.
     java.awt.image.BufferedImage renderString(java.lang.String str, boolean outline)
              Renders single string using the specified attributes from the TextPainter context.
     java.awt.image.BufferedImage renderText(java.lang.String text, int width, boolean outline, boolean centralized)
              Renders line-wraped to the specified width text using the specified attributes from the TextPainter context.
     void setAntialiased(boolean antialiased)
              Sets this painter context's antialiased attribute to the specified value.
     void setBackgroundColor(java.awt.Color backgroundColor)
              Sets the background Color attribute for the TextPainter context.
     void setFont(java.awt.Font font)
              Sets this painter context's font to the specified font.
     void setForegroundPaint(java.awt.Paint foregroundPaint)
              Sets the foreground Paint attribute for the TextPainter context.
     void setOutlinePaint(java.awt.Paint outlinePaint)
              Sets the outline Paint attribute for the TextPainter context.
     
    Methods inherited from class java.lang.Object
    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    TextPainter

    public TextPainter(java.awt.Font font)
    Constructs TextPainter object with the specified Font and set by default foreground Paint attribute to Color.BLACK, outline Paint attribute to Color.WHITE, background Color attribute to full-TRANSPARENT_COLOR Color and anti-aliased to 'TRUE'.

    Parameters:
    font - this painter context's font
    Throws:
    java.lang.NullPointerException - If font is null
    See Also:
    Font

    TextPainter

    public TextPainter(java.awt.Font font,
                       boolean antialiased)
    Constructs TextPainter object with the specified Font and the specified anti-aliased attribute. Set by default foreground Paint attribute to Color.BLACK, outline Paint attribute to Color.WHITE, background Color attribute to full-TRANSPARENT_COLOR Color.

    Parameters:
    font - this painter context's font
    antialiased - if true then render anti-aliased text
    Throws:
    java.lang.NullPointerException - If font is null
    See Also:
    Font

    TextPainter

    public TextPainter(java.awt.Font font,
                       java.awt.Paint foregroundPaint,
                       java.awt.Paint outlinePaint,
                       java.awt.Color backgroundColor,
                       boolean antialiased)
    Constructs TextPainter object with the specified Font attribute, foreground Paint attribute, outline Paint attribute, background Color attribute and anti-aliased attribute.

    Parameters:
    font - this painter context's font
    foregroundPaint - the Paint object to be used to generate color or pattern during the text rendering process, or null
    outlinePaint - the Paint object to be used to generate color or pattern during the outline rendering process, or null
    backgroundColor - the Color object to be used to generate color during the background rendering process, or null
    antialiased - if true then render anti-aliased text
    Throws:
    java.lang.NullPointerException - If font is null
    See Also:
    Font, Paint
    Method Detail

    getBackgroundColor

    public java.awt.Color getBackgroundColor()
    Returns the current background Color of the TextPainter context.

    Returns:
    the current background Color.
    See Also:
    Color

    getFont

    public java.awt.Font getFont()
    Gets the current font.

    Returns:
    this painter context's Font

    getForegroundPaint

    public java.awt.Paint getForegroundPaint()
    Returns the current foreground Paint of the TextPainter context.

    Returns:
    the current foreground Paint, which defines a color or pattern.
    See Also:
    Paint

    getOutlinePaint

    public java.awt.Paint getOutlinePaint()
    Returns the current outline Paint of the TextPainter context.

    Returns:
    the current outline Paint, which defines a color or pattern.
    See Also:
    Paint

    isAntialiased

    public boolean isAntialiased()
    Gets the current antialiased attribute

    Returns:
    the current antialiased attribute

    renderString

    public java.awt.image.BufferedImage renderString(java.lang.String str)
    Renders single string using the specified attributes from the TextPainter context. Outline Paint attribute is ignored. Size of the returned BufferedImage object is equal to visual bounds of the rendered string.

    Parameters:
    str - string to render
    Returns:
    BufferedImage object which contains the specified rendered string
    Throws:
    java.lang.NullPointerException - If string to render is null
    See Also:
    setFont(java.awt.Font), setForegroundPaint(java.awt.Paint), setAntialiased(boolean), setBackgroundColor(java.awt.Color)

    renderString

    public java.awt.image.BufferedImage renderString(java.lang.String str,
                                                     boolean outline)
    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.

    Parameters:
    str - string to render
    outline - if true then render the string outlined
    Returns:
    BufferedImage object which contains the specified rendered string
    Throws:
    java.lang.NullPointerException - If string to render is null
    See Also:
    setFont(java.awt.Font), setForegroundPaint(java.awt.Paint), setOutlinePaint(java.awt.Paint), setAntialiased(boolean), setBackgroundColor(java.awt.Color)

    renderText

    public java.awt.image.BufferedImage renderText(java.lang.String text,
                                                   int width,
                                                   boolean outline,
                                                   boolean centralized)
    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.

    Parameters:
    text - text to render
    width - the specified width to line-wrap text
    outline - if true then render the string outlined
    centralized - if true then the specified text is centralized
    Returns:
    BufferedImage object which contains the specified rendered text
    Throws:
    java.lang.NullPointerException - If text to render is null
    See Also:
    setFont(java.awt.Font), setForegroundPaint(java.awt.Paint), setOutlinePaint(java.awt.Paint), setAntialiased(boolean), setBackgroundColor(java.awt.Color)

    setAntialiased

    public void setAntialiased(boolean antialiased)
    Sets this painter context's antialiased attribute to the specified value.

    Parameters:
    antialiased -

    setBackgroundColor

    public void setBackgroundColor(java.awt.Color backgroundColor)
    Sets the background Color attribute for the TextPainter context. Calling this method with a null Color object does not have any effect on the current background Color attribute of this TextPainter.

    Parameters:
    backgroundColor - the Color object to be used to generate color during the rendering process, or null
    See Also:
    getBackgroundColor(), Color

    setFont

    public void setFont(java.awt.Font font)
    Sets this painter context's font to the specified font.

    Parameters:
    font - Font to render
    See Also:
    Font

    setForegroundPaint

    public void setForegroundPaint(java.awt.Paint foregroundPaint)
    Sets the foreground Paint attribute for the TextPainter context. Calling this method with a null Paint object does not have any effect on the current foreground Paint attribute of this TextPainter.

    Parameters:
    foregroundPaint - the Paint object to be used to generate color during the rendering process, or null
    See Also:
    getForegroundPaint(), Paint

    setOutlinePaint

    public void setOutlinePaint(java.awt.Paint outlinePaint)
    Sets the outline Paint attribute for the TextPainter context. Calling this method with a null Paint object does not have any effect on the current outline Paint attribute of this TextPainter.

    Parameters:
    outlinePaint - the Paint object to be used to generate color during the rendering process, or null
    See Also:
    getOutlinePaint(), Paint