com.sun.lwuit
Class Graphics

java.lang.Object
  extended by com.sun.lwuit.Graphics

public final class Graphics
extends java.lang.Object

Abstracts the underlying platform graphics context thus allowing us to achieve portability between MIDP devices and CDC devices. This abstaction simplifies and unifies the Graphics implementations of various platforms.

A graphics instance should never be created by the developer and is always accessed using either a paint callback or a mutable image. There is no supported way to create this object directly.


Method Summary
 java.lang.Object beginNativeGraphicsAccess()
          Starts accessing the native graphics in the underlying OS, when accessing the native graphics LWUIT shouldn't be used!
 void clipRect(int x, int y, int width, int height)
          Clips the given rectangle by intersecting with the current clipping region, this method can thus only shrink the clipping region and never increase it.
 void darkerColor(int factor)
          Makes the current color slightly darker, this is useful for many visual effects
 void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
          Draws a circular or elliptical arc based on the given angles and bounding box
 void drawChar(char character, int x, int y)
          Draw the given char using the current font and color in the x,y coordinates.
 void drawChars(char[] data, int offset, int length, int x, int y)
          Draw the given char array using the current font and color in the x,y coordinates.
 void drawImage(Image img, int x, int y)
          Draws the image so its top left coordinate corresponds to x/y
 void drawImage(Image img, int x, int y, int w, int h)
          Draws the image so its top left coordinate corresponds to x/y and scales it to width/height
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a line between the 2 X/Y coordinates
 void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
          Draws a closed polygon defined by arrays of x and y coordinates.
 void drawRect(int x, int y, int width, int height)
          Draws a rectangle in the given coordinates
 void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Draws a rounded corner rectangle in the given coordinates with the arcWidth/height matching the last two arguments respectively.
 void drawString(java.lang.String str, int x, int y)
          Draw a string using the current font and color in the x,y coordinates.
 void drawString(java.lang.String str, int x, int y, int textDecoration)
          Draw a string using the current font and color in the x,y coordinates.
 void endNativeGraphicsAccess()
          Invoke this to restore LWUIT's graphics settings into the native graphics
 void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
          Fills a circular or elliptical arc based on the given angles and bounding box.
 void fillLinearGradient(int startColor, int endColor, int x, int y, int width, int height, boolean horizontal)
          Draws a linear gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient
 void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
          Fills a closed polygon defined by arrays of x and y coordinates.
 void fillRadialGradient(int startColor, int endColor, int x, int y, int width, int height)
          Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient.
 void fillRect(int x, int y, int width, int height)
          Fills the rectangle from the given position according to the width/height minus 1 pixel according to the convention in Java.
 void fillRect(int x, int y, int w, int h, byte alpha)
          Fills a rectangle with an optionally translucent fill color
 void fillRectRadialGradient(int startColor, int endColor, int x, int y, int width, int height, float relativeX, float relativeY, float relativeSize)
          Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient.
 void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Fills a rounded rectangle in the same way as drawRoundRect
 void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
          Draws a filled triangle with the given coordinates
 int getAlpha()
          Returnes the alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent
 int[] getClip()
          Returns the clip as an x,y,w,h array
 int getClipHeight()
          Returns the clip height
 int getClipWidth()
          Returns the clip width
 int getClipX()
          Returns the x clipping position
 int getClipY()
          Returns the y clipping position
 int getColor()
          Returns the current color
 Font getFont()
          Returns the font used with the drawString method calls
 int getTranslateX()
          Returns the current x translate value
 int getTranslateY()
          Returns the current y translate value
 boolean isAffineSupported()
          Indicates whether the underlying implementation can draw using an affine transform hence methods such as rotate, scale and shear would work
 boolean isAlphaSupported()
          Indicates whether invoking set/getAlpha would have an effect on all further rendering from this graphics object.
 boolean isAntiAliased()
          Returns true if anti-aliasing for standard rendering operations is turned on.
 boolean isAntiAliasedText()
          Indicates whether anti-aliasing for text is active, notice that text anti-aliasing is a separate attribute from standard anti-alisaing.
 boolean isAntiAliasedTextSupported()
          Returns true if anti-aliasing for text is supported, notice that text anti-aliasing is a separate attribute from standard anti-alisaing.
 boolean isAntiAliasingSupported()
          Returns true if anti-aliasing for standard rendering operations is supported, notice that text anti-aliasing is a separate attribute.
 void lighterColor(int factor)
          Makes the current color slightly lighter, this is useful for many visual effects
 void resetAffine()
          Resets the affine transform to the default value
 void rotate(float angle)
          Rotates the coordinate system around a radian angle using the affine transform
 void scale(float x, float y)
          Scales the coordinate system using the affine transform
 void setAlpha(int a)
          Sets alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent
 void setAntiAliased(boolean a)
          Set whether anti-aliasing for standard rendering operations is turned on.
 void setAntiAliasedText(boolean a)
          Set whether anti-aliasing for text is active, notice that text anti-aliasing is a separate attribute from standard anti-alisaing.
 void setClip(int[] clip)
          Sets the clip from an array containing x, y, width, height value
 void setClip(int x, int y, int width, int height)
          Updates the clipping region to match the given region exactly
 void setColor(int RGB)
          Sets the current rgb color while ignoring any potential alpha component within said color value.
 void setFont(Font font)
          Sets the font to use with the drawString method calls
 void shear(float x, float y)
          Shear the graphics coordinate system using the affine transform
 void translate(int x, int y)
          Translates the X/Y location for drawing on the underlying surface.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

