Class GraphicsUtilities
- java.lang.Object
-
- org.apache.pivot.wtk.GraphicsUtilities
-
public final class GraphicsUtilities extends java.lang.Object
Contains utility methods dealing with the Java2D API.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
GraphicsUtilities.PaintType
Enumeration representing a paint type.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CENTER_X_KEY
Dictionary key for the center X-position value for a radial gradient.static java.lang.String
CENTER_Y_KEY
Dictionary key for the center Y-position value for a radial gradient.static java.lang.String
COLOR_KEY
Dictionary key for the color value.static java.lang.String
END_COLOR_KEY
Dictionary key for the ending color value for a gradient.static java.lang.String
END_X_KEY
Dictionary key for the ending X-position value.static java.lang.String
END_Y_KEY
Dictionary key for the ending Y-position value.static java.lang.String
OFFSET_KEY
Dictionary key for the gradient offset values.static java.lang.String
PAINT_TYPE_KEY
Dictionary key for the paint type value.static java.lang.String
RADIUS_KEY
Dictionary key for the radius value for a radial gradient.static java.lang.String
START_COLOR_KEY
Dictionary key for the starting color value for a gradient.static java.lang.String
START_X_KEY
Dictionary key for the starting X-position value.static java.lang.String
START_Y_KEY
Dictionary key for the starting Y-position value.static java.lang.String
STOPS_KEY
Dictionary key for the dictionary of gradient stop values.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.awt.Color
decodeColor(java.lang.String value)
Interprets a string as a color value.static java.awt.Color
decodeColor(java.lang.String value, java.lang.String argument)
Interprets a string as a color value.static java.awt.Paint
decodePaint(java.lang.String value)
Interpret a string as aPaint
value.static java.awt.Paint
decodePaint(Dictionary<java.lang.String,?> dictionary)
Interpret a dictionary as aPaint
value.static void
drawBorders(java.awt.Graphics2D graphics, Borders borders, int top, int left, int bottom, int right)
Draw borders around a rectangular area.static void
drawLine(java.awt.Graphics2D graphics, int x, int y, int length, Orientation orientation)
Draw a straight line in the given graphics context from a start position for a given length along the given horizontal or vertical direction with default thickness.static void
drawLine(java.awt.Graphics2D graphics, int x, int y, int length, Orientation orientation, int thickness)
Draw a straight line in the given graphics context from a start position for a given length along the given horizontal or vertical direction with given thickness.static void
drawRect(java.awt.Graphics2D graphics, int x, int y, int width, int height)
Draws a rectangle with a thickness of one pixel at the specified coordinates whose outer border is the specified width and height.static void
drawRect(java.awt.Graphics2D graphics, int x, int y, int width, int height, int thickness)
Draws a rectangle with the specified thickness at the specified coordinates whose outer border is the specified width and height.static Dimensions
getAverageCharacterSize(java.awt.Font font)
Calculate the average character bounds for the given font.static java.awt.Rectangle
getCaretRectangle(java.awt.font.TextHitInfo caret, java.text.AttributedCharacterIterator text, int leftOffset, int topOffset)
Get a caret rectangle from the given attributed text.static java.awt.Color
getColor(int rgb, float alpha)
Generate a full color value given the RGB value along with the alpha (opacity) value.static java.awt.BasicStroke
getFocusStroke(boolean thick)
Construct a line stroke object for a focus rectangle, in one of two styles: very thin with one pixel gaps, and a thicker one of double width and two pixel gaps.static java.awt.font.FontRenderContext
prepareForText(java.awt.Graphics2D graphics)
Prepare for text rendering by getting the platform font render context and then setting the default rendering hints in the graphics object.static void
prepareForText(java.awt.Graphics2D graphics, java.awt.font.FontRenderContext fontRenderContext, java.awt.Font font, java.awt.Color color)
Set the context in the given graphics environment for subsequent font drawing.static java.awt.font.FontRenderContext
prepareForText(java.awt.Graphics2D graphics, java.awt.Font font, java.awt.Color color)
Set the context in the given graphics environment for subsequent font drawing and return the font render context.static void
setAntialiasingOff(java.awt.Graphics2D graphics)
Set anti-aliasing of for the given graphics context.static void
setAntialiasingOn(java.awt.Graphics2D graphics)
Set anti-aliasing on for the given graphics context.static void
setFontRenderingHints(java.awt.Graphics2D graphics, java.awt.font.FontRenderContext fontRenderContext)
Set the default font rendering hints for the given graphics object.
-
-
-
Field Detail
-
PAINT_TYPE_KEY
public static final java.lang.String PAINT_TYPE_KEY
Dictionary key for the paint type value.- See Also:
- Constant Field Values
-
COLOR_KEY
public static final java.lang.String COLOR_KEY
Dictionary key for the color value.- See Also:
- Constant Field Values
-
START_X_KEY
public static final java.lang.String START_X_KEY
Dictionary key for the starting X-position value.- See Also:
- Constant Field Values
-
START_Y_KEY
public static final java.lang.String START_Y_KEY
Dictionary key for the starting Y-position value.- See Also:
- Constant Field Values
-
END_X_KEY
public static final java.lang.String END_X_KEY
Dictionary key for the ending X-position value.- See Also:
- Constant Field Values
-
END_Y_KEY
public static final java.lang.String END_Y_KEY
Dictionary key for the ending Y-position value.- See Also:
- Constant Field Values
-
START_COLOR_KEY
public static final java.lang.String START_COLOR_KEY
Dictionary key for the starting color value for a gradient.- See Also:
- Constant Field Values
-
END_COLOR_KEY
public static final java.lang.String END_COLOR_KEY
Dictionary key for the ending color value for a gradient.- See Also:
- Constant Field Values
-
CENTER_X_KEY
public static final java.lang.String CENTER_X_KEY
Dictionary key for the center X-position value for a radial gradient.- See Also:
- Constant Field Values
-
CENTER_Y_KEY
public static final java.lang.String CENTER_Y_KEY
Dictionary key for the center Y-position value for a radial gradient.- See Also:
- Constant Field Values
-
RADIUS_KEY
public static final java.lang.String RADIUS_KEY
Dictionary key for the radius value for a radial gradient.- See Also:
- Constant Field Values
-
STOPS_KEY
public static final java.lang.String STOPS_KEY
Dictionary key for the dictionary of gradient stop values.- See Also:
- Constant Field Values
-
OFFSET_KEY
public static final java.lang.String OFFSET_KEY
Dictionary key for the gradient offset values.- See Also:
- Constant Field Values
-
-
Method Detail
-
setAntialiasingOn
public static void setAntialiasingOn(java.awt.Graphics2D graphics)
Set anti-aliasing on for the given graphics context.- Parameters:
graphics
- The 2D graphics context to set the attribute for.
-
setAntialiasingOff
public static void setAntialiasingOff(java.awt.Graphics2D graphics)
Set anti-aliasing of for the given graphics context.- Parameters:
graphics
- The 2D graphics context to clear the attribute for.
-
drawLine
public static void drawLine(java.awt.Graphics2D graphics, int x, int y, int length, Orientation orientation)
Draw a straight line in the given graphics context from a start position for a given length along the given horizontal or vertical direction with default thickness.- Parameters:
graphics
- The graphics context to draw in.x
- Starting X position.y
- Starting Y position.length
- Length along the desired direction.orientation
- Whether the line is vertical or horizontal for the given length.- See Also:
drawLine(Graphics2D, int, int, int, Orientation, int)
-
drawLine
public static void drawLine(java.awt.Graphics2D graphics, int x, int y, int length, Orientation orientation, int thickness)
Draw a straight line in the given graphics context from a start position for a given length along the given horizontal or vertical direction with given thickness.- Parameters:
graphics
- The graphics context to draw in.x
- Starting X position.y
- Starting Y position.length
- Length along the desired direction.orientation
- Whether the line is vertical or horizontal for the given length.thickness
- The pixel thickness of the line.- See Also:
drawLine(Graphics2D, int, int, int, Orientation)
-
drawRect
public static void drawRect(java.awt.Graphics2D graphics, int x, int y, int width, int height)
Draws a rectangle with a thickness of one pixel at the specified coordinates whose outer border is the specified width and height. In other words, the distance from the left edge of the leftmost pixel to the left edge of the rightmost pixel iswidth - 1
.This method provides more reliable pixel rounding behavior than
java.awt.Graphics#drawRect
when scaling is applied because this method does not stroke the shape but instead explicitly fills the desired pixels with the graphics context's paint. For this reason, and because Pivot supports scaling the display host, it is recommended that skins use this method overjava.awt.Graphics#drawRect
.- Parameters:
graphics
- The graphics context that will be used to perform the operation.x
- The x-coordinate of the upper-left corner of the rectangle.y
- The y-coordinate of the upper-left corner of the rectangle.width
- The outer width of the rectangle.height
- The outer height of the rectangle.- See Also:
drawRect(Graphics2D, int, int, int, int, int)
-
drawRect
public static void drawRect(java.awt.Graphics2D graphics, int x, int y, int width, int height, int thickness)
Draws a rectangle with the specified thickness at the specified coordinates whose outer border is the specified width and height. In other words, the distance from the left edge of the leftmost pixel to the left edge of the rightmost pixel iswidth - thickness
.This method provides more reliable pixel rounding behavior than
java.awt.Graphics#drawRect
when scaling is applied because this method does not stroke the shape but instead explicitly fills the desired pixels with the graphics context's paint. For this reason, and because Pivot supports scaling the display host, it is recommended that skins use this method overjava.awt.Graphics#drawRect
.- Parameters:
graphics
- The graphics context that will be used to perform the operation.x
- The x-coordinate of the upper-left corner of the rectangle.y
- The y-coordinate of the upper-left corner of the rectangle.width
- The outer width of the rectangle.height
- The outer height of the rectangle.thickness
- The thickness of each edge.- See Also:
drawRect(Graphics2D, int, int, int, int)
-
decodeColor
public static java.awt.Color decodeColor(java.lang.String value, java.lang.String argument) throws java.lang.NumberFormatException
Interprets a string as a color value.- Parameters:
value
- One of the following forms:- 0xdddddddd - 8 hexadecimal digits, specifying 8 bits each of red, green, and blue, followed by 8 bits of alpha.
- #dddddd - 6 hexadecimal digits, specifying 8 bits each of red, green, and blue.
- Any of the names of the static colors in the Java
Color
class. - Any of the CSS3/X11 color names from here: http://www.w3.org/TR/css3-color/
- Any of the standard Java color names given by the fields in the
Color
class (such asLIGHT_GRAY
and the like). - null - case-insensitive
Note: Since the RGB color value of the CSS color "Green" (or "Pink" and some others) is different than the Java
Color.green
orColor.GREEN
, we decode color names in a case-sensitive way. See the notes inCSSColor
for more information.argument
- A name for this color value (for the exception if it can't be decoded).- Returns:
- A
Color
on successful decoding, which could benull
for an input of"null"
. - Throws:
java.lang.NumberFormatException
- if the value in the first two cases contains illegal hexadecimal digits.java.lang.IllegalArgumentException
- if the value is not in one of the formats listed above.- See Also:
CSSColor
-
decodeColor
public static java.awt.Color decodeColor(java.lang.String value) throws java.lang.NumberFormatException
Interprets a string as a color value.- Parameters:
value
- One of the following forms:- 0xdddddddd - 8 hexadecimal digits, specifying 8 bits each of red, green, and blue, followed by 8 bits of alpha.
- #dddddd - 6 hexadecimal digits, specifying 8 bits each of red, green, and blue.
- Any of the names of the static colors in the Java
Color
class. - Any of the CSS3/X11 color names from here: http://www.w3.org/TR/css3-color/ (except the Java color values will be used for the standard Java names).
- null - case-insensitive
- Returns:
- A
Color
on successful decoding - Throws:
java.lang.NumberFormatException
- if the value in the first two cases contains illegal hexadecimal digits.java.lang.IllegalArgumentException
- if the value is not in one of the formats listed above.- See Also:
decodeColor(String, String)
,CSSColor
-
getColor
public static java.awt.Color getColor(int rgb, float alpha)
Generate a full color value given the RGB value along with the alpha (opacity) value.- Parameters:
rgb
- The 24-bit red, green, and blue value.alpha
- The opacity value (0.0 - 1.0).- Returns:
- The full color value from these two parts.
-
decodePaint
public static java.awt.Paint decodePaint(java.lang.String value)
Interpret a string as aPaint
value.- Parameters:
value
- Either (a) One of the color values recognized by Pivot or (b) A JSON dictionary describing a Paint value.- Returns:
- The decoded paint value.
- Throws:
java.lang.IllegalArgumentException
- if the given value isnull
or empty or there is a problem decoding the value.
-
decodePaint
public static java.awt.Paint decodePaint(Dictionary<java.lang.String,?> dictionary)
Interpret a dictionary as aPaint
value.- Parameters:
dictionary
- A dictionary containing a key "paintType" and further elements according to its value:- solid_color - key "color" with value being any of the color values recognized by Pivot
- gradient - keys "startX", "startY", "endX", "endY" (values are coordinates), "startColor", "endColor" (values are colors)
- linear_gradient - keys "startX", "startY", "endX", "endY" (coordinates), "stops" (a list of dictionaries with keys "offset" (a number in [0,1]) and "color")
- radial_gradient - keys "centerX", "centerY" (coordinates), "radius" (a number), "stops" (a list of dictionaries with keys "offset" and "color")
- Returns:
- The fully decoded paint value.
- Throws:
java.lang.IllegalArgumentException
- if there is no paint type key found.
-
setFontRenderingHints
public static void setFontRenderingHints(java.awt.Graphics2D graphics, java.awt.font.FontRenderContext fontRenderContext)
Set the default font rendering hints for the given graphics object.- Parameters:
graphics
- The graphics object to initialize.fontRenderContext
- The source of the font rendering hints.
-
prepareForText
public static void prepareForText(java.awt.Graphics2D graphics, java.awt.font.FontRenderContext fontRenderContext, java.awt.Font font, java.awt.Color color)
Set the context in the given graphics environment for subsequent font drawing.- Parameters:
graphics
- The graphics context.fontRenderContext
- The font rendering context used to get the font drawing hints.font
- The font to use.color
- The foreground color for the text.
-
prepareForText
public static java.awt.font.FontRenderContext prepareForText(java.awt.Graphics2D graphics)
Prepare for text rendering by getting the platform font render context and then setting the default rendering hints in the graphics object.- Parameters:
graphics
- The graphics object to prepare.- Returns:
- The
Platform.getFontRenderContext()
value.
-
prepareForText
public static java.awt.font.FontRenderContext prepareForText(java.awt.Graphics2D graphics, java.awt.Font font, java.awt.Color color)
Set the context in the given graphics environment for subsequent font drawing and return the font render context.- Parameters:
graphics
- The graphics context.font
- The font to use.color
- The foreground color for the text.- Returns:
- The font render context for the platform.
-
getAverageCharacterSize
public static Dimensions getAverageCharacterSize(java.awt.Font font)
Calculate the average character bounds for the given font.This bounds is the width of the "missing glyph code" and the maximum character height of any glyph in the font.
- Parameters:
font
- The font in question.- Returns:
- The bounding rectangle to use for the average character size.
- See Also:
Platform.getFontRenderContext()
-
getCaretRectangle
public static java.awt.Rectangle getCaretRectangle(java.awt.font.TextHitInfo caret, java.text.AttributedCharacterIterator text, int leftOffset, int topOffset)
Get a caret rectangle from the given attributed text.- Parameters:
caret
- The location within the text where the caret should be located.text
- The attributed text iterator.leftOffset
- Horizontal offset within the control of the text (to add into the position).topOffset
- Same for vertical offset.- Returns:
- The resulting rectangle for the caret.
-
drawBorders
public static void drawBorders(java.awt.Graphics2D graphics, Borders borders, int top, int left, int bottom, int right)
Draw borders around a rectangular area.- Parameters:
graphics
- The graphics area to draw in.borders
- The borders specification.top
- The top coordinate (typically 0)left
- The left coordinate (typically 0)bottom
- The bottom interior coordinate (height - 1)right
- The right interior coordinate (width - 1)
-
getFocusStroke
public static java.awt.BasicStroke getFocusStroke(boolean thick)
Construct a line stroke object for a focus rectangle, in one of two styles: very thin with one pixel gaps, and a thicker one of double width and two pixel gaps.- Parameters:
thick
-true
to get the thicker style, orfalse
to get the (default) thin one.- Returns:
- The line stroke needed to draw the line(s).
-
-