public class

Graphics

extends Object
implements Anchor
java.lang.Object
   ↳ com.neomades.graphics.Graphics

Class Overview

The Graphics class handles all the drawing primitives functions that can be called from the paint method of a CanvasScreen. Those drawing methods are similar to MIDP:

  • Line based primitives such as Lines, Rectangles, Round Rectangles, Circles, Arcs and Triangles.
  • Filled primitives such as Filled Rectangle, Filled Round Rectangles, Filled Circles, Filled Arcs and Filled Triangles.
  • Chars and Strings.
  • Full images, Regions of images or image as RGB integer array.

There are also setters to change states of various elements during the drawing:

  • Draw colors for filled operations and font drawings
  • Current Font aspect
  • Color Blending activation/de-activation for Images
  • Translation of the Screen
  • Clipping of the Screen

Neomad graphics generality and philosophy:

Neomad graphics is power by openGLES and as a consequence the Graphics class inherit from all the pros and cons of OpenGLES. Thus, all drawing methods have to be used gently with good practices to not overflow communication between CPU and GPU. If one part in the rendering of the drawing area is costly, all the rendering will be penalized, and so the application.

Neomad handles the case where images are lost after a Suspend/Resume and will automatically re-upload them when the Suspend/Resume is over. You do not have to handle it yourself. To sort out this known issue with some technologies, the selected solution is to re-upload the necessary images just before the 1st drawing after the Suspend/Resume is finished (in order not to reload all textures just after the Suspend/Resume and impose a potentially long loading time before re-accessing your application). The only side effect using this method might be a few hiccups during a few seconds coming back from the Suspend/Resume but the performances should stabilize themselves quickly.

How to maximize performances of your application?

All the following needs to be taken into consideration when creating your graphic engine or needs to be considered in order to port your game from another technology to Neomad carefully. Mainly, to maximize performances, the drawing methods must be called grouped (if possible) by categories written above. More precisely, it is a good advice to regroup drawing call in three parts:

  • Line based and filled geometric primitives.
  • Chars and String
  • Images drawing

The clear(int) method must be called at the beginning of the paint method to clear the previous drawings before a new one.

Engine/Technology limitations and warnings:

Line and filled based primitives: It is very important to understand that OpenGLES isn't really designed to maximize performances for Line based primitives and filled primitives. Those functions are nevertheless present for compatibility reasons and should be efficient enough to keep performances but they aren't the core features OpenGLES is efficient with.

Image primitives: In order to draw Images and Regions, the methods use OpenGL textures to render images. The texture is created in the Image object constructor and the image data are uploaded on the GPU before the next following paint call. Please refer to the Image section to read more about OpenGLES Texture constraints. For a big number of image creations, it is a good practice to spread the texture creation/upload on several paints in order to prevent the application from running out of memory. Keep in mind that there is a need to allocate temporary heap bitmap memory to store the image to upload during the next paint. If you load a lot of Images at the time, the Bitmap memory can be easily overloaded.

To reach the best performances with Images and Regions of images, the best is to regrouping drawing calls using the same image to avoid texture swapping. The best is to create several big textures (1024x1024 for instance) containing multiple images and transferred to screen using drawRegion operations. Regroup the texture using the order they are drawn on the screen as much as you can and try only to swap the textures when necessary.

Memory management: Depending on the Image you create, the Bitmap memory might or might not be recycled (and may stay in the main heap bitmap memory). Only Images coming from resources are recycled (i.e. only images loaded with the resource manager). Otherwise, the bitmap is kept in memory in order to reload textures in case of application Suspend/Resume. So loading images by resource manager is preferable for a minimum of memory usage.

String and Char functions: Draw Char and String: these methods are slower than others since the hardware rendering do not support font rendering natively. Drawing a string has the same cost than drawing a Char so draw char method should not used to draw string. Each string is loaded on the GPU as a texture and a String cache is used to optimize the rendering of string that doesn't change between frames. If you want to draw a String once every 2 frames, the best is to keep the call to draw the string but drawing it off-screen to keep the string in the caches (this will save a Bitmap/Texture creation when the String is displayed again).

Garbage collection

In order to avoid garbage collecting, do not use the new keyword in the paint(Graphics) method implementation. For example, do not call "new Point(x, y)" as parameter of graphics methods.

DrawRGB/GetRGB functions: The drawRGB() method is very slow since the data has do be transfer to the GPU at each rendering call to the paint method, passing by the Image class guarantees better performance.

Summary