translate

public void translate(int x,
                      int y)
Translates the X/Y location for drawing on the underlying surface. Translation is incremental so the new value will be added to the current translation and in order to reset translation we have to invoke translate(-getTranslateX(), -getTranslateY())

Parameters:
x - the x coordinate
y - the y coordinate

getTranslateX

public int getTranslateX()
Returns the current x translate value

Returns:
the current x translate value

getTranslateY

public int getTranslateY()
Returns the current y translate value

Returns:
the current y translate value

getColor

public int getColor()
Returns the current color

Returns:
the RGB graphics color

setColor

public void setColor(int RGB)
Sets the current rgb color while ignoring any potential alpha component within said color value.

Parameters:
RGB - the RGB value for the color.

getFont

public Font getFont()
Returns the font used with the drawString method calls

Returns:
the font used with the drawString method calls

setFont

public void setFont(Font font)
Sets the font to use with the drawString method calls

Parameters:
font - the font used with the drawString method calls

getClipX

public int getClipX()
Returns the x clipping position

Returns:
the x clipping position

getClip

public int[] getClip()
Returns the clip as an x,y,w,h array

Returns:
clip array copy

setClip

public void setClip(int[] clip)
Sets the clip from an array containing x, y, width, height value

Parameters:
clip - 4 element array

getClipY

public int getClipY()
Returns the y clipping position

Returns:
the y clipping position

getClipWidth

public int getClipWidth()
Returns the clip width

Returns:
the clip width

getClipHeight

public int getClipHeight()
Returns the clip height

Returns:
the clip height

clipRect

public void clipRect(int x,
                     int y,
                     int width,
                     int height)
Clips the given rectangle by intersecting with the current clipping region, this method can thus only shrink the clipping region and never increase it.

Parameters:
x - the x coordinate of the rectangle to intersect the clip with
y - the y coordinate of the rectangle to intersect the clip with
width - the width of the rectangle to intersect the clip with
height - the height of the rectangle to intersect the clip with

setClip

public void setClip(int x,
                    int y,
                    int width,
                    int height)
Updates the clipping region to match the given region exactly

Parameters:
x - the x coordinate of the new clip rectangle.
y - the y coordinate of the new clip rectangle.
width - the width of the new clip rectangle.
height - the height of the new clip rectangle.

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a line between the 2 X/Y coordinates

Parameters:
x1 - first x position
y1 - first y position
x2 - second x position
y2 - second y position

fillRect

public void fillRect(int x,
                     int y,
                     int width,
                     int height)
Fills the rectangle from the given position according to the width/height minus 1 pixel according to the convention in Java.

Parameters:
x - the x coordinate of the rectangle to be filled.
y - the y coordinate of the rectangle to be filled.
width - the width of the rectangle to be filled.
height - the height of the rectangle to be filled.

drawRect

public void drawRect(int x,
                     int y,
                     int width,
                     int height)
Draws a rectangle in the given coordinates

Parameters:
x - the x coordinate of the rectangle to be drawn.
y - the y coordinate of the rectangle to be drawn.
width - the width of the rectangle to be drawn.
height - the height of the rectangle to be drawn.

drawRoundRect

public void drawRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Draws a rounded corner rectangle in the given coordinates with the arcWidth/height matching the last two arguments respectively.

Parameters:
x - the x coordinate of the rectangle to be drawn.
y - the y coordinate of the rectangle to be drawn.
width - the width of the rectangle to be drawn.
height - the height of the rectangle to be drawn.
arcWidth - the horizontal diameter of the arc at the four corners.
arcHeight - the vertical diameter of the arc at the four corners.

lighterColor

public void lighterColor(int factor)
Makes the current color slightly lighter, this is useful for many visual effects

Parameters:
factor - the degree of lightening a color per channel a number from 1 to 255

darkerColor

public void darkerColor(int factor)
Makes the current color slightly darker, this is useful for many visual effects

Parameters:
factor - the degree of lightening a color per channel a number from 1 to 255

