Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

echopoint.image
Class TextImageReference  view TextImageReference download TextImageReference.java

java.lang.Object
  extended bynextapp.echo.AbstractImageReference
      extended bynextapp.echo.StreamImageReference
          extended byechopoint.image.EncodedImageReference
              extended byechopoint.image.TextImageReference
All Implemented Interfaces:
nextapp.echo.ImageReference, java.io.Serializable

public class TextImageReference
extends EncodedImageReference
implements java.io.Serializable

The TextImageReference class is an ImageReference implementation that can draw text onto a background image. This makes the class perfect for "dynamic" button icons since a "button-like" background image can be used and then button text can be dynamically drawn on it.

If the text to be rendered wont fit within the background image (plus its Insets) then the background image can be "scaled" to fit the text. The scaling options are :

The last two scaling options are very useful for "button-like" background images. This is because they will not scale the "outer" edges of the image but rather "splice and fill". This means that "button-like shadows and outlines" will not be lost or scaled unecesarily.

TextImageReference will also "break" strings on carriage return boundaries, to allow for multi line text. This may result in the need to "auto-resize" the background image.

The text is drawn at a specific {x,y} position. If you use a contructor without the {x,y} parameters, then the text is automatically centred within the height and width of the backgroundImage. You can use the horizontal and vertical alignment properties to control where the text is placed in relation to the {x,y} position. The {x,y} position will be the base line of the text.

AttributedString's are used to render the image text. These contain formatting instructions for specific characters. While the formatting information uses java.awt.* visual classes, a series of static helper methods have been provided to help create java.text.AttributedStrings using Echo color/font objects. You can also use the ColorKit and FontKit classes.

AttributedStrings have a great feature whereby the Unicode replacement character u'FFFC' can be substituted with another visual element. This is supported via the replacementImages and the replacementImageAlignment properties. The images in the replacementImages will be substituted, in a round robin fashion, for each Unicode replacement character in the AttributedString.

This class by default does not keep the encoded output image bytes in memory. You can change this by setKeptInMemory(true).

You should think about loading the background images via ImageKit.loadCachedImage(...) so that these static images are cached even if the dynamic TextImageReference is not.


Nested Class Summary
private  class TextImageReference.ACI
          An AttributedCharacterIteraror delegate.
private  class TextImageReference.TextImageDrawer
          TextImageDrawer holds all the drawing code, just to help us organise this java source file.
 
Field Summary
private  java.text.AttributedString attributedString
           
private  boolean autoCenterAtConstruction
           
private  java.awt.Image backgroundImage
           
private  boolean bestQuality
           
static nextapp.echo.Insets DEFAULT_INSETS
          The default insets between the text and the outer edge of the background image
private  TextImageReference.TextImageDrawer drawer
           
private  int horizontalAlignment
           
private  nextapp.echo.Insets insets
           
private  int origImageH
           
private  int origImageW
           
private  int replacementImageAlignment
           
private  java.awt.Image[] replacementImages
           
static int SCALE_DEFAULT
          The default scaling option is SCALE_SPLICE_V_THEN_H
static int SCALE_FAST
          Choose a scaling algorithm to optimize speed more than smoothness of the scaled image
static int SCALE_NONE
          Dont scale the background image to the text size
static int SCALE_SMOOTH
          Choose a scaling algorithm to optimize smoothness of the scaled image more than speed
static int SCALE_SPLICE_H_THEN_V
          Choose a scaling algorithm that splices horizontally then vertically
static int SCALE_SPLICE_V_THEN_H
          Choose a scaling algorithm that splices vertically then horizontally
private  int scaleOption
           
private  int textAngle
           
private  int verticalAlignment
           
private  int x
           
private  int y
           
 
Fields inherited from class echopoint.image.EncodedImageReference
 
Fields inherited from class nextapp.echo.AbstractImageReference
listenerList
 
