net.sourceforge.webcompmath.draw
Class RationalLabel

java.lang.Object
  extended by net.sourceforge.webcompmath.draw.RationalLabel

public class RationalLabel
extends java.lang.Object

RationalLabels provide a way to label an axis with fractions, with an optional constant factor such as pi. The class adjusts for special cases:

1) the numerator is zero

2) the denominator is one (and hence no fraction is needed)

3) there is a factor and the numerator is 1; in this case, the factor is placed in the numerator (i.e., it shows pi/2 instead of 1/2 pi)

4) if it is negative and a fraction is needed, the minus sign is placed in front of the fractions (not in the numerator)

To use, create a RationalLabel, then call its drawFraction method. You can also use getBounds to find out how big it will be (useful when labeling an axis to avoid crowding labels too close together).

Author:
Tom Downey

Nested Class Summary
static class RationalLabel.HJust
          Specify the horizontal justification for the rational label
static class RationalLabel.VJust
          Specify the vertical justification for the rational label
 
Field Summary
static java.lang.String pi
          Used to represent pi
 
Constructor Summary
RationalLabel()
          Default Constructor
RationalLabel(Fraction frac, java.lang.String factorString)
          Construct a rational label.
 
Method Summary
 void drawFraction(java.awt.Graphics2D g2, int x, int y, RationalLabel.HJust hJust, RationalLabel.VJust vJust)
          Draws the rational label.
 java.awt.geom.Rectangle2D getBounds(java.awt.Graphics2D g2)
          This method returns a bounding rectangle for the rational label.
 java.lang.String getFactorString()
          Get the factor
 Fraction getFrac()
          Get the fraction
 boolean isRationalText()
          Get whether to use text or typeset label
 void setFactorString(java.lang.String factorString)
          Set the factor (does nothing if argument is null)
 void setFrac(Fraction frac)
          Set the fraction (does nothing if argument is null)
 void setRationalText(boolean rationalText)
          Set whether to use text or typeset label
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pi

public static final java.lang.String pi
Used to represent pi

See Also:
Constant Field Values
Constructor Detail

RationalLabel

public RationalLabel()
Default Constructor


RationalLabel

public RationalLabel(Fraction frac,
                     java.lang.String factorString)
Construct a rational label. Does nothing if either argument is null

Parameters:
frac - the fraction to use for the label
factorString - the constant factor, usually "", but might be "e" or pi
Method Detail

getFactorString

public java.lang.String getFactorString()
Get the factor

Returns:
Returns the factorString.

setFactorString

public void setFactorString(java.lang.String factorString)
Set the factor (does nothing if argument is null)

Parameters:
factorString - The factorString to set.

getFrac

public Fraction getFrac()
Get the fraction

Returns:
Returns the frac.

setFrac

public void setFrac(Fraction frac)
Set the fraction (does nothing if argument is null)

Parameters:
frac - The frac to set.

isRationalText

public boolean isRationalText()
Get whether to use text or typeset label

Returns:
Returns the rationalText.

setRationalText

public void setRationalText(boolean rationalText)
Set whether to use text or typeset label

Parameters:
rationalText - true for text, false for typeset (default)

getBounds

public java.awt.geom.Rectangle2D getBounds(java.awt.Graphics2D g2)
This method returns a bounding rectangle for the rational label. The width and height are set to enclose the entire label, including the fraction, any leading minus sign, and any trailing factor. Like the bounds returned from Font.getStringBounds, x is always 0, and y is a negative number indicating the offset from the top to the baseline (i.e., y is -ascent).

Parameters:
g2 - the graphics context
Returns:
the bounding rectangle (float)

drawFraction

public void drawFraction(java.awt.Graphics2D g2,
                         int x,
                         int y,
                         RationalLabel.HJust hJust,
                         RationalLabel.VJust vJust)
Draws the rational label. x and y indicate a point, and hJust and vJust provide the justifications of the label relative to the point. If both are CENTER, then the label will be centered at the point. hJust=CENTER and vJust=TOP would be usefull for x axis labels that lie below the x axis and are top-justified. hJust=RIGHT and vJust=CENTER would be useful for y axis labels that lie to the left of the axis and are right justified.

Parameters:
g2 - the graphics context
x - the x coordinate of the point
y - the y coordinate of the point
hJust - the horizontal justification, CENTER, LEFT, or RIGHT
vJust - the vertical justification, CENTER, TOP, or BOTTOM