fillRoundRect

public void fillRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Fills a rounded rectangle in the same way as drawRoundRect

Parameters:
x - the x coordinate of the rectangle to be filled.
y - the y coordinate of the rectangle to be filled.
width - the width of the rectangle to be filled.
height - the height of the rectangle to be filled.
arcWidth - the horizontal diameter of the arc at the four corners.
arcHeight - the vertical diameter of the arc at the four corners.
See Also:
drawRoundRect(int, int, int, int, int, int)

fillArc

public void fillArc(int x,
                    int y,
                    int width,
                    int height,
                    int startAngle,
                    int arcAngle)
Fills a circular or elliptical arc based on the given angles and bounding box. The resulting arc begins at startAngle and extends for arcAngle degrees.

Parameters:
x - the x coordinate of the upper-left corner of the arc to be filled.
y - the y coordinate of the upper-left corner of the arc to be filled.
width - the width of the arc to be filled.
height - the height of the arc to be filled.
startAngle - the beginning angle.
arcAngle - the angular extent of the arc, relative to the start angle.

drawArc

public void drawArc(int x,
                    int y,
                    int width,
                    int height,
                    int startAngle,
                    int arcAngle)
Draws a circular or elliptical arc based on the given angles and bounding box

Parameters:
x - the x coordinate of the upper-left corner of the arc to be drawn.
y - the y coordinate of the upper-left corner of the arc to be drawn.
width - the width of the arc to be drawn.
height - the height of the arc to be drawn.
startAngle - the beginning angle.
arcAngle - the angular extent of the arc, relative to the start angle.

drawString

public void drawString(java.lang.String str,
                       int x,
                       int y,
                       int textDecoration)
Draw a string using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