Constants
int DOTTED
int SOLID
[Expand]
Inherited Constants
From interface com.neomades.graphics.Anchor
Public Methods
void clear(Color color)
Clears the entire graphics area to the parameter color.
void clear(int color)
Clear the screen with the given ARGB color.
void clipRect(int x, int y, int width, int height)
Set a clip region in the Screen.
void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Draws the outline of a circular or elliptical arc covering the specified rectangle.
void drawArc(Rect rectangle, int startAngle, int arcAngle)
Draws the outline of a circular or elliptical arc covering the specified rectangle, using the current color and stroke style.
void drawChar(char character, int x, int y, int anchor)
This method draw a char at screen coordinates x and y according to anchor.
void drawChar(char character, Point location, int anchor)
Draws the specified character using the current font and color.
void drawChars(char[] data, int offset, int length, int x, int y, int anchor)
This method draw length char of an array of char starting from position offset at screen coordinates x and y according to anchor.
void drawChars(char[] data, int offset, int length, Point position, int anchor)
Draws the specified characters using the current font and color.
void drawImage(Image image, Point point, int anchor)
Draws an image at the given point.
void drawImage(Image image, int x, int y, int angle, int anchor)
Draw the specified source image at a specified destination point and according to an anchoring mechanism.
void drawImage(Image image, int x, int y, int destWidth, int destHeight, int angle, int anchor)
Draw the specified source image at a specified destination using an anchor mechanism, possibly zooming and rotating the image.
void drawImage(Image src, int x, int y, int anchor)
Draws the specified image by using the anchor point.
void drawImage(Image image, int x, int y, int destWidth, int destHeight, int anchor)
Draw the specified source image at a specified destination and according to an anchoring mechanism.
void drawImage(Image image, Point p)
Draw the specified source image at the specified destination point
void drawImage(Image image, int xSrc, int ySrc, int width, int height, int xDest, int yDest, int anchor)
Copies a region of the specified source image at a location on screen according to an anchor mechanism.
void drawImageRegion(Image image, Rect region, Point dest, int anchor)
Draws a region of the image at the given point
void drawImageRegion(Image src, int xSrc, int ySrc, int width, int height, int x, int y, int anchor)
Copies a region of the specified source image at a specified destination.
void drawImageRegion(Image image, Rect region, Point dest)
Copies a region of the specified source image at a specified destination.
void drawLine(Point p1, Point p2)
Draw a line segment (with a border of 1px) between two points p1 and p2.
void drawLine(int x1, int y1, int x2, int y2)
Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.
void drawRGB(int[] rgbData, int offset, int scanlength, Rect rect, boolean processAlpha)
Treat the specified array of colors as a bitmap, and draw it.
void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height, boolean processAlpha)
Treat the specified array of colors as a bitmap, and draw it.
void drawRect(int x, int y, int width, int height)
Draws the outline of the specified rectangle.
void drawRect(Rect rect)
Draw a rectangle (with a border of 1px).
void drawRegion(Image image, int xSrc, int ySrc, int width, int height, int transformation, int xDest, int yDest, int angle, int anchor)
Copies a region of the specified source image at a location on screen according to an anchoring mechanism.
void drawRegion(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int transformation, int destX, int destY, int anchor)
Copies a region of the specified source image to a location within the destination, possibly transforming (rotating and reflecting) the image data using the chosen transform function.
void drawRegion(Image image, int xSrc, int ySrc, int width, int height, int transformation, int xDest, int yDest, int widthDest, int heightDest, int angle, int anchor)
Copies a region of the specified source image to a location within the destination at anchor point.
void drawRegion(Image image, int xSrc, int ySrc, int width, int height, int transformation, int xDest, int yDest, int widthDest, int heightDest, int anchor)
Copies a region of the specified source image at a location on screen according to an anchoring mechanism.
void drawRoundRect(int x, int y, int width, int height, int rx, int ry)
Draws an outlined round-cornered rectangle using this graphics context's current color.
void drawRoundRect(Rect rect, int rx, int ry)
Draws an outlined round-cornered rectangle using this graphics context's current color.
void drawString(String str, Point p)
Draw a string at point coordinates.
void drawString(String str, Point p, int anchor)
Draws a string at point coordinates according to the anchor.
void drawString(String str, int x, int y, int anchor)
Draw a string with at screen coordinates x and y according to anchor.
void drawSubstring(String str, int offset, int len, int x, int y, int anchor)
This method draws a substring of a string starting from position offset with length len.
void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
Draw a triangle using three points.
void drawTriangle(Point p1, Point p2, Point p3)
Draw a triangle using three points.
void enableColorBlending(boolean enable)
Enable or disable the color blending with the current draw color.
void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Fills a circular or elliptical arc covering the specified rectangle.
void fillArc(Point center, int radius, int start, int end)
Fills a circular or elliptical arc covering the specified rectangle.
void fillRect(Rect rect)
Draws a filled rectangle.
void fillRect(int x, int y, int width, int height)
Fills the specified rectangle.
void fillRoundRect(int x, int y, int width, int height, int rx, int ry)
Fills the specified rounded corner rectangle with the current color.
void fillRoundRect(Rect rect, int rw, int rh)
Fills the specified rounded corner rectangle with the current color.
void fillTriangle(Point p1, Point p2, Point p3)
Fills the specified triangle with the current color.
void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
Fills the specified triangle with the current color.
int getClipHeight()
Returns the current clipping height.
int getClipWidth()
Returns the current clipping width.
int getClipX()
Gets the x offset of the current clipping area, relative to the coordinate system origin of this graphics context.
int getClipY()
Gets the y offset of the current clipping area, relative to the coordinate system origin of this graphics context.
Rect getClipping()
Returns the clipping area.
Color getColor()
Gets the current color
Font getFont()
Gets the current font.
Point getTranslate()
Gets the coordinates of the translated origin of this graphics context.
int getTranslateX()
Gets the x coordinate of the translated origin of this graphics context.
int getTranslateY()
Gets the y coordinate of the translated origin of this graphics context.
void reset()
Resets the clip and translate values for the current graphics.
void setAlphaBlending(int alpha)
Sets the alpha blending value.
void setClip(int x, int y, int width, int height)
Set a clip region in the Screen.
void setClip(Rect clipping)
Sets the clip for this graphics The clipping is reset after each paint.
void setColor(int red, int green, int blue)
Set the current color for all drawing methods by assing each 8 bits component of a RGB color
void setColor(Color color)
Sets the current color for all drawing methods.
void setColor(int color)
Sets the current color for all drawing methods.
void setFont(Font fnt)
Sets the current font of the graphics.
void setStrokeStyle(int style)
Sets the stroke style used for drawing lines, arcs, rectangles, and rounded rectangles.
void translate(int x, int y)
s Translates the coordinates of the origins.
void translate(Point p)
Translates the coordinates of the origins.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int DOTTED