Fields inherited from interface nextapp.echo.ImageReference
SIZE_UNKNOWN
 
Constructor Summary
TextImageReference(java.text.AttributedString string, java.awt.Image backgroundImage)
          Constructs a TextImageReference that will be drawn centered within the background image.
TextImageReference(java.text.AttributedString string, java.awt.Image backgroundImage, int scaleOption)
          Constructs a TextImageReference that will be drawn centered within the background image and scalled according to the scaling option.
TextImageReference(java.text.AttributedString string, java.lang.String imageFileName)
          Constructs a TextImageReference that will be drawn centered within the background image, which is loaded from a file.
TextImageReference(java.text.AttributedString string, java.net.URL imageURL)
          Constructs a TextImageReference that will be centered within the background image, which is loaded from an URL.
TextImageReference(int x, int y, java.text.AttributedString string, java.awt.Image backgroundImage)
          Constructs a TextImageReference that will be drawn at the x,y points within the background image.
TextImageReference(int x, int y, java.text.AttributedString string, java.lang.String imageFileName)
          Constructs a TextImageReference that will be drawn at the x,y points within the background image, which is loaded from a file.
TextImageReference(int x, int y, java.text.AttributedString string, java.net.URL imageURL)
          Constructs a TextImageReference that will be drawn at the x,y points within the background image, which is loaded from an URL.
 
Method Summary
private  void constructTextImageRefererence(int x, int y, java.text.AttributedString string, java.awt.Image backgroundImage, int scalingOption)
          Constructs the object
private  void fireImageChange()
          Called when the image properties have changed and the background image might have to be scaled.
static java.text.AttributedString getAttributedString(java.lang.String s)
          Returns an AttributedString with no color or font information
static java.text.AttributedString getAttributedString(java.lang.String s, nextapp.echo.Color foreground, nextapp.echo.Font font)
          Returns an AttributedString with the specified forground color and font based on nextapp.echo.* visual objects.
 java.awt.Image getBackgroundImage()
          Returns the backgroundImage in use
 int getHeight()
          This returns the height of the backgroundImage
 int getHorizontalAlignment()
          Returns the horizontal alignment of the text relative to the x,y drawing point.
 java.awt.Image getImage()
          We override getImage() so that the resultant image is "painted" every time it is needed.
 nextapp.echo.Insets getInsets()
          Returns the Insets in use
 int getReplacementImageAlignment()
          Returns the alignment of the replacement images within the AttributedString.
 java.awt.Image[] getReplacementImages()
          Returns the array of replacement images that will be substituted for the Unicode replacement character 'u'FFFC'.
 int getScaleOption()
          Returns the scaling option in effect.
 java.text.AttributedString getString()
          Returns the AttributedString in use.
 int getTextAngle()
          Returns the current textAngle.
protected  java.awt.font.TextLayout[] getTextLayouts(java.awt.Graphics2D g, java.text.AttributedString as)
          This methods returns an array of TextLayouts that will be used to draw the attributed string.
 int getVerticalAlignment()
          Returns the vertical alignment of the text relative to the x,y drawing point.
 int getWidth()
          This returns the width of the backgroundImage.
 int getX()
          The x position where the etxt will be drawn.
 int getY()
          The y position where the etxt will be drawn.
 boolean isBestQuality()
          Returns true if the image produce is of the best quality.
protected  java.awt.geom.Rectangle2D paintText(java.awt.Graphics2D g, int x, int y)
          Called to paint the text of the TextImageReference.
private  void readObject(java.io.ObjectInputStream in)
           
