APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing.plaf.basic

public Class BasicGraphicsUtils

extends Object
Class Inheritance
Imports
java.awt.Color, .Component, .Dimension, .Font, .FontMetrics, .Graphics, .Graphics2D, .Insets, .Rectangle, .Toolkit, java.awt.event.InputEvent, javax.swing.AbstractButton, .Icon, .JComponent, .SwingUtilities, sun.swing.SwingUtilities2

Convenient util class.
Author
Hans Muller

Field Summary

Modifier and TypeField and Description
private static final Insets
private static final Insets

Constructor Summary

AccessConstructor and Description
public
BasicGraphicsUtils()

Constructs a BasicGraphicsUtils.

Method Summary

Modifier and TypeMethod and Description
public static void
drawBezel(Graphics
an instance of Graphics
g
,
int
an X coordinate
x
,
int
an Y coordinate
y
,
int
a width
w
,
int
a height
h
,
boolean
is component pressed
isPressed
,
boolean
is default drawing
isDefault
,
Color
a color of shadow
shadow
,
Color
a color of dark shadow
darkShadow
,
Color
a color highlighting
highlight
,
Color
a color of light highlighting
lightHighlight
)

Draws a bezel.

public static void
drawDashedRect(Graphics
an instance of Graphics
g
,
int
an X coordinate
x
,
int
an Y coordinate
y
,
int
a width of rectangle
width
,
int
a height of rectangle
height
)

Draws dashed rectangle.

public static void
drawEtchedRect(Graphics
an instance of Graphics
g
,
int
an X coordinate
x
,
int
an Y coordinate
y
,
int
a width
w
,
int
a height
h
,
Color
a color of shadow
shadow
,
Color
a color of dark shadow
darkShadow
,
Color
a color highlighting
highlight
,
Color
a color of light highlighting
lightHighlight
)

Draws an etched rectangle.

public static void
drawGroove(Graphics
an instance of Graphics
g
,
int
an X coordinate
x
,
int
an Y coordinate
y
,
int
a width
w
,
int
a height
h
,
Color
a color of shadow
shadow
,
Color
a color highlighting
highlight
)

Draws a groove.

public static void
drawLoweredBezel(Graphics
an instance of Graphics
g
,
int
an X coordinate
x
,
int
an Y coordinate
y
,
int
a width
w
,
int
a height
h
,
Color
a color of shadow
shadow
,
Color
a color of dark shadow
darkShadow
,
Color
a color highlighting
highlight
,
Color
a color of light highlighting
lightHighlight
)

Draws a lowered bezel.

public static void
drawString(Graphics
an instance of Graphics
g
,
String
a text
text
,
int
an underlined char
underlinedChar
,
int
an X coordinate
x
,
int
an Y coordinate
y
)

Draw a string with the graphics g at location (x,y) just like g.drawString would.

public static void
drawString(JComponent
the component that will display the string, may be null
c
,
Graphics2D
the graphics context, must not be null
g
,
String
the string to display, may be null
string
,
float
the x coordinate to draw the text at
x
,
float
the y coordinate to draw the text at
y
)

Draws the given string at the specified location using text properties and anti-aliasing hints from the provided component.

public static void
drawStringUnderlineCharAt(Graphics
Graphics to draw with
g
,
String
String to draw
text
,
int
Index of character in text to underline
underlinedIndex
,
int
x coordinate to draw at
x
,
int
y coordinate to draw at
y
)

Draw a string with the graphics g at location (x, y) just like g.drawString would.

public static void
drawStringUnderlineCharAt(JComponent
the component that will display the string, may be null
c
,
Graphics2D
the graphics context, must not be null
g
,
String
the string to display, may be null
string
,
int
index of a char value (Unicode code unit) in the string to underline
underlinedIndex
,
float
the x coordinate to draw the text at
x
,
float
the y coordinate to draw the text at
y
)

Draws the given string at the specified location underlining the specified character.

public static String

Returns:

the clipped string that fits in the provided space, an empty string if the given string argument is null or empty
getClippedString
(JComponent
the component, may be null
c
,
FontMetrics
the FontMetrics used to measure the string width, must be obtained from the correct font and graphics. Must not be null.
fm
,
String
the string to clip, may be null
string
,
int
the amount of space that the string can be drawn in
availTextWidth
)