Constant Value: 0 (0x00000000)

public static final int SOLID

Constant Value: 1 (0x00000001)

Public Methods

public void clear (Color color)

Clears the entire graphics area to the parameter color.

Note: the setColor(Color) is not modified by this method.

Parameters
color color to draw for clear
Throws
NullPointerException if the color parameter is null

public void clear (int color)

Clear the screen with the given ARGB color. You should called this method at the beginning of the paint method to clear drawings of the previous frame.

Parameters
color the color to clear the screen as an ARGB integer with 8 bits by component.

public void clipRect (int x, int y, int width, int height)

Set a clip region in the Screen. Only drawing in the specified region will be visible but all drawing calls will be rendered. Any previous screen translation (translate(int, int)) are taken into account.

Parameters
x the x coordinate of the upper left corner of the region to clip
y the y coordinate of the upper left corner of the region to clip
width the width of the region to clip
height the height of the region to clip

public void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle)

Draws the outline of a circular or elliptical arc covering the specified rectangle. The resulting arc begins at startAngle and extends for arcAngle degrees, using the current color. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation.

The center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments.

The resulting arc covers an area width + 1 pixels wide by height + 1 pixels tall.

The angles are specified relative to the non-square extents of the bounding rectangle such that 45 degrees always falls on the line from the center of the ellipse to the upper right corner of the bounding rectangle. As a result, if the bounding rectangle is noticeably longer in one axis than the other, the angles to the start and end of the arc segment will be skewed farther along the longer axis of the bounds.

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.

public void drawArc (Rect rectangle, int startAngle, int arcAngle)

Draws the outline of a circular or elliptical arc covering the specified rectangle, using the current color and stroke style.

Parameters
rectangle the angle of the arc
startAngle the start of the arc
arcAngle the end of the arc
Throws
NullPointerException if the rectangle parameter is null

public void drawChar (char character, int x, int y, int anchor)

This method draw a char at screen coordinates x and y according to anchor.

Note: This function is very slow when used for drawing string. You should used instead drawString(String, Point) or drawChars(char[], int, int, Point, int).

Parameters
character character to draw
x the x coordinate of the anchor point in the destination drawing area
y the y coordinate of the anchor point in the destination drawing area
anchor the anchor point for positioning the region within the destination image
Throws
IllegalArgumentException if the anchor is not valid