protected  void replaceAttributedImages(java.awt.Graphics2D g, java.awt.font.FontRenderContext frc, java.text.AttributedString as)
          Replaces all Unicode replacement characters u'FFFC' with the images from getReplacementImages() array in a round robin fashion.
 void setBackgroundImage(java.awt.Image image)
          Sets the background image to be used
 void setBackgroundImage(java.lang.String imageFileName)
          Sets the background image by loading from an image file.
 void setBackgroundImage(java.net.URL imageURL)
          Sets the background image by loading from an image URL.
 void setBestQuality(boolean b)
          Set this to true if you want the best quality images to be drawn.
 void setHorizontalAlignment(int i)
          Sets the horizontal alignment of the text relative to the x,y drawing point.
 void setInsets(nextapp.echo.Insets insets)
          Sets the Insets between the text and the background image outer edge.
 void setReplacementImageAlignment(int i)
          Sets the alignment of the replacement images within the AttributedString.
 void setReplacementImages(java.awt.Image[] images)
          Sets the array of replacement images that will be substituted for the Unicode replacement character u'FFFC'.
 void setScaleOption(int scalingOption)
          Sets the scaling option that will be used to draw the background image.
 void setString(java.text.AttributedString string)
          This is the AttributedString to be drawn onto the image.
 void setTextAngle(int i)
          Sets the text angle in degrees to be used.
 void setVerticalAlignment(int i)
          Sets the vertical alignment of the text relative to the x,y drawing point.
 void setX(int i)
          Sets the X position to draw the text on the image.
 void setY(int i)
          Sets the y position for drawing the text on the image.
 java.lang.String toString()
          Convert this Object to a human-readable String.
private  void writeObject(java.io.ObjectOutputStream out)
           
 
Methods inherited from class echopoint.image.EncodedImageReference
getContentType, getEncoder, isKeptInMemory, isValid, render, setEncoder, setImage, setKeptInMemory, setValid, update
 
Methods inherited from class nextapp.echo.AbstractImageReference
addImageUpdateListener, removeImageUpdateListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SCALE_NONE

public static final int SCALE_NONE
Dont scale the background image to the text size

See Also:
Constant Field Values

SCALE_FAST

public static final int SCALE_FAST
Choose a scaling algorithm to optimize speed more than smoothness of the scaled image

See Also:
Constant Field Values

SCALE_SMOOTH

public static final int SCALE_SMOOTH
Choose a scaling algorithm to optimize smoothness of the scaled image more than speed

See Also:
Constant Field Values

SCALE_SPLICE_V_THEN_H

public static final int SCALE_SPLICE_V_THEN_H
Choose a scaling algorithm that splices vertically then horizontally

See Also:
Constant Field Values

SCALE_SPLICE_H_THEN_V

public static final int SCALE_SPLICE_H_THEN_V
Choose a scaling algorithm that splices horizontally then vertically

See Also:
Constant Field Values

SCALE_DEFAULT

public static final int SCALE_DEFAULT
The default scaling option is SCALE_SPLICE_V_THEN_H

See Also:
Constant Field Values

DEFAULT_INSETS

public static final nextapp.echo.Insets DEFAULT_INSETS
The default insets between the text and the outer edge of the background image


attributedString

private transient java.text.AttributedString attributedString

backgroundImage

private transient java.awt.Image backgroundImage

bestQuality

private boolean bestQuality

autoCenterAtConstruction

private boolean autoCenterAtConstruction

horizontalAlignment

private int horizontalAlignment

replacementImageAlignment

private int replacementImageAlignment

replacementImages

private transient java.awt.Image[] replacementImages

textAngle

private int textAngle

verticalAlignment

private int verticalAlignment

x

private int x

y

private int y

origImageW

private int origImageW

origImageH

private int origImageH

scaleOption

private int scaleOption

insets

private nextapp.echo.Insets insets

drawer

private TextImageReference.TextImageDrawer drawer
Constructor Detail

TextImageReference

public TextImageReference(java.text.AttributedString string,
                          java.awt.Image backgroundImage)
Constructs a TextImageReference that will be drawn centered within the background image.


TextImageReference

public TextImageReference(java.text.AttributedString string,
                          java.awt.Image backgroundImage,
                          int scaleOption)
