org.opensourcephysics.display
Class TextLine

java.lang.Object
  extended by org.opensourcephysics.display.TextLine
Direct Known Subclasses:
DrawableTextLine

public class TextLine
extends java.lang.Object

TextLine is designed to bundle together all the information required to draw short Strings with subscripts and superscripts. TextLine was modified by W. Christian to add Greek characters using TeX notation.

Author:
Leigh Brookshaw

Field Summary
static int ALGEBRAIC
          Format to use when parsing a double
protected  int ascent
          The ascent using the current font
protected  java.awt.Color background
          Background Color
static int CENTER
          Center the Text over the point
protected  java.awt.Color color
          Text color
protected  int descent
          The descent using the current font
protected  java.awt.Font font
          Font to use for text
protected  java.lang.String fontname
          The logical name of the font to use
protected  int fontsize
          The font size
protected  int fontstyle
          The font style
protected  int height
          The height using the current font ie ascent+descent+leading
protected  int justification
          Text justification.
protected  int leading
          The leading using the current font
static int LEFT
          Position the Text to the Left of the point
protected  java.awt.Graphics lg
          Local graphics context.
protected  java.util.Vector<org.opensourcephysics.display.TextState> list
          The parsed string.
protected  int maxAscent
          The maximum ascent using the current font
protected  int maxDescent
          The maximum descent using the current font
protected  boolean parse
          Has the string been parsed! This only needs to be done once unless the font is altered.
static int RIGHT
          Position the Text to the Right of the point
static int SCIENTIFIC
          Format to use when parsing a double
protected  double script_fraction
          Decrease in size of successive script levels
protected  double sub_offset
          Subscript offset
protected  double sup_offset
          Superscript offset
protected  java.lang.String text
          The text to display
protected  int width
          The width of the text using the current Font
 
Constructor Summary
TextLine()
          Instantiate the class
TextLine(java.awt.Font f, java.awt.Color c, int j)
          Instantiate the class
TextLine(java.lang.String s)
          Instantiate the class.
TextLine(java.lang.String s, java.awt.Color c)
          Instantiate the class
TextLine(java.lang.String s, java.awt.Font f)
          Instantiate the class
TextLine(java.lang.String s, java.awt.Font f, java.awt.Color c, int j)
          Instantiate the class
 
Method Summary
 int charWidth(java.awt.Graphics g, char ch)
           
 TextLine copyState()
          Create a New Textline object copying the state of the existing object into the new one.
 void copyState(TextLine t)
          Copy the state of the parsed Textline into the existing object.
 void drawText(java.awt.Graphics g, int x, int y)
          Parse the text then draw it without any rotation.
 void drawText(java.awt.Graphics g, int x, int y, int j)
          Parse the text then draw it.
 int getAscent(java.awt.Graphics g)
           
 java.awt.Color getBackground()
           
 java.awt.Color getColor()
           
 int getDescent(java.awt.Graphics g)
           
 java.awt.FontMetrics getFM(java.awt.Graphics g)
           
 java.awt.Font getFont()
           
 java.lang.String getFontName()
           
 int getFontSize()
           
 int getFontStyle()
           
 int getHeight(java.awt.Graphics g)
           
 int getJustification()
           
 int getLeading(java.awt.Graphics g)
           
 int getMaxAscent(java.awt.Graphics g)
           
 int getMaxDescent(java.awt.Graphics g)
           
 java.awt.Font getScriptFont(java.awt.Font f)
           
 java.awt.geom.Rectangle2D getStringBounds(java.awt.Graphics g)
          Returns the bounding box for this string.
 java.lang.String getText()
           
 int getWidth(java.awt.Graphics g)
           
 boolean isNull()
           