public void drawChar (char character, Point location, int anchor)

Draws the specified character using the current font and color.

Parameters
character the character to draw
location destination position
anchor anchor point
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException if the location parameter is null

public void drawChars (char[] data, int offset, int length, int x, int y, int anchor)

This method draw length char of an array of char starting from position offset at screen coordinates x and y according to anchor.

Parameters
data the array of char
offset the start position in the array
length the number of char to draw
x the x coordinate of the anchor point in the destination drawing area
y the y coordinate of the anchor point in the destination drawing area
anchor the anchor point for positioning the region within the destination image
Throws
IllegalArgumentException if the anchor is invalid
NullPointerException data is null

public void drawChars (char[] data, int offset, int length, Point position, int anchor)

Draws the specified characters using the current font and color.

The offset and length parameters must specify a valid range of characters within the character array data. The offset parameter must be within the range [0..(data.length)], inclusive.

The length parameter must be a non-negative integer such that (offset + length) <= data.length.

Parameters
data the array of characters to be drawn
offset the start offset in the data
length the number of characters to be drawn
position the anchor point position
anchor the anchor point for positioning the text
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException if the data parameter is null

public void drawImage (Image image, Point point, int anchor)

Draws an image at the given point.

Parameters
image the image
point the point where to draw the image. The top-left point of the image will be drawn at the this point
anchor the anchor parameter to position the image according to the destination point
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException if the image, point parameter is null

public void drawImage (Image image, int x, int y, int angle, int anchor)

Draw the specified source image at a specified destination point and according to an anchoring mechanism. The image can be rotated if requested. The rotation is done according to the center of the image and the angle in degrees.

Parameters
image the source image to draw
x the x coordinate of the anchor point in the destination drawing area
y the y coordinate of the anchor point in the destination drawing area
angle rotation angle in degrees. Interval is [-360, 360].
anchor the anchor mechanism for positioning the image at the destination point

public void drawImage (Image image, int x, int y, int destWidth, int destHeight, int angle, int anchor)

Draw the specified source image at a specified destination using an anchor mechanism, possibly zooming and rotating the image. Zooming is done by setting destination width and height with different values that the original size of the image. Rotation is done according to the center of the image and the angle in degrees.

Parameters
image the source image to draw
x the x coordinate of the anchor point in the destination drawing area
y the y coordinate of the anchor point in the destination drawing area
destWidth the width of the image in the drawing area. Can be different than the original width to zoom in or out.
destHeight the height of the image in the drawing area. Can be different than the original height to zoom in or out.
angle rotation angle in degrees. 2PI is 360.
anchor the anchor point for positioning the image within the destination image

public void drawImage (Image src, int x, int y, int anchor)

Draws the specified image by using the anchor point.

The image can be drawn in different positions relative to the anchor point by passing the appropriate position constants.

If the source image contains transparent pixels, the corresponding pixels in the destination image must be left untouched. If the source image contains partially transparent pixels, a compositing operation must be performed with the destination pixels, leaving all pixels of the destination image fully opaque.

Parameters
src the specified image to be drawn
x the x coordinate of the anchor point
y the y coordinate of the anchor point
anchor the anchor point for positioning the image
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException if the src parameter is null
See Also

public void drawImage (Image image, int x, int y, int destWidth, int destHeight, int anchor)

Draw the specified source image at a specified destination and according to an anchoring mechanism. The image can be zoomed if needed. Zooming is done by setting destination width and height with different values than the original image size.

This method uses internally drawImage(Image, int, int, int, int, int, int)

Parameters
image the source image to draw
x the x coordinate of the anchor point in the destination drawing area
y the y coordinate of the anchor point in the destination drawing area
destWidth the final width of the image when drawn on screen. Can be different than the original width to zoom in or out.
destHeight the final height of the image when drawn on screen. Can be different than the original height to zoom in or out.
anchor the anchor point for positioning the image according to the destination point

public void drawImage (Image image, Point p)

Draw the specified source image at the specified destination point

Parameters
image the source image to draw
p the destination point where to display the image (top left)
Throws
NullPointerException if the image or point parameter is null

public void drawImage (Image image, int xSrc, int ySrc, int width, int height, int xDest, int yDest, int anchor)

Copies a region of the specified source image at a location on screen according to an anchor mechanism.

The (xSrc, ySrc) coordinates are relative to the upper left corner of the source image. The xSrc, ySrc, width, and height parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image.

The (xDest, yDest) coordinates are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