Clips the passed in string to the space provided.

public static Insets

Returns:

the inset of an etched rect
getEtchedInsets
()

Returns the amount of space taken up by a border drawn by drawEtchedRect()

public static Insets

Returns:

the inset of a groove border
getGrooveInsets
()

Returns the amount of space taken up by a border drawn by drawGroove()

public static Dimension

Returns:

the preferred size of the button
getPreferredButtonSize
(AbstractButton
an instance of AbstractButton
b
,
int
a gap between text and icon
textIconGap
)

Returns the preferred size of the button.

public static float

Returns:

the advance width of the specified string, zero is returned for null string
getStringWidth
(JComponent
the component, may be null
c
,
FontMetrics
the FontMetrics used to measure the advance string width, must be obtained from the correct font and graphics. Must not be null.
fm
,
String
the string to get the advance width of, may be null
string
)

Returns the width of the passed in string using text properties and anti-aliasing hints from the provided component.

pack-priv static boolean
pack-priv static boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ETCHED_INSETSback to summary
private static final Insets ETCHED_INSETS
GROOVE_INSETSback to summary
private static final Insets GROOVE_INSETS

Constructor Detail

BasicGraphicsUtilsback to summary
public BasicGraphicsUtils()

Constructs a BasicGraphicsUtils.

Method Detail

drawBezelback to summary
public static void drawBezel(Graphics g, int x, int y, int w, int h, boolean isPressed, boolean isDefault, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)

Draws a bezel.

Parameters
g:Graphics

an instance of Graphics

x:int

an X coordinate

y:int

an Y coordinate

w:int

a width

h:int

a height

isPressed:boolean

is component pressed

isDefault:boolean

is default drawing

shadow:Color

a color of shadow

darkShadow:Color

a color of dark shadow

highlight:Color

a color highlighting

lightHighlight:Color

a color of light highlighting

drawDashedRectback to summary
public static void drawDashedRect(Graphics g, int x, int y, int width, int height)

Draws dashed rectangle.

Parameters
g:Graphics

an instance of Graphics

x:int

an X coordinate

y:int

an Y coordinate

width:int

a width of rectangle

height:int

a height of rectangle

drawEtchedRectback to summary
public static void drawEtchedRect(Graphics g, int x, int y, int w, int h, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)

Draws an etched rectangle.

Parameters
g:Graphics

an instance of Graphics

x:int

an X coordinate

y:int

an Y coordinate

w:int

a width

h:int

a height

shadow:Color

a color of shadow

darkShadow:Color

a color of dark shadow

highlight:Color

a color highlighting

lightHighlight:Color

a color of light highlighting

drawGrooveback to summary
public static void drawGroove(Graphics g, int x, int y, int w, int h, Color shadow, Color highlight)

Draws a groove.

Parameters
g:Graphics

an instance of Graphics

x:int

an X coordinate

y:int

an Y coordinate

w:int

a width

h:int

a height

shadow:Color

a color of shadow

highlight:Color

a color highlighting

drawLoweredBezelback to summary
public static void drawLoweredBezel(Graphics g, int x, int y, int w, int h, Color shadow, Color darkShadow, Color highlight, Color lightHighlight)

Draws a lowered bezel.

Parameters
g:Graphics

an instance of Graphics

x:int

an X coordinate

y:int

an Y coordinate

w:int

a width

h:int

a height

shadow:Color

a color of shadow

darkShadow:Color

a color of dark shadow

highlight:Color

a color highlighting

lightHighlight:Color

a color of light highlighting

drawStringback to summary
public static void drawString(Graphics g, String text, int underlinedChar, int x, int y)

Draw a string with the graphics g at location (x,y) just like g.drawString would. The first occurrence of underlineChar in text will be underlined. The matching algorithm is not case sensitive.

Parameters
g:Graphics

an instance of Graphics

text:String

a text

underlinedChar:int

an underlined char

x:int

an X coordinate

y:int

an Y coordinate

