javax.swing.plaf.basic

Class BasicGraphicsUtils

public class BasicGraphicsUtils extends Object

A utility class providing commonly used drawing and measurement routines.
Constructor Summary
BasicGraphicsUtils()
Constructor.
Method Summary
static voiddrawBezel(Graphics g, int x, int y, int width, int height, boolean isPressed, boolean isDefault, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
Draws a border that is suitable for buttons of the Basic look and feel.
static voiddrawDashedRect(Graphics g, int x, int y, int width, int height)
Draws a rectangle, simulating a dotted stroke by painting only every second pixel along the one-pixel thick edge.
static voiddrawEtchedRect(Graphics g, int x, int y, int width, int height, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
Draws a rectangle that appears etched into the surface, given four colors that are used for drawing.
static voiddrawGroove(Graphics g, int x, int y, int width, int height, Color shadow, Color highlight)
Draws a rectangle that appears etched into the surface, given two colors that are used for drawing.
static voiddrawLoweredBezel(Graphics g, int x, int y, int width, int height, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
Draws a rectangle that appears lowered into the surface, given four colors that are used for drawing.
static voiddrawString(Graphics g, String text, int underlinedChar, int x, int y)
Draws a String at the given location, underlining the first occurence of a specified character.
static voiddrawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
Draws a String at the given location, underlining the character at the specified index.
static InsetsgetEtchedInsets()
Determines the width of the border that gets painted by {@link #drawEtchedRect}.
static InsetsgetGrooveInsets()
Determines the width of the border that gets painted by {@link #drawGroove}.
static DimensiongetPreferredButtonSize(AbstractButton b, int textIconGap)
Determines the preferred width and height of an AbstractButton, given the gap between the button’s text and icon.

Constructor Detail

BasicGraphicsUtils

public BasicGraphicsUtils()
Constructor. It is utterly unclear why this class should be constructable, but this is what the API specification says.

Method Detail

drawBezel

public static void drawBezel(Graphics g, int x, int y, int width, int height, boolean isPressed, boolean isDefault, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
Draws a border that is suitable for buttons of the Basic look and feel.

[An illustration that shows which pixels
 get painted in what color]

Parameters: g the graphics into which the rectangle is drawn. x the x coordinate of the rectangle. y the y coordinate of the rectangle. width the width of the rectangle in pixels. height the height of the rectangle in pixels. isPressed true to draw the button border with a pressed-in appearance; false for normal (unpressed) appearance. isDefault true to draw the border with the appearance it has when hitting the enter key in a dialog will simulate a click to this button; false for normal appearance. shadow the shadow color. darkShadow a darker variant of the shadow color. highlight the highlight color. lightHighlight a brighter variant of the highlight color.

drawDashedRect

public static void drawDashedRect(Graphics g, int x, int y, int width, int height)
Draws a rectangle, simulating a dotted stroke by painting only every second pixel along the one-pixel thick edge. The color of those pixels is the current color of the Graphics g. Any other pixels are left unchanged.

[An illustration that shows which pixels
 get painted]

Parameters: g the graphics into which the rectangle is drawn. x the x coordinate of the rectangle. y the y coordinate of the rectangle. width the width of the rectangle in pixels. height the height of the rectangle in pixels.

drawEtchedRect

public static void drawEtchedRect(Graphics g, int x, int y, int width, int height, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
Draws a rectangle that appears etched into the surface, given four colors that are used for drawing.

[An illustration that shows which pixels
 get painted in what color]

Parameters: g the graphics into which the rectangle is drawn. x the x coordinate of the rectangle. y the y coordinate of the rectangle. width the width of the rectangle in pixels. height the height of the rectangle in pixels. shadow the color that will be used for painting the outer side of the top and left edges. darkShadow the color that will be used for painting the inner side of the top and left edges. highlight the color that will be used for painting the inner side of the bottom and right edges. lightHighlight the color that will be used for painting the outer side of the bottom and right edges.

See Also: getEtchedInsets EtchedBorder

drawGroove

public static void drawGroove(Graphics g, int x, int y, int width, int height, Color shadow, Color highlight)
Draws a rectangle that appears etched into the surface, given two colors that are used for drawing.

[An illustration that shows which pixels
 get painted in what color]

Parameters: g the graphics into which the rectangle is drawn. x the x coordinate of the rectangle. y the y coordinate of the rectangle. width the width of the rectangle in pixels. height the height of the rectangle in pixels. shadow the color that will be used for painting the outer side of the top and left edges, and for the inner side of the bottom and right ones. highlight the color that will be used for painting the inner side of the top and left edges, and for the outer side of the bottom and right ones.

See Also: getGrooveInsets EtchedBorder

drawLoweredBezel

public static void drawLoweredBezel(Graphics g, int x, int y, int width, int height, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)
Draws a rectangle that appears lowered into the surface, given four colors that are used for drawing.

[An illustration that shows which pixels
 get painted in what color]

Compatibility with the Sun reference implementation: The Sun reference implementation seems to ignore the x and y arguments, at least in JDK 1.3.1 and 1.4.1_01. The method always draws the rectangular area at location (0, 0). A bug report has been filed with Sun; its “bug ID” is 4880003. The GNU Classpath implementation behaves correctly, thus not replicating this bug.

Parameters: g the graphics into which the rectangle is drawn. x the x coordinate of the rectangle. y the y coordinate of the rectangle. width the width of the rectangle in pixels. height the height of the rectangle in pixels. shadow the color that will be used for painting the inner side of the top and left edges. darkShadow the color that will be used for painting the outer side of the top and left edges. highlight the color that will be used for painting the inner side of the bottom and right edges. lightHighlight the color that will be used for painting the outer side of the bottom and right edges.

drawString

public static void drawString(Graphics g, String text, int underlinedChar, int x, int y)
Draws a String at the given location, underlining the first occurence of a specified character. The algorithm for determining the underlined position is not sensitive to case. If the character is not part of text, the text will be drawn without underlining. Drawing is performed in the current color and font of g.

[An illustration showing how to use the
 method]

Parameters: g the graphics into which the String is drawn. text the String to draw. underlinedChar the character whose first occurence in text will be underlined. It is not clear why the API specification declares this argument to be of type int instead of char. While this would allow to pass Unicode characters outside Basic Multilingual Plane 0 (U+0000 .. U+FFFE), at least the GNU Classpath implementation does not underline anything if underlinedChar is outside the range of char. x the x coordinate of the text, as it would be passed to {@link java.awt.Graphics#drawString(java.lang.String, int, int)}. y the y coordinate of the text, as it would be passed to {@link java.awt.Graphics#drawString(java.lang.String, int, int)}.

drawStringUnderlineCharAt

public static void drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
Draws a String at the given location, underlining the character at the specified index. Drawing is performed in the current color and font of g.

[An illustration showing how to use the
 method]

Parameters: g the graphics into which the String is drawn. text the String to draw. underlinedIndex the index of the underlined character in text. If underlinedIndex falls outside the range [0, text.length() - 1], the text will be drawn without underlining anything. x the x coordinate of the text, as it would be passed to {@link java.awt.Graphics#drawString(java.lang.String, int, int)}. y the y coordinate of the text, as it would be passed to {@link java.awt.Graphics#drawString(java.lang.String, int, int)}.

Since: 1.4

getEtchedInsets

public static Insets getEtchedInsets()
Determines the width of the border that gets painted by {@link #drawEtchedRect}.

Returns: an Insets object whose top, left, bottom and right field contain the border width at the respective edge in pixels.

getGrooveInsets

public static Insets getGrooveInsets()
Determines the width of the border that gets painted by {@link #drawGroove}.

Returns: an Insets object whose top, left, bottom and right field contain the border width at the respective edge in pixels.

getPreferredButtonSize

public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
Determines the preferred width and height of an AbstractButton, given the gap between the button’s text and icon.

Parameters: b the button whose preferred size is determined. textIconGap the gap between the button’s text and icon.

Returns: a Dimension object whose width and height fields indicate the preferred extent in pixels.

See Also: SwingUtilities