Parameters
image the source image to copy from
xSrc the x coordinate of the upper left corner of the region within the source image to copy
ySrc the y coordinate of the upper left corner of the region within the source image to copy
width the width of the region to copy
height the height of the region to copy
xDest the x coordinate of the anchor point in the destination drawing area
yDest the y coordinate of the anchor point in the destination drawing area
anchor the anchor parameter to position the image according to the destination point
Throws
IllegalArgumentException if the anchor is invalid
NullPointerException if the image is null

public void drawImageRegion (Image image, Rect region, Point dest, int anchor)

Draws a region of the image at the given point

Parameters
image image the image
region a rectangle representing the region of image to draw
dest the point where to draw the image. The top-left point of the image will be drawn ath this point
anchor the anchor point for positioning the region within the destination image
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException

public void drawImageRegion (Image src, int xSrc, int ySrc, int width, int height, int x, int y, int anchor)

Copies a region of the specified source image at a specified destination.

The x and y coordinates of the region are relative to the upper left corner of the source image. The region parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image.

The x and y coordinates of the destination point are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

Parameters
src the source image to copy from
xSrc the x coordinate of the upper left corner of the region within the source image to copy
ySrc the y coordinate of the upper left corner of the region within the source image to copy
width the width of the region to copy
height the height of the region to copy
x the x coordinate of the anchor point in the destination drawing area
y the y coordinate of the anchor point in the destination drawing area
anchor the anchor point for positioning the region within the destination image
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException if the src parameter is null

public void drawImageRegion (Image image, Rect region, Point dest)

Copies a region of the specified source image at a specified destination.

The x and y coordinates of the region are relative to the upper left corner of the source image. The region parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image.

The x and y coordinates of the destination point are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

Parameters
image the source image to copy from
region the region to copy
dest the destination point in the drawing area
Throws
NullPointerException if a parameter is null

public void drawLine (Point p1, Point p2)

Draw a line segment (with a border of 1px) between two points p1 and p2.

Parameters
p1 the start of the line
p2 the end of the line

public void drawLine (int x1, int y1, int x2, int y2)

Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.

Parameters
x1 the first point's x coordinate.
y1 the first point's y coordinate.
x2 the second point's x coordinate.
y2 the second point's y coordinate.

public void drawRGB (int[] rgbData, int offset, int scanlength, Rect rect, boolean processAlpha)

Treat the specified array of colors as a bitmap, and draw it. This gives the same result as first creating a bitmap from the array, and then drawing it, but this method avoids explicitly creating a bitmap object which can be more efficient if the colors are changing often.

Parameters
rgbData Array of colors representing the pixels of the bitmap
offset Offset into the array of colors for the first pixel
scanlength The number of colors in the array between rows (must be >= width or <= -width).
rect the area where to draw the bitmap
processAlpha True if the alpha channel of the colors contains valid values. If false, the alpha byte is ignored (assumed to be 0xFF for every pixel).
Throws
NullPointerException if the rgbData or rect parameter is null

public void drawRGB (int[] rgbData, int offset, int scanlength, int x, int y, int width, int height, boolean processAlpha)

Treat the specified array of colors as a bitmap, and draw it. This gives the same result as first creating a bitmap from the array, and then drawing it, but this method avoids explicitly creating a bitmap object which can be more efficient if the colors are changing often.

Parameters
rgbData Array of colors representing the pixels of the bitmap
offset Offset into the array of colors for the first pixel
scanlength The number of colors in the array between rows (must be >= width or <= -width).
x The X coordinate where to draw the bitmap.
y The Y coordinate where to draw the bitmap
width The width of the bitmap.
height The height of the bitmap
processAlpha True if the alpha channel of the colors contains valid values. If false, the alpha byte is ignored (assumed to be 0xFF for every pixel).
Throws
NullPointerException if the rgbData parameter is null

public void drawRect (int x, int y, int width, int height)

Draws the outline of the specified rectangle. The left and right edges of the rectangle are at x and x + width. The top and bottom edges are at y and y + height. The rectangle is drawn using the graphics context's current color.

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

public void drawRect (Rect rect)

Draw a rectangle (with a border of 1px).

Parameters
rect the rectangle to draw

public void drawRegion (Image image, int xSrc, int ySrc, int width, int height, int transformation, int xDest, int yDest, int angle, int anchor)

Copies a region of the specified source image at a location on screen according to an anchoring mechanism.

Possible transformations:

  • Rotating and reflecting the image data using the chosen transform function
  • Free rotating the image data with the angle parameter