static double log10(double x)
           
 boolean parseDouble(double d)
          Parse a double value.
 boolean parseDouble(double d, int p)
          Parse a double value.
 boolean parseDouble(double d, int n, int p, int f)
          Parse a double value
 void parseText(java.awt.Graphics g)
          parse the text.
 void setBackground(java.awt.Color c)
          Set the Background Color to use with the class
 void setColor(java.awt.Color c)
          Set the Color to use with the class
 void setFont(java.awt.Font f)
          Set the Font to use with the class
 void setFontName(java.lang.String s)
          Set the Logical font name of the current font
 void setFontSize(int i)
          Set the Font size of the current font
 void setFontStyle(int i)
          Set the Font style of the current font
 void setJustification(int i)
          Set the Justification to use with the class
 void setText(java.lang.String s)
          Set the String to use with the class
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CENTER

public static final int CENTER
Center the Text over the point

See Also:
Constant Field Values

LEFT

public static final int LEFT
Position the Text to the Left of the point

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
Position the Text to the Right of the point

See Also:
Constant Field Values

SCIENTIFIC

public static final int SCIENTIFIC
Format to use when parsing a double

See Also:
Constant Field Values

ALGEBRAIC

public static final int ALGEBRAIC
Format to use when parsing a double

See Also:
Constant Field Values

script_fraction

protected double script_fraction
Decrease in size of successive script levels


sup_offset

protected double sup_offset
Superscript offset


sub_offset

protected double sub_offset
Subscript offset


font

protected java.awt.Font font
Font to use for text


color

protected java.awt.Color color
Text color


background

protected java.awt.Color background
Background Color


text

protected java.lang.String text
The text to display


fontname

protected java.lang.String fontname
The logical name of the font to use


fontsize

protected int fontsize
The font size


fontstyle

protected int fontstyle
The font style


justification

protected int justification
Text justification. Either CENTER, LEFT or RIGHT


width

protected int width
The width of the text using the current Font


ascent

protected int ascent
The ascent using the current font


maxAscent

protected int maxAscent
The maximum ascent using the current font


descent

protected int descent
The descent using the current font


maxDescent

protected int maxDescent
The maximum descent using the current font


height

protected int height
The height using the current font ie ascent+descent+leading


leading

protected int leading
The leading using the current font


parse

protected boolean parse
Has the string been parsed! This only needs to be done once unless the font is altered.


lg

protected java.awt.Graphics lg
Local graphics context.


list

protected java.util.Vector<org.opensourcephysics.display.TextState> list
The parsed string. Each element in the vector represents a change of context in the string ie font change and offset.

Constructor Detail

TextLine

public TextLine()
Instantiate the class


TextLine

public TextLine(java.lang.String s)
Instantiate the class.

Parameters:
s - String to parse.

TextLine

public TextLine(java.lang.String s,
                java.awt.Font f)
Instantiate the class

Parameters:
s - String to parse.
f - Font to use.

TextLine

public TextLine(java.lang.String s,
                java.awt.Font f,
                java.awt.Color c,
                int j)
Instantiate the class

Parameters:
s - String to parse.
f - Font to use.
c - Color to use
j - Justification

TextLine

public TextLine(java.lang.String s,
                java.awt.Color c)
Instantiate the class

Parameters:
s - String to parse.
c - Color to use

TextLine

public TextLine(java.awt.Font f,
                java.awt.Color c,
                int j)
Instantiate the class

Parameters:
f - Font to use.
c - Color to use
j - Justification
Method Detail

copyState

public TextLine copyState()
Create a New Textline object copying the state of the existing object into the new one. The state of the class is the color, font, and justification ie everything but the string.


copyState

public void copyState(TextLine t)
Copy the state of the parsed Textline into the existing object.

Parameters:
t - The TextLine to get the state information from.

setFont

public void setFont(java.awt.Font f)
Set the Font to use with the class

Parameters:
f - Font

setText

public void setText(java.lang.String s)
Set the String to use with the class

Parameters:
s - String

setColor

public void setColor(java.awt.Color c)
Set the Color to use with the class

Parameters:
c - Color

setBackground