Constructs a TextImageReference that will be drawn centered within the background image and scalled according to the scaling option.


TextImageReference

public TextImageReference(java.text.AttributedString string,
                          java.lang.String imageFileName)
Constructs a TextImageReference that will be drawn centered within the background image, which is loaded from a file.


TextImageReference

public TextImageReference(java.text.AttributedString string,
                          java.net.URL imageURL)
Constructs a TextImageReference that will be centered within the background image, which is loaded from an URL.


TextImageReference

public TextImageReference(int x,
                          int y,
                          java.text.AttributedString string,
                          java.awt.Image backgroundImage)
Constructs a TextImageReference that will be drawn at the x,y points within the background image.


TextImageReference

public TextImageReference(int x,
                          int y,
                          java.text.AttributedString string,
                          java.lang.String imageFileName)
Constructs a TextImageReference that will be drawn at the x,y points within the background image, which is loaded from a file.


TextImageReference

public TextImageReference(int x,
                          int y,
                          java.text.AttributedString string,
                          java.net.URL imageURL)
Constructs a TextImageReference that will be drawn at the x,y points within the background image, which is loaded from an URL.

Method Detail

constructTextImageRefererence

private void constructTextImageRefererence(int x,
                                           int y,
                                           java.text.AttributedString string,
                                           java.awt.Image backgroundImage,
                                           int scalingOption)
Constructs the object


paintText

protected java.awt.geom.Rectangle2D paintText(java.awt.Graphics2D g,
                                              int x,
                                              int y)
Called to paint the text of the TextImageReference. The background image is already present when this method is called

Subclasses are expected to draw the AttributedString, respecting the other parameters that are in force such as textAngle

This method returns a Rectangle2D that outlines the boundaries of any text painted, whcih may be larger than the actual drawing space.


getTextLayouts

protected java.awt.font.TextLayout[] getTextLayouts(java.awt.Graphics2D g,
                                                    java.text.AttributedString as)
This methods returns an array of TextLayouts that will be used to draw the attributed string. In this default version of the method, the TextLayouts are broken down on carriage return boundaries. These TextLayouts can then be used to determine the height and width of the attributed string.

This is called to determine the dimensions of the current text. This is used to decide whether to "scale" the background image if the text inside it is bigger then these plus the insets. This method should return the same dimensions that are used in the paintText() method.


replaceAttributedImages

protected void replaceAttributedImages(java.awt.Graphics2D g,
                                       java.awt.font.FontRenderContext frc,
                                       java.text.AttributedString as)
Replaces all Unicode replacement characters u'FFFC' with the images from getReplacementImages() array in a round robin fashion.


getImage

public java.awt.Image getImage()
We override getImage() so that the resultant image is "painted" every time it is needed. This is a computation vs memory tradeoff.

Overrides:
getImage in class EncodedImageReference

getBackgroundImage

public java.awt.Image getBackgroundImage()
Returns the backgroundImage in use


getHeight

public int getHeight()
This returns the height of the backgroundImage

Specified by:
getHeight in interface nextapp.echo.ImageReference
Overrides:
getHeight in class EncodedImageReference

getHorizontalAlignment

public int getHorizontalAlignment()
Returns the horizontal alignment of the text relative to the x,y drawing point.


getReplacementImageAlignment

public int getReplacementImageAlignment()
Returns the alignment of the replacement images within the AttributedString. This can be EchoConstants.TOP, EchoConstants.CENTER or EchoConstants.BOTTOM


getReplacementImages

public java.awt.Image[] getReplacementImages()
Returns the array of replacement images that will be substituted for the Unicode replacement character 'u'FFFC'. These are replaced in a round-robin fashion, by circling around the array. This can be a null array.


getString

public java.text.AttributedString getString()
Returns the AttributedString in use.


getTextAngle

public int getTextAngle()
Returns the current textAngle.


getVerticalAlignment

public int getVerticalAlignment()
Returns the vertical alignment of the text relative to the x,y drawing point.