The (xSrc, ySrc) coordinates are relative to the upper left corner of the source image. The xSrc, ySrc, width, and height parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image.

The (xDest, yDest) coordinates are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

The transform is applied to the image data from the region of the source image, and the result is rendered with its anchor point positioned at location (xDest, yDest) in the destination.

A free rotation in degrees can be applied in addition to the rotation specified by the transform parameter. The two rotations are added.

Parameters
image the source image to copy from
xSrc the x coordinate of the upper left corner of the region within the source image to copy
ySrc the y coordinate of the upper left corner of the region within the source image to copy
width the width of the region to copy
height the height of the region to copy
transformation the desired transformation for the selected region being copied
xDest the x coordinate of the anchor point in the destination drawing area
yDest the y coordinate of the anchor point in the destination drawing area
angle the rotation angle in degrees. 2PI is equal to 360.
anchor the anchor parameter to position the image according to the destination point

public void drawRegion (Image image, int srcX, int srcY, int srcWidth, int srcHeight, int transformation, int destX, int destY, int anchor)

Copies a region of the specified source image to a location within the destination, possibly transforming (rotating and reflecting) the image data using the chosen transform function.

The destination, if it is an image, must not be the same image as the source image. If it is, an exception is thrown. This restriction is present in order to avoid ill-defined behaviors that might occur if overlapped, transformed copies were permitted.

The (srcX, srcY) coordinates are relative to the upper left corner of the source image. The srcX, srcY, srcWidth, and srcHeight parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image.

The (destX, destY) coordinates are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

The transform is applied to the image data from the region of the source image, and the result is rendered with its anchor point positioned at location (destX, destY) in the destination.

Parameters
image the source image to copy from
srcX the x coordinate of the upper left corner of the region within the source image to copy
srcY the y coordinate of the upper left corner of the region within the source image to copy
srcWidth the width of the region to copy
srcHeight the height of the region to copy
transformation the desired transformation for the selected region being copied
destX the X coordinate of the anchor point in the destination drawing area
destY the Y coordinate of the anchor point in the destination drawing area
anchor the anchor point for positioning the region within the destination image
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException if the image parameter is null

public void drawRegion (Image image, int xSrc, int ySrc, int width, int height, int transformation, int xDest, int yDest, int widthDest, int heightDest, int angle, int anchor)

Copies a region of the specified source image to a location within the destination at anchor point.

Possible transformations:

  • Zooming the image data with the widthDest and heightDest parameters
  • Rotating and reflecting the image data using the chosen transform function
  • Free rotating the image data with the angle parameter

The (xSrc, ySrc) coordinates are relative to the upper left corner of the source image. The xSrc, ySrc, width, and height parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image.

The (xDest, yDest) coordinates are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

The transform is applied to the image data from the region of the source image, and the result is rendered with its anchor point positioned at location (xDest, yDest) in the destination.

The (widthDest, heightDest) size is used to specified zoom in/out of the rectangular region drawn within the destination.

A free rotation in degrees can be applied in addition to the rotation specified by the transform parameter. The two rotations are added.

Parameters
image the source image to copy from
xSrc the x coordinate of the upper left corner of the region within the source image to copy
ySrc the y coordinate of the upper left corner of the region within the source image to copy
width the width of the region to copy
height the height of the region to copy
transformation the desired transformation for the selected region being copied
xDest the x coordinate of the anchor point in the destination drawing area
yDest the y coordinate of the anchor point in the destination drawing area
widthDest the width of the image in the drawing area. Can be different than the original width to zoom in or out.
heightDest the height of the image in the drawing area. Can be different than the original height to zoom in or out.
angle rotation angle in degrees. Interval is [-360, 360].
anchor the anchor point for positioning the region within the destination image

public void drawRegion (Image image, int xSrc, int ySrc, int width, int height, int transformation, int xDest, int yDest, int widthDest, int heightDest, int anchor)

Copies a region of the specified source image at a location on screen according to an anchoring mechanism.

Possible transformations:

  • Zooming the image data with the widthDest and heightDest parameters
  • Rotating and reflecting the image data using the chosen transform function

The (xSrc, ySrc) coordinates are relative to the upper left corner of the source image. The xSrc, ySrc, width, and height parameters specify a rectangular region of the source image. It is illegal for this region to extend beyond the bounds of the source image.

The (xDest, yDest) coordinates are relative to the coordinate system of this Graphics object. It is legal for the destination area to extend beyond the bounds of the Graphics object. Pixels outside of the bounds of the Graphics object will not be drawn.