public void setBackground(java.awt.Color c)
Set the Background Color to use with the class

Parameters:
c - Color

setJustification

public void setJustification(int i)
Set the Justification to use with the class

Parameters:
i - Justification

getFont

public java.awt.Font getFont()
Returns:
the font the class is using

getText

public java.lang.String getText()
Returns:
the String the class is using.

getColor

public java.awt.Color getColor()
Returns:
the Color the class is using.

getBackground

public java.awt.Color getBackground()
Returns:
the Background Color the class is using.

getJustification

public int getJustification()
Returns:
the Justification the class is using.

getFM

public java.awt.FontMetrics getFM(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the Fontmetrics the class is using.

charWidth

public int charWidth(java.awt.Graphics g,
                     char ch)
Parameters:
g - Graphics context.
ch - The character.
Returns:
the width of the character.

getStringBounds

public java.awt.geom.Rectangle2D getStringBounds(java.awt.Graphics g)
Returns the bounding box for this string.

Parameters:
g - Graphics
Returns:
Rectangle2D

getWidth

public int getWidth(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the width of the parsed text.

getHeight

public int getHeight(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the height of the parsed text.

getAscent

public int getAscent(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the ascent of the parsed text.

getMaxAscent

public int getMaxAscent(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the maximum ascent of the parsed text.

getDescent

public int getDescent(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the descent of the parsed text.

getMaxDescent

public int getMaxDescent(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the maximum descent of the parsed text.

getLeading

public int getLeading(java.awt.Graphics g)
Parameters:
g - Graphics context.
Returns:
the leading of the parsed text.

parseText

public void parseText(java.awt.Graphics g)
parse the text. When the text is parsed the width, height, leading are all calculated. The text will only be truly parsed if the graphics context has changed or the text has changed or the font has changed. Otherwise nothing is done when this method is called.

Parameters:
g - Graphics context.

isNull

public boolean isNull()
Returns:
true if the text has never been set or is null

drawText

public void drawText(java.awt.Graphics g,
                     int x,
                     int y,
                     int j)
Parse the text then draw it.

Parameters:
g - Graphics context
x - pixel position of the text
y - pixel position of the text
j - justification of the text

drawText

public void drawText(java.awt.Graphics g,
                     int x,
                     int y)
Parse the text then draw it without any rotation.

Parameters:
g - Graphics context
x - pixel position of the text
y - pixel position of the text

getFontName

public java.lang.String getFontName()
Returns:
Logical font name of the set font

getFontStyle

public int getFontStyle()
Returns:
Style of the set font

getFontSize

public int getFontSize()
Returns:
Size of the set font

setFontName

public void setFontName(java.lang.String s)
Set the Logical font name of the current font

Parameters:
s - Logical font name.

setFontStyle

public void setFontStyle(int i)
Set the Font style of the current font

Parameters:
i - Font style.

setFontSize

public void setFontSize(int i)
Set the Font size of the current font

Parameters:
i - Font size.

getScriptFont

public java.awt.Font getScriptFont(java.awt.Font f)
Parameters:
f - Font
Returns:
The script font version of the parsed font using the script_fraction variable.

parseDouble

public boolean parseDouble(double d)
Parse a double value. Precision is 6 figures, with 7 significant figures.

Parameters:
d - double to parse return true if the parse was successful

parseDouble

public boolean parseDouble(double d,
                           int p)
Parse a double value. Number of significant figures is 1 greater than the precision.

Parameters:
d - double to parse
p - precision of the number return true if the parse was successful

parseDouble

public boolean parseDouble(double d,
                           int n,
                           int p,
                           int f)
Parse a double value

Parameters:
d - double to parse
n - number of significant figures
p - precision of the number
f - format of the number scientific, algebraic etc. return true if the parse was successful

log10

public static double log10(double x)
                    throws java.lang.ArithmeticException
Parameters:
x - a double value
Returns:
The log10
Throws:
java.lang.ArithmeticException