Parameters:
str - the string to be drawn.
x - the x coordinate.
y - the y coordinate.
textDecoration - Text decoration bitmask (See Style's TEXT_DECORATION_* constants)

drawString

public void drawString(java.lang.String str,
                       int x,
                       int y)
Draw a string using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

Parameters:
str - the string to be drawn.
x - the x coordinate.
y - the y coordinate.

drawChar

public void drawChar(char character,
                     int x,
                     int y)
Draw the given char using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

Parameters:
character - - the character to be drawn
x - the x coordinate of the baseline of the text
y - the y coordinate of the baseline of the text

drawChars

public void drawChars(char[] data,
                      int offset,
                      int length,
                      int x,
                      int y)
Draw the given char array using the current font and color in the x,y coordinates. The font is drawn from the top position and not the baseline.

Parameters:
data - the array of characters to be drawn
offset - the start offset in the data
length - the number of characters to be drawn
x - the x coordinate of the baseline of the text
y - the y coordinate of the baseline of the text

drawImage

public void drawImage(Image img,
                      int x,
                      int y)
Draws the image so its top left coordinate corresponds to x/y

Parameters:
img - the specified image to be drawn. This method does nothing if img is null.
x - the x coordinate.
y - the y coordinate.

drawImage

public void drawImage(Image img,
                      int x,
                      int y,
                      int w,
                      int h)
Draws the image so its top left coordinate corresponds to x/y and scales it to width/height

Parameters:
img - the specified image to be drawn. This method does nothing if img is null.
x - the x coordinate.
y - the y coordinate.
w - the width to occupy
h - the height to occupy

fillTriangle

public void fillTriangle(int x1,
                         int y1,
                         int x2,
                         int y2,
                         int x3,
                         int y3)
Draws a filled triangle with the given coordinates

Parameters:
x1 - the x coordinate of the first vertex of the triangle
y1 - the y coordinate of the first vertex of the triangle
x2 - the x coordinate of the second vertex of the triangle
y2 - the y coordinate of the second vertex of the triangle
x3 - the x coordinate of the third vertex of the triangle
y3 - the y coordinate of the third vertex of the triangle

fillRadialGradient

public void fillRadialGradient(int startColor,
                               int endColor,
                               int x,
                               int y,
                               int width,
                               int height)
Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient. Notice that a radial gradient will result in a circular shape, to create a square use fillRect or draw a larger shape and clip to the appropriate size.

Parameters:
startColor - the starting RGB color
endColor - the ending RGB color
x - the x coordinate
y - the y coordinate
width - the width of the region to be filled
height - the height of the region to be filled

fillRectRadialGradient

public void fillRectRadialGradient(int startColor,
                                   int endColor,
                                   int x,
                                   int y,
                                   int width,
                                   int height,
                                   float relativeX,
                                   float relativeY,
                                   float relativeSize)
Draws a radial gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient. Notice that this method differs from fillRadialGradient since it draws a sqare gradient at all times and can thus be cached Notice that a radial gradient will result in a circular shape, to create a square use fillRect or draw a larger shape and clip to the appropriate size.

Parameters:
startColor - the starting RGB color
endColor - the ending RGB color
x - the x coordinate
y - the y coordinate
width - the width of the region to be filled
height - the height of the region to be filled
relativeX - indicates the relative position of the gradient within the drawing region
relativeY - indicates the relative position of the gradient within the drawing region
relativeSize - indicates the relative size of the gradient within the drawing region

fillLinearGradient

public void fillLinearGradient(int startColor,
                               int endColor,
                               int x,
                               int y,
                               int width,
                               int height,
                               boolean horizontal)
Draws a linear gradient in the given coordinates with the given colors, doesn't take alpha into consideration when drawing the gradient

Parameters:
startColor - the starting RGB color
endColor - the ending RGB color
x - the x coordinate
y - the y coordinate
width - the width of the region to be filled
height - the height of the region to be filled
horizontal - indicating wheter it is a horizontal fill or vertical

fillRect

public void fillRect(int x,
                     int y,
                     int w,
                     int h,
                     byte alpha)
Fills a rectangle with an optionally translucent fill color

Parameters:
x - the x coordinate of the rectangle to be filled
y - the y coordinate of the rectangle to be filled
w - the width of the rectangle to be filled
h - the height of the rectangle to be filled
alpha - the alpha values specify semitransparency

fillPolygon

public void fillPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point.

Parameters:
xPoints - - a an array of x coordinates.
yPoints - - a an array of y coordinates.
nPoints - - a the total number of points.

drawPolygon

public void drawPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point.

Parameters:
xPoints - - a an array of x coordinates.
yPoints - - a an array of y coordinates.
nPoints - - a the total number of points.

isAlphaSupported

public boolean isAlphaSupported()
Indicates whether invoking set/getAlpha would have an effect on all further rendering from this graphics object.

Returns:
false if setAlpha has no effect true if it applies to everything some effect

setAlpha

public void setAlpha(int a)
Sets alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent

Parameters:
a - the alpha value

getAlpha

public int getAlpha()
Returnes the alpha as a value between 0-255 (0 - 0xff) where 255 is completely opaque and 0 is completely transparent

Returns:
the alpha value

isAntiAliasingSupported

public boolean isAntiAliasingSupported()
Returns true if anti-aliasing for standard rendering operations is supported, notice that text anti-aliasing is a separate attribute.

Returns:
true if anti aliasing is supported

isAntiAliasedTextSupported

public boolean isAntiAliasedTextSupported()
Returns true if anti-aliasing for text is supported, notice that text anti-aliasing is a separate attribute from standard anti-alisaing.

Returns:
true if text anti aliasing is supported

isAntiAliased

public boolean isAntiAliased()
Returns true if anti-aliasing for standard rendering operations is turned on.

Returns:
true if anti aliasing is active

setAntiAliased

public void setAntiAliased(boolean a)
Set whether anti-aliasing for standard rendering operations is turned on.

Parameters:
a - true if anti aliasing is active

setAntiAliasedText

public void setAntiAliasedText(boolean a)
Set whether anti-aliasing for text is active, notice that text anti-aliasing is a separate attribute from standard anti-alisaing.

Parameters:
a - true if text anti aliasing is supported

isAntiAliasedText

public boolean isAntiAliasedText()
Indicates whether anti-aliasing for text is active, notice that text anti-aliasing is a separate attribute from standard anti-alisaing.

Returns:
true if text anti aliasing is supported

isAffineSupported

public boolean isAffineSupported()
Indicates whether the underlying implementation can draw using an affine transform hence methods such as rotate, scale and shear would work

Returns:
true if an affine transformation matrix is present

resetAffine

public void resetAffine()
Resets the affine transform to the default value


scale

public void scale(float x,
                  float y)
Scales the coordinate system using the affine transform

Parameters:
x - scale factor for x
y - scale factor for y

rotate

public void rotate(float angle)
Rotates the coordinate system around a radian angle using the affine transform

Parameters:
angle - the rotation angle in radians

shear

public void shear(float x,
                  float y)
Shear the graphics coordinate system using the affine transform

Parameters:
x - shear factor for x
y - shear factor for y

beginNativeGraphicsAccess

public java.lang.Object beginNativeGraphicsAccess()
Starts accessing the native graphics in the underlying OS, when accessing the native graphics LWUIT shouldn't be used! The native graphics is unclipped and untranslated by default and its the responsibility of the caller to clip/translate appropriately.

When finished with the native graphics it is essential to invoke endNativeGraphicsAccess

Returns:
an instance of the underlying native graphics object

endNativeGraphicsAccess

public void endNativeGraphicsAccess()
Invoke this to restore LWUIT's graphics settings into the native graphics