The transform is applied to the image data from the region of the source image, and the result is rendered with its anchor point positioned at location (xDest, yDest) in the destination.

The (widthDest, heightDest) size is used to specified zoom in/out of the rectangular region drawn within the destination.

Parameters
image the source image to copy from
xSrc the x coordinate of the upper left corner of the region within the source image to copy
ySrc the y coordinate of the upper left corner of the region within the source image to copy
width the width of the region to copy
height the height of the region to copy
transformation the desired transformation for the selected region being copied
xDest the x coordinate of the anchor point in the destination drawing area
yDest the y coordinate of the anchor point in the destination drawing area
widthDest the width of the image in the drawing area. Can be different than the original width to zoom in or out.
heightDest the height of the image in the drawing area. Can be different than the original height to zoom in or out.
anchor the anchor parameter to position the image according to the destination point

public void drawRoundRect (int x, int y, int width, int height, int rx, int ry)

Draws an outlined round-cornered rectangle using this graphics context's current color. The left and right edges of the rectangle are at x and x + width, respectively. The top and bottom edges of the rectangle are at y and y + height.

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.
rx the horizontal diameter of the arc at the four corners.
ry the vertical diameter of the arc at the four corners.

public void drawRoundRect (Rect rect, int rx, int ry)

Draws an outlined round-cornered rectangle using this graphics context's current color.

Parameters
rect the rectangle to draw
rx the horizontal diameter of the arc at the four corners
ry the vertical diameter of the arc at the four corners

public void drawString (String str, Point p)

Draw a string at point coordinates.

Parameters
str the string to draw
p the coordinates of the destination drawing area
Throws
NullPointerException if the str or p parameter is null

public void drawString (String str, Point p, int anchor)

Draws a string at point coordinates according to the anchor.

Parameters
str the String to draw
p the point where to draw the string. The top-left point of the string will be drawn ath this point
anchor the anchor point for positioning the region within the destination image
Throws
IllegalArgumentException if anchor is not a legal value
NullPointerException if the str or p parameter is null

public void drawString (String str, int x, int y, int anchor)

Draw a string with at screen coordinates x and y according to anchor. The string is automatically added to an intern string cache to optimize further drawing in case of the same string is drawn along several successive frames.

Parameters
str the string to draw
x the x coordinate of the anchor point in the destination drawing area
y the y coordinate of the anchor point in the destination drawing area
anchor the anchor point for positioning the region within the destination image
Throws
NullPointerException if str is null

public void drawSubstring (String str, int offset, int len, int x, int y, int anchor)

This method draws a substring of a string starting from position offset with length len. The substring is drawn at screen coordinates x and y according to anchor.

Parameters
str the source string
offset the start position of the substring
len the length of the substring
x the x coordinate of the destination point on screen
y the y coordinate of the destination point on screen
anchor the anchor for positioning the string relatively to the destination point

public void drawTriangle (int x1, int y1, int x2, int y2, int x3, int y3)

Draw a triangle using three points.

Parameters
x1 the x coordinate of the first point
y1 the y coordinate of the first point
x2 the x coordinate of the second point
y2 the y coordinate of the second point
x3 the x coordinate of the third point
y3 the y coordinate of the third point

public void drawTriangle (Point p1, Point p2, Point p3)

Draw a triangle using three points.

Parameters
p1 first point
p2 second point
p3 third point

public void enableColorBlending (boolean enable)

Enable or disable the color blending with the current draw color. Color blending is applied on Image and Font type primitives which use OpenGL ES textures. Color blending is done by changing the texture mode in GL_REPLACE if disabled and to GL_MODULATE if enabled.

Parameters
enable set at True to enable the color blending. At False to disable.

public void fillArc (int x, int y, int width, int height, int startAngle, int arcAngle)

Fills a circular or elliptical arc covering the specified rectangle. The resulting arc begins at startAngle and extends for arcAngle degrees. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value indicates a counter-clockwise rotation while a negative value indicates a clockwise rotation.

The center of the arc is the center of the rectangle whose origin is (x, y) and whose size is specified by the width and height arguments.

The resulting arc covers an area width + 1 pixels wide by height + 1 pixels tall.

The angles are specified relative to the non-square extents of the bounding rectangle such that 45 degrees always falls on the line from the center of the ellipse to the upper right corner of the bounding rectangle. As a result, if the bounding rectangle is noticeably longer in one axis than the other, the angles to the start and end of the arc segment will be skewed farther along the longer axis of the bounds.

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.

public void fillArc (Point center, int radius, int start, int end)