getWidth

public int getWidth()
This returns the width of the backgroundImage.

Specified by:
getWidth in interface nextapp.echo.ImageReference
Overrides:
getWidth in class EncodedImageReference

getX

public int getX()
The x position where the etxt will be drawn.


getY

public int getY()
The y position where the etxt will be drawn.


isBestQuality

public boolean isBestQuality()
Returns true if the image produce is of the best quality.


setBackgroundImage

public void setBackgroundImage(java.awt.Image image)
Sets the background image to be used


setBackgroundImage

public void setBackgroundImage(java.lang.String imageFileName)
Sets the background image by loading from an image file.


setBackgroundImage

public void setBackgroundImage(java.net.URL imageURL)
Sets the background image by loading from an image URL.


setBestQuality

public void setBestQuality(boolean b)
Set this to true if you want the best quality images to be drawn. This setting affects the rendering hints used during image drawing.


setHorizontalAlignment

public void setHorizontalAlignment(int i)
Sets the horizontal alignment of the text relative to the x,y drawing point.


setReplacementImageAlignment

public void setReplacementImageAlignment(int i)
Sets the alignment of the replacement images within the AttributedString. This can be EchoConstants.TOP, EchoConstants.CENTER or EchoConstants.BOTTOM


setReplacementImages

public void setReplacementImages(java.awt.Image[] images)
Sets the array of replacement images that will be substituted for the Unicode replacement character u'FFFC'. These are replaced in a round-robin fashion, for each Unicode replacement character in the AttribuedtString. This can be a null array and hence no replacement will be done.


setString

public void setString(java.text.AttributedString string)
This is the AttributedString to be drawn onto the image. This can be null.


setTextAngle

public void setTextAngle(int i)
Sets the text angle in degrees to be used.


setVerticalAlignment

public void setVerticalAlignment(int i)
Sets the vertical alignment of the text relative to the x,y drawing point.


setX

public void setX(int i)
Sets the X position to draw the text on the image. If this is set to Integer.MAX_VALUE then the text will be centered within the background image.


setY

public void setY(int i)
Sets the y position for drawing the text on the image. If this is set to Integer.MAX_VALUE then the text will be centered within the background image.


readObject

private void readObject(java.io.ObjectInputStream in)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException

writeObject

private void writeObject(java.io.ObjectOutputStream out)
                  throws java.io.IOException

getAttributedString

public static java.text.AttributedString getAttributedString(java.lang.String s)
Returns an AttributedString with no color or font information


getAttributedString

public static java.text.AttributedString getAttributedString(java.lang.String s,
                                                             nextapp.echo.Color foreground,
                                                             nextapp.echo.Font font)
Returns an AttributedString with the specified forground color and font based on nextapp.echo.* visual objects.


getScaleOption

public int getScaleOption()
Returns the scaling option in effect.


setScaleOption

public void setScaleOption(int scalingOption)
Sets the scaling option that will be used to draw the background image. If the text of the TextImageReference is to large for the background image, then the image will be scaled up to the requireed size, using this scaling option.


getInsets

public nextapp.echo.Insets getInsets()
Returns the Insets in use


setInsets

public void setInsets(nextapp.echo.Insets insets)
Sets the Insets between the text and the background image outer edge.


fireImageChange

private void fireImageChange()
Called when the image properties have changed and the background image might have to be scaled.


toString

public java.lang.String toString()
Description copied from class: java.lang.Object
Convert this Object to a human-readable String. There are no limits placed on how long this String should be or what it should contain. We suggest you make it as intuitive as possible to be able to place it into System.out.println() 55 and such.

It is typical, but not required, to ensure that this method never completes abruptly with a java.lang.RuntimeException.

This method will be called when performing string concatenation with this object. If the result is null, string concatenation will instead use "null".

The default implementation returns getClass().getName() + "@" + Integer.toHexString(hashCode()).