Package org.jfree.chart.text
Class TextUtils
java.lang.Object
org.jfree.chart.text.TextUtils
Some utility methods for working with text in Java2D.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Rectangle2D
calcAlignedStringBounds
(String text, Graphics2D g2, float x, float y, TextAnchor anchor) Returns the bounds of an aligned string.static Shape
calculateRotatedStringBounds
(String text, Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY) Returns a shape that represents the bounds of the string after the specified rotation has been applied.static Shape
calculateRotatedStringBounds
(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, TextAnchor rotationAnchor) Returns a shape that represents the bounds of the string after the specified rotation has been applied.static TextBlock
createTextBlock
(String text, Font font, Paint paint) Creates aTextBlock
from aString
.static TextBlock
createTextBlock
(String text, Font font, Paint paint, float maxWidth, int maxLines, TextMeasurer measurer) Creates a new text block from the given string, breaking the text into lines so that themaxWidth
value is respected.static TextBlock
createTextBlock
(String text, Font font, Paint paint, float maxWidth, TextMeasurer measurer) Creates a new text block from the given string, breaking the text into lines so that themaxWidth
value is respected.static Rectangle2D
drawAlignedString
(String text, Graphics2D g2, float x, float y, TextAnchor anchor) Draws a string such that the specified anchor point is aligned to the given (x, y) location.static void
drawRotatedString
(String text, Graphics2D g2, double angle, float x, float y) A utility method for drawing rotated text.static void
drawRotatedString
(String text, Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY) A utility method for drawing rotated text.static void
drawRotatedString
(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, float rotationX, float rotationY) Draws a string that is aligned by one anchor point and rotated about another anchor point.static void
drawRotatedString
(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, TextAnchor rotationAnchor) Draws a string that is aligned by one anchor point and rotated about another anchor point.static boolean
Returns the flag that controls whether or not strings are drawn using the current font attributes (such as underlining, strikethrough etc).static Rectangle2D
getTextBounds
(String text, Graphics2D g2, FontMetrics fm) Returns the bounds for the specified text.static boolean
Returns the flag that controls whether the FontMetrics.getStringBounds() method is used or not.static boolean
Returns the flag that controls whether or not a workaround is used for drawing rotated strings.static void
setDrawStringsWithFontAttributes
(boolean b) Sets the flag that controls whether or not strings are drawn using the current font attributes.static void
setUseDrawRotatedStringWorkaround
(boolean use) Sets the flag that controls whether or not a workaround is used for drawing rotated strings.static void
setUseFontMetricsGetStringBounds
(boolean use) Sets the flag that controls whether the FontMetrics.getStringBounds() method is used or not.
-
Method Details
-
createTextBlock
- Parameters:
text
- the text.font
- the font.paint
- the paint.- Returns:
- A text block.
-
createTextBlock
public static TextBlock createTextBlock(String text, Font font, Paint paint, float maxWidth, TextMeasurer measurer) Creates a new text block from the given string, breaking the text into lines so that themaxWidth
value is respected.- Parameters:
text
- the text.font
- the font.paint
- the paint.maxWidth
- the maximum width for each line.measurer
- the text measurer.- Returns:
- A text block.
-
createTextBlock
public static TextBlock createTextBlock(String text, Font font, Paint paint, float maxWidth, int maxLines, TextMeasurer measurer) Creates a new text block from the given string, breaking the text into lines so that themaxWidth
value is respected.- Parameters:
text
- the text.font
- the font.paint
- the paint.maxWidth
- the maximum width for each line.maxLines
- the maximum number of lines.measurer
- the text measurer.- Returns:
- A text block.
-
getTextBounds
Returns the bounds for the specified text.- Parameters:
text
- the text (null
permitted).g2
- the graphics context (notnull
).fm
- the font metrics (notnull
).- Returns:
- The text bounds (
null
if thetext
argument isnull
).
-
calcAlignedStringBounds
public static Rectangle2D calcAlignedStringBounds(String text, Graphics2D g2, float x, float y, TextAnchor anchor) Returns the bounds of an aligned string.- Parameters:
text
- the string (null
not permitted).g2
- the graphics target (null
not permitted).x
- the x-coordinate.y
- the y-coordinate.anchor
- the anchor point that will be aligned to(x, y)
(null
not permitted).- Returns:
- The text bounds (never
null
).
-
drawAlignedString
public static Rectangle2D drawAlignedString(String text, Graphics2D g2, float x, float y, TextAnchor anchor) Draws a string such that the specified anchor point is aligned to the given (x, y) location.- Parameters:
text
- the text.g2
- the graphics device.x
- the x coordinate (Java 2D).y
- the y coordinate (Java 2D).anchor
- the anchor location.- Returns:
- The text bounds (adjusted for the text position).
-
drawRotatedString
A utility method for drawing rotated text.A common rotation is -Math.PI/2 which draws text 'vertically' (with the top of the characters on the left).
- Parameters:
text
- the text.g2
- the graphics device.angle
- the angle of the (clockwise) rotation (in radians).x
- the x-coordinate.y
- the y-coordinate.
-
drawRotatedString
public static void drawRotatedString(String text, Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY) A utility method for drawing rotated text.A common rotation is -Math.PI/2 which draws text 'vertically' (with the top of the characters on the left).
- Parameters:
text
- the text.g2
- the graphics device.textX
- the x-coordinate for the text (before rotation).textY
- the y-coordinate for the text (before rotation).angle
- the angle of the (clockwise) rotation (in radians).rotateX
- the point about which the text is rotated.rotateY
- the point about which the text is rotated.
-
drawRotatedString
public static void drawRotatedString(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, float rotationX, float rotationY) Draws a string that is aligned by one anchor point and rotated about another anchor point.- Parameters:
text
- the text.g2
- the graphics device.x
- the x-coordinate for positioning the text.y
- the y-coordinate for positioning the text.textAnchor
- the text anchor.angle
- the rotation angle.rotationX
- the x-coordinate for the rotation anchor point.rotationY
- the y-coordinate for the rotation anchor point.
-
drawRotatedString
public static void drawRotatedString(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, TextAnchor rotationAnchor) Draws a string that is aligned by one anchor point and rotated about another anchor point.- Parameters:
text
- the text.g2
- the graphics device.x
- the x-coordinate for positioning the text.y
- the y-coordinate for positioning the text.textAnchor
- the text anchor.angle
- the rotation angle (in radians).rotationAnchor
- the rotation anchor.
-
calculateRotatedStringBounds
public static Shape calculateRotatedStringBounds(String text, Graphics2D g2, float x, float y, TextAnchor textAnchor, double angle, TextAnchor rotationAnchor) Returns a shape that represents the bounds of the string after the specified rotation has been applied.- Parameters:
text
- the text (null
permitted).g2
- the graphics device.x
- the x coordinate for the anchor point.y
- the y coordinate for the anchor point.textAnchor
- the text anchor.angle
- the angle.rotationAnchor
- the rotation anchor.- Returns:
- The bounds (possibly
null
).
-
calculateRotatedStringBounds
public static Shape calculateRotatedStringBounds(String text, Graphics2D g2, float textX, float textY, double angle, float rotateX, float rotateY) Returns a shape that represents the bounds of the string after the specified rotation has been applied.- Parameters:
text
- the text (null
permitted).g2
- the graphics device.textX
- the x coordinate for the text.textY
- the y coordinate for the text.angle
- the angle.rotateX
- the x coordinate for the rotation point.rotateY
- the y coordinate for the rotation point.- Returns:
- The bounds (
null
iftext
isnull
or has zero length).
-
getUseFontMetricsGetStringBounds
Returns the flag that controls whether the FontMetrics.getStringBounds() method is used or not. If you are having trouble with label alignment or positioning, try changing the value of this flag.- Returns:
- A boolean.
-
setUseFontMetricsGetStringBounds
Sets the flag that controls whether the FontMetrics.getStringBounds() method is used or not. If you are having trouble with label alignment or positioning, try changing the value of this flag.- Parameters:
use
- the flag.
-
isUseDrawRotatedStringWorkaround
Returns the flag that controls whether or not a workaround is used for drawing rotated strings.- Returns:
- A boolean.
-
setUseDrawRotatedStringWorkaround
Sets the flag that controls whether or not a workaround is used for drawing rotated strings. The related bug is on Sun's bug parade (id 4312117) and the workaround involves using aTextLayout
instance to draw the text instead of calling thedrawString()
method in theGraphics2D
class.- Parameters:
use
- the new flag value.
-
getDrawStringsWithFontAttributes
Returns the flag that controls whether or not strings are drawn using the current font attributes (such as underlining, strikethrough etc). The default value isfalse
.- Returns:
- A boolean.
-
setDrawStringsWithFontAttributes
Sets the flag that controls whether or not strings are drawn using the current font attributes. This is a hack to allow underlining of titles without big changes to the API. See: http://www.jfree.org/phpBB2/viewtopic.php?p=45459&highlight=#45459- Parameters:
b
- the new flag value.
-