Fills a circular or elliptical arc covering the specified rectangle.

Parameters
center the center of the arc
radius the angle of the arc
start the start of the arc
end the end of the arc
Throws
NullPointerException if the center parameter is null

public void fillRect (Rect rect)

Draws a filled rectangle.

Parameters
rect the rectangle to draw
Throws
NullPointerException if the rect parameter is null

public void fillRect (int x, int y, int width, int height)

Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.

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.

public void fillRoundRect (int x, int y, int width, int height, int rx, int ry)

Fills the specified rounded corner rectangle with the current color. The left and right edges of the rectangle are at x and x + width - 1, respectively. The top and bottom edges of the rectangle are at y and y + height - 1.

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.
rx the horizontal diameter of the arc at the four corners.
ry the vertical diameter of the arc at the four corners.

public void fillRoundRect (Rect rect, int rw, int rh)

Fills the specified rounded corner rectangle with the current color.

Parameters
rect the rectangle to fill
rw the horizontal diameter of the arc at the four corners.
rh the vertical diameter of the arc at the four corners.

public void fillTriangle (Point p1, Point p2, Point p3)

Fills the specified triangle with the current color. The lines connecting each pair of points are included in the filled triangle.

Parameters
p1 first point
p2 second point
p3 third point

public void fillTriangle (int x1, int y1, int x2, int y2, int x3, int y3)

Fills the specified triangle with the current color. The lines connecting each pair of points are included in the filled triangle.

Parameters
x1 the x coordinate of the first point
y1 the y coordinate of the first point
x2 the x coordinate of the second point
y2 the y coordinate of the second point
x3 the x coordinate of the third point
y3 the y coordinate of the third point

public int getClipHeight ()

Returns the current clipping height.

Returns
  • the clipping height

public int getClipWidth ()

Returns the current clipping width.

Returns
  • the clipping width

public int getClipX ()

Gets the x offset of the current clipping area, relative to the coordinate system origin of this graphics context.

Returns
  • x offset of the current clipping area

public int getClipY ()

Gets the y offset of the current clipping area, relative to the coordinate system origin of this graphics context.

Returns
  • the clipping y

public Rect getClipping ()

Returns the clipping area.

Returns
  • the clipping rectangle

public Color getColor ()

Gets the current color

Returns
  • the current color

public Font getFont ()

Gets the current font.

Returns
  • the current font

public Point getTranslate ()

Gets the coordinates of the translated origin of this graphics context.

Returns
  • the translate

public int getTranslateX ()

Gets the x coordinate of the translated origin of this graphics context.

Returns
  • the translate x

public int getTranslateY ()

Gets the y coordinate of the translated origin of this graphics context.

Returns
  • the translate y

public void reset ()

Resets the clip and translate values for the current graphics.

public void setAlphaBlending (int alpha)

Sets the alpha blending value.

Parameters
alpha (0 - 255)

public void setClip (int x, int y, int width, int height)

Set a clip region in the Screen. Only drawing in the specified region will be visible but all drawing calls will be rendered. Any previous screen translation (translate(int, int)) are taken into account.

Parameters
x the x coordinate of the upper left corner of the region to clip
y the y coordinate of the upper left corner of the region to clip
width the width of the region to clip
height the height of the region to clip

public void setClip (Rect clipping)

Sets the clip for this graphics The clipping is reset after each paint.

Parameters
clipping the clipping rectangle

public void setColor (int red, int green, int blue)

Set the current color for all drawing methods by assing each 8 bits component of a RGB color

Parameters
red the red component as an 8 bits int, so in range [0, 255].
green the green component as an 8 bits int, so in range [0, 255].
blue the blue component as an 8 bits int, so in range [0, 255].

public void setColor (Color color)

Sets the current color for all drawing methods.

Parameters
color the color to set

public void setColor (int color)

Sets the current color for all drawing methods.

Parameters
color ARGB color

public void setFont (Font fnt)

Sets the current font of the graphics. This font will be used to draw strings.

Parameters
fnt the font to set

public void setStrokeStyle (int style)

Sets the stroke style used for drawing lines, arcs, rectangles, and rounded rectangles. This does not affect fill, text, and image operations.

Parameters
style can be SOLID or DOTTED.
See Also

public void translate (int x, int y)

s Translates the coordinates of the origins. Theses coordinates are reset after each paint.

Parameters
x the x coordinate of the new translation origin
y the y coordinate of the new translation origin

public void translate (Point p)

Translates the coordinates of the origins. Theses coordinates are reset after each paint.

Parameters
p the coordinates of the new translation origin