drawStringback to summary
public static void drawString(JComponent c, Graphics2D g, String string, float x, float y)

Draws the given string at the specified location using text properties and anti-aliasing hints from the provided component. Nothing is drawn for the null string.

Parameters
c:JComponent

the component that will display the string, may be null

g:Graphics2D

the graphics context, must not be null

string:String

the string to display, may be null

x:float

the x coordinate to draw the text at

y:float

the y coordinate to draw the text at

Exceptions
NullPointerException:
if the specified g is null
Since
9
drawStringUnderlineCharAtback to summary
public static void drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)

Draw a string with the graphics g at location (x, y) just like g.drawString would. The character at index underlinedIndex in text will be underlined. If index is beyond the bounds of text (including < 0), nothing will be underlined.

Parameters
g:Graphics

Graphics to draw with

text:String

String to draw

underlinedIndex:int

Index of character in text to underline

x:int

x coordinate to draw at

y:int

y coordinate to draw at

Since
1.4
drawStringUnderlineCharAtback to summary
public static void drawStringUnderlineCharAt(JComponent c, Graphics2D g, String string, int underlinedIndex, float x, float y)

Draws the given string at the specified location underlining the specified character. The provided component is used to query text properties and anti-aliasing hints.

The underlinedIndex parameter points to a char value (Unicode code unit) in the given string. If the char value specified at the underlined index is in the high-surrogate range and the char value at the following index is in the low-surrogate range then the supplementary character corresponding to this surrogate pair is underlined.

No character is underlined if the index is negative or greater than the string length (index < 0 || index >= string.length()) or if the char value specified at the given index is in the low-surrogate range.

Parameters
c:JComponent

the component that will display the string, may be null

g:Graphics2D

the graphics context, must not be null

string:String

the string to display, may be null

underlinedIndex:int

index of a char value (Unicode code unit) in the string to underline

x:float

the x coordinate to draw the text at

y:float

the y coordinate to draw the text at

Exceptions
NullPointerException:
if the specified g is null
Since
9
See Also
getStringWidth
getClippedStringback to summary
public static String getClippedString(JComponent c, FontMetrics fm, String string, int availTextWidth)

Clips the passed in string to the space provided. The provided component is used to query text properties and anti-aliasing hints. The unchanged string is returned if the space provided is greater than the string width.

Parameters
c:JComponent

the component, may be null

fm:FontMetrics

the FontMetrics used to measure the string width, must be obtained from the correct font and graphics. Must not be null.

string:String

the string to clip, may be null

availTextWidth:int

the amount of space that the string can be drawn in

Returns:String

the clipped string that fits in the provided space, an empty string if the given string argument is null or empty

Exceptions
NullPointerException:
if the specified fm is null
Since
9
See Also
getStringWidth
getEtchedInsetsback to summary
public static Insets getEtchedInsets()

Returns the amount of space taken up by a border drawn by drawEtchedRect()

Returns:Insets

the inset of an etched rect

getGrooveInsetsback to summary
public static Insets getGrooveInsets()

Returns the amount of space taken up by a border drawn by drawGroove()

Returns:Insets

the inset of a groove border

getPreferredButtonSizeback to summary
public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)

Returns the preferred size of the button.

Parameters
b:AbstractButton

an instance of AbstractButton

textIconGap:int

a gap between text and icon

Returns:Dimension

the preferred size of the button

getStringWidthback to summary
public static float getStringWidth(JComponent c, FontMetrics fm, String string)

Returns the width of the passed in string using text properties and anti-aliasing hints from the provided component. If the passed string is null, returns zero.

Parameters
c:JComponent

the component, may be null

fm:FontMetrics

the FontMetrics used to measure the advance string width, must be obtained from the correct font and graphics. Must not be null.

string:String

the string to get the advance width of, may be null

Returns:float

the advance width of the specified string, zero is returned for null string

Exceptions
NullPointerException:
if the specified fm is null
Since
9
isLeftToRightback to summary
pack-priv static boolean isLeftToRight(Component c)
isMenuShortcutKeyDownback to summary
pack-priv static boolean isMenuShortcutKeyDown(InputEvent event)