public final class Graphics extends Object
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.
Modifier and Type | Method and Description |
---|---|
Object |
beginNativeGraphicsAccess()
Starts accessing the native graphics in the underlying OS, when accessing
the native graphics Codename One shouldn't be used! The native graphics is unclipped
and untranslated by default and its the responsibility of the caller to clip/translate
appropriately.
|
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 |
drawRect(int x,
int y,
int width,
int height,
int thickness)
Draws a rectangle in the given coordinates with the given thickness
|
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 |
drawShape(Shape shape,
Stroke stroke)
Draws a outline shape inside the specified bounding box.
|
void |
drawString(String str,
int x,
int y)
Draw a string using the current font and color in the x,y coordinates.
|
void |
drawString(String str,
int x,
int y,
int textDecoration)
Draw a string using the current font and color in the x,y coordinates.
|
void |
drawStringBaseline(String str,
int x,
int y)
Draws a string using baseline coordinates.
|
void |
drawStringBaseline(String str,
int x,
int y,
int textDecoration)
Draws a string using baseline coordinates.
|
void |
endNativeGraphicsAccess()
Invoke this to restore Codename One'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 |
fillShape(Shape shape)
Fills the given shape using the current alpha and color settings.
|
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
|
float |
getScaleX()
Returns the affine X scale
|
float |
getScaleY()
Returns the affine Y scale
|
Transform |
getTransform()
Gets the transformation matrix that is currently applied to this graphics context.
|
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.
|
boolean |
isPerspectiveTransformSupported()
Checks to see if perspective (3D)
com.codename1.ui.geom.Matrix transforms are supported by this graphics
context. |
boolean |
isShapeSupported()
Checks to see if this graphics context supports drawing shapes (i.e.
|
boolean |
isTransformSupported()
Checks to see if
com.codename1.ui.geom.Matrix transforms are supported by this graphics context. |
void |
lighterColor(int factor)
Makes the current color slightly lighter, this is useful for many visual effects
|
void |
popClip()
Pops the top clip from the clip stack and sets it as the current clip.
|
void |
pushClip()
Pushes the current clip onto the clip stack.
|
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 |
rotate(float angle,
int pivotX,
int pivotY)
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 |
setTransform(Transform transform)
Sets the transformation
com.codename1.ui.geom.Matrix to apply to drawing in this graphics context. |
void |
shear(float x,
float y)
Shear the graphics coordinate system using the affine transform
|
void |
tileImage(Image img,
int x,
int y,
int w,
int h)
Allows an implementation to optimize image tiling rendering logic
|
void |
translate(int x,
int y)
Translates the X/Y location for drawing on the underlying surface.
|
public void translate(int x, int y)
translate(-getTranslateX(), -getTranslateY())
x
- the x coordinatey
- the y coordinatepublic int getTranslateX()
public int getTranslateY()
public int getColor()
public void setColor(int RGB)
RGB
- the RGB value for the color.public Font getFont()
public void setFont(Font font)
font
- the font used with the drawString method callspublic int getClipX()
public int[] getClip()
public void setClip(int[] clip)
clip
- 4 element arraypublic int getClipY()
public int getClipWidth()
public int getClipHeight()
public void clipRect(int x, int y, int width, int height)
x
- the x coordinate of the rectangle to intersect the clip withy
- the y coordinate of the rectangle to intersect the clip withwidth
- the width of the rectangle to intersect the clip withheight
- the height of the rectangle to intersect the clip withpublic void setClip(int x, int y, int width, int height)
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.public void pushClip()
popClip()
.public void popClip()
public void drawLine(int x1, int y1, int x2, int y2)
x1
- first x positiony1
- first y positionx2
- second x positiony2
- second y positionpublic void fillRect(int x, int y, int width, int height)
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.public void drawRect(int x, int y, int width, int height)
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.public void drawRect(int x, int y, int width, int height, int thickness)
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.thickness
- the thickness in pixelspublic void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
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.public void lighterColor(int factor)
factor
- the degree of lightening a color per channel a number from 1 to 255public void darkerColor(int factor)
factor
- the degree of lightening a color per channel a number from 1 to 255public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
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.drawRoundRect(int, int, int, int, int, int)
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
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.public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
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.public void drawString(String str, int x, int y, int textDecoration)
str
- the string to be drawn.x
- the x coordinate.y
- the y coordinate.textDecoration
- Text decoration bitmask (See Style's TEXT_DECORATION_* constants)public void drawStringBaseline(String str, int x, int y)
str
- The string to be drawn.x
- The x-coordinate of the start of left edge of the text block.y
- The y-coordinate of the baseline of the text.drawString(java.lang.String, int, int)
public void drawStringBaseline(String str, int x, int y, int textDecoration)
str
- The string to be drawn.x
- The x-coordinate of the start of left edge of the text block.y
- The y-coordinate of the baseline of the text.textDecoration
- Text decoration bitmask (See Style's TEXT_DECORATION_* constants)drawString(java.lang.String, int, int, int)
public void drawString(String str, int x, int y)
str
- the string to be drawn.x
- the x coordinate.y
- the y coordinate.public void drawChar(char character, int x, int y)
character
- - the character to be drawnx
- the x coordinate of the baseline of the texty
- the y coordinate of the baseline of the textpublic void drawChars(char[] data, int offset, int length, int x, int y)
data
- the array of characters to be drawnoffset
- the start offset in the datalength
- the number of characters to be drawnx
- the x coordinate of the baseline of the texty
- the y coordinate of the baseline of the textpublic void drawImage(Image img, int x, int y)
img
- the specified image to be drawn. This method does
nothing if img is null.x
- the x coordinate.y
- the y coordinate.public void drawImage(Image img, int x, int y, int w, int h)
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 occupyh
- the height to occupypublic void drawShape(Shape shape, Stroke stroke)
This is not supported on
all platforms and contexts currently. Use isShapeSupported()
to check if the current
context supports drawing shapes.
shape
- The shape to be drawn.#setStroke
,
isShapeSupported()
public void fillShape(Shape shape)
This is not supported on
all platforms and contexts currently. Use isShapeSupported()
to check if the current
context supports drawing shapes.
shape
- The shape to be filled.isShapeSupported()
public boolean isTransformSupported()
com.codename1.ui.geom.Matrix
transforms are supported by this graphics context.com.codename1.ui.geom.Matrix
transforms.
Note that this method only confirms that 2D transforms are supported. If you need to perform 3D
transformations, you should use the isPerspectiveTransformSupported()
method.
setTransform(com.codename1.ui.Transform)
,
getTransform()
,
isPerspectiveTransformSupported()
public boolean isPerspectiveTransformSupported()
com.codename1.ui.geom.Matrix
transforms are supported by this graphics
context. If 3D transforms are supported, you can use a 4x4 transformation com.codename1.ui.geom.Matrix
via setTransform(com.codename1.ui.Transform)
to perform 3D transforms.
Note: It is possible for 3D transforms to not be supported but Affine (2D)
transforms to be supported. In this case you would be limited to a 3x3 transformation
matrix in setTransform(com.codename1.ui.Transform)
. You can check for 2D transformation support using the isTransformSupported()
method.
isTransformSupported()
,
setTransform(com.codename1.ui.Transform)
,
getTransform()
public boolean isShapeSupported()
drawShape(com.codename1.ui.geom.Shape, com.codename1.ui.Stroke)
and fillShape(com.codename1.ui.geom.Shape)
methods. If this returns false, and you call drawShape(com.codename1.ui.geom.Shape, com.codename1.ui.Stroke)
or fillShape(com.codename1.ui.geom.Shape)
, then
nothing will be drawn.public void setTransform(Transform transform)
com.codename1.ui.geom.Matrix
to apply to drawing in this graphics context.
In order to use this for 2D/Affine transformations you should first check to
make sure that transforms are supported by calling the isTransformSupported()
method. For 3D/Perspective transformations, you should first check to
make sure that 3D/Perspective transformations are supported by calling the
isPerspectiveTransformSupported()
.
Transformations are applied with (0,0) as the origin. So rotations and
scales are anchored at this point on the screen. You can use a different
anchor point by either embedding it in the transformation matrix (i.e. pre-transform the com.codename1.ui.geom.Matrix
to anchor at a different point)
or use the #setTransform(com.codename1.ui.geom.Matrix,int,int)
variation that allows you to explicitly set the
anchor point.
transform
- The transformation com.codename1.ui.geom.Matrix
to use for drawing. 2D/Affine transformations
can be achieved using a 3x3 transformation com.codename1.ui.geom.Matrix
. 3D/Perspective transformations
can be achieved using a 4x3 transformation com.codename1.ui.geom.Matrix
.isTransformSupported()
,
isPerspectiveTransformSupported()
,
#setTransform(com.codename1.ui.geom.Matrix,int,int)
public Transform getTransform()
setTransform(com.codename1.ui.Transform)
public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
x1
- the x coordinate of the first vertex of the triangley1
- the y coordinate of the first vertex of the trianglex2
- the x coordinate of the second vertex of the triangley2
- the y coordinate of the second vertex of the trianglex3
- the x coordinate of the third vertex of the triangley3
- the y coordinate of the third vertex of the trianglepublic void fillRadialGradient(int startColor, int endColor, int x, int y, int width, int height)
startColor
- the starting RGB colorendColor
- the ending RGB colorx
- the x coordinatey
- the y coordinatewidth
- the width of the region to be filledheight
- the height of the region to be filledpublic void fillRectRadialGradient(int startColor, int endColor, int x, int y, int width, int height, float relativeX, float relativeY, float relativeSize)
startColor
- the starting RGB colorendColor
- the ending RGB colorx
- the x coordinatey
- the y coordinatewidth
- the width of the region to be filledheight
- the height of the region to be filledrelativeX
- indicates the relative position of the gradient within the drawing regionrelativeY
- indicates the relative position of the gradient within the drawing regionrelativeSize
- indicates the relative size of the gradient within the drawing regionpublic void fillLinearGradient(int startColor, int endColor, int x, int y, int width, int height, boolean horizontal)
startColor
- the starting RGB colorendColor
- the ending RGB colorx
- the x coordinatey
- the y coordinatewidth
- the width of the region to be filledheight
- the height of the region to be filledhorizontal
- indicating wheter it is a horizontal fill or verticalpublic void fillRect(int x, int y, int w, int h, byte alpha)
x
- the x coordinate of the rectangle to be filledy
- the y coordinate of the rectangle to be filledw
- the width of the rectangle to be filledh
- the height of the rectangle to be filledalpha
- the alpha values specify semitransparencypublic void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
xPoints
- - a an array of x coordinates.yPoints
- - a an array of y coordinates.nPoints
- - a the total number of points.public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
xPoints
- - a an array of x coordinates.yPoints
- - a an array of y coordinates.nPoints
- - a the total number of points.public boolean isAlphaSupported()
public void setAlpha(int a)
a
- the alpha valuepublic int getAlpha()
public boolean isAntiAliasingSupported()
public boolean isAntiAliasedTextSupported()
public boolean isAntiAliased()
public void setAntiAliased(boolean a)
a
- true if anti aliasing is activepublic void setAntiAliasedText(boolean a)
a
- true if text anti aliasing is supportedpublic boolean isAntiAliasedText()
public boolean isAffineSupported()
public void resetAffine()
public void scale(float x, float y)
x
- scale factor for xy
- scale factor for ypublic void rotate(float angle)
angle
- the rotation angle in radianspublic void rotate(float angle, int pivotX, int pivotY)
angle
- the rotation angle in radianspivotX
- the pivot pointpivotY
- the pivot pointpublic void shear(float x, float y)
x
- shear factor for xy
- shear factor for ypublic Object beginNativeGraphicsAccess()
When finished with the native graphics it is essential to invoke endNativeGraphicsAccess
public void endNativeGraphicsAccess()
public void tileImage(Image img, int x, int y, int w, int h)
img
- the imagex
- coordinate to tile the image alongy
- coordinate to tile the image alongw
- coordinate to tile the image alongh
- coordinate to tile the image alongpublic float getScaleX()
public float getScaleY()