com.yospace.yae.yogi
Class Graphics

java.lang.Object
  |
  +--com.yospace.yae.yogi.Graphics
Direct Known Subclasses:
MIDletGraphics

public abstract class Graphics
extends java.lang.Object

An abstract class representing the graphics capabilities required from the underlying graphics system. A Graphics object represents a "pen", or "Graphics Context" with which drawing may be performed on some surface. It maintains a current color, pen style, font, clip area, and translation origin.

Graphics objects are usually acquired as arguments to the various paint methods that are called by the paint system in YogiComponent or ComponentUI.

The methods in this class are closely based around those provided by AWT and MIDP, and in such environments, Graphics will delegate to such a native implementation.

As with other such systems, it is assumed that there are two axes of integer pixel coordinates: a horizontal axis going from left to right and a vertical axis going from top to bottom. The coordinates define the points on a grid of infinitessimally fine lines; an actual pixel drawn at a particular location hangs below and to the right of that point.


Field Summary
static int DOTTED
          Constant for specifying DOTTED line-drawing style.
static int SOLID
          Constant for specifying SOLID line-drawing style.
 
Constructor Summary
Graphics()
           
 
Method Summary
abstract  void clearRect(int x, int y, int width, int height)
          Convenience method for calling fillRect with a paint color set to white.
abstract  void clipRect(int x, int y, int width, int height)
          This method sets the clip region to be the overlap of the existing clip region and the rectangle described by the arguments.
abstract  void clipRect(Rectangle r)
          Convenience method for calling clipRect(int,int,int,int).
abstract  Graphics create()
          This factory method creates a new instance of a Graphics as an exact copy of this one.
 Graphics create(int x, int y, int width, int height)
          This factory method creates a new instance of a Graphics as an exact copy of this one, but clipped to apply only to the specified sub-region.
abstract  void dispose()
          This method is called when the Graphics object is no longer required.
abstract  void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
          Draws an arc sector of an oval, using the current color and stroke style.
abstract  void drawImage(Image image, int x, int y)
          Draw the specified Image at the given location.
abstract  void drawLine(int x1, int y1, int x2, int y2)
          Draws a straight line, using the current color and stroke style, joining the points (x1, y1) and (x2, y2).
abstract  void drawOval(int x, int y, int width, int height)
          Draws an oval, using the current color and stroke style, whose bounding rectangle is defined by the x, y, width and height arguments.
abstract  void drawPolygon(int[] xPoints, int[] yPoints, int numPoints)
          Draws a series of lines joining the points defined in the xPoints and yPoints arrays, in the same style as drawPolyline.
abstract  void drawPolyline(int[] xPoints, int[] yPoints, int numPoints)
          Draws a series of straight lines, using the current color and stroke style, joining the points defined by successive pairs of values in the two arrays.
abstract  void drawRect(int x, int y, int width, int height)
          This method draws the border of a rectangle described by its arguments, using the current color and stroke style.
abstract  void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Draws a rectangle with rounded corners, using the current color and stroke style.
abstract  void drawString(java.lang.String text, int x, int y)
          Draws the specified text in the current color and font at the specified location.
abstract  void drawString(java.lang.String text, int x, int y, int backColor)
          Draws the specified text in the current color and font at the specified location.
abstract  void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
          Fills an arc sector of an oval, using the current color.
abstract  void fillOval(int x, int y, int width, int height)
          Fills an oval, using the current color, whose bounding rectangle is defined by the x, y, width and height arguments.
abstract  void fillRect(int x, int y, int width, int height)
          This method fills the rectangle described by the arguments with the current color.
abstract  void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Fills a rectangle with rounded corners, using the current color.
abstract  Rectangle getClipBounds()
          Convenience method for returning the current clip rectangle in one method call.
abstract  int getClipHeight()
          Returns the height of the current clip region.
abstract  int getClipWidth()
          Returns the width of the current clip region.
abstract  int getClipX()
          Returns the x-coordinate of the top-left corner of the current clip region, relative to the current origin.
abstract  int getClipY()
          Returns the y-coordinate of the top-left corner of the current clip region, relative to the current origin.
abstract  int getColor()
          Returns the current drawing color in 0xRRGGBB format.
abstract  int getTranslateX()
          Returns the x-coordinate of the current origin in Absolute Coordinates.
abstract  int getTranslateY()
          Returns the y-coordinate of the current origin in Absolute Coordinates.
abstract  boolean hitClip(int x, int y, int width, int height)
          Determines whether the rectangle described by the arguments and the current clip area intersect.
abstract  void setClip(int x, int y, int width, int height)
          This method sets the clip rectangle to be precisely that defined by the arguments, relative to the current origin (which is not affected).
abstract  void setClip(Rectangle r)
          Convenience method for calling setClip(int,int,int,int).
abstract  void setColor(int c)
          Sets the current drawing color, using 0xRRGGBB format.
abstract  void setFont(Font f)
          Sets the font to be used for subsequent text drawing operations.
abstract  void setStrokeStyle(int strokeStyle)
          This method sets the drawing stroke style for subsequent drawing operations.
abstract  void translate(int x, int y)
          This method moves the origin of all subsequent graphical operations to the specified location.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOLID

public static final int SOLID
Constant for specifying SOLID line-drawing style.
See Also:
setStrokeStyle(int)

DOTTED

public static final int DOTTED
Constant for specifying DOTTED line-drawing style.
See Also:
setStrokeStyle(int)
Constructor Detail

Graphics

public Graphics()
Method Detail

create

public abstract Graphics create()
This factory method creates a new instance of a Graphics as an exact copy of this one. This is useful when multiple screen areas need drawing independently.
Returns:
a new Graphics object.
See Also:
create(int, int, int, int)

create

public Graphics create(int x,
                       int y,
                       int width,
                       int height)
This factory method creates a new instance of a Graphics as an exact copy of this one, but clipped to apply only to the specified sub-region. The rectangle defined by the arguments is intersected with the existing clip area, and then the translation origin is moved to (x, y), so that the new Graphics can only affect that rectangle and sees its top-left corner as the origin.

This is a convenience method to replace:

Graphics g = create();
g.clipRect(x, y, width, height);
g.translate(x, y);
Parameters:
x - the x coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
width - the width of the new clip rectangle.
height - the height of the new clip rectangle.
Returns:
a new Graphics object.
See Also:
clipRect(int,int,int,int), translate(int,int), create()

dispose

public abstract void dispose()
This method is called when the Graphics object is no longer required. It presents an early opportunity for its resources to be freed. The behavior of a Graphics after it has been disposed is undefined, and should be avoided.

setClip

public abstract void setClip(int x,
                             int y,
                             int width,
                             int height)
This method sets the clip rectangle to be precisely that defined by the arguments, relative to the current origin (which is not affected). The existing clip area is disregarded.

As this can make the clip region larger than it was, it is generally risky to call setClip, as it may allow unwanted corruption of other graphics areas. In general, the clipRect method is safer.

Parameters:
x - the x coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
width - the width of the new clip rectangle.
height - the height of the new clip rectangle.

setClip

public abstract void setClip(Rectangle r)
Convenience method for calling setClip(int,int,int,int).
Parameters:
r - a Rectangle object defining the new clip area

clipRect

public abstract void clipRect(int x,
                              int y,
                              int width,
                              int height)
This method sets the clip region to be the overlap of the existing clip region and the rectangle described by the arguments. Thus, the new clip area will not exceed its previous bounds, making it a safer alternative to setClip.

The origin is not affected.

Parameters:
x - the x coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
width - the width of the new clip rectangle.
height - the height of the new clip rectangle.

clipRect

public abstract void clipRect(Rectangle r)
Convenience method for calling clipRect(int,int,int,int).
Parameters:
r - a Rectangle object defining the new clip area

hitClip

public abstract boolean hitClip(int x,
                                int y,
                                int width,
                                int height)
Determines whether the rectangle described by the arguments and the current clip area intersect. This is useful for deciding whether the area in that rectangle is worth painting.
Parameters:
x - the x coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the new clip rectangle, relative to the current origin.
width - the width of the new clip rectangle.
height - the height of the new clip rectangle.

getClipX

public abstract int getClipX()
Returns the x-coordinate of the top-left corner of the current clip region, relative to the current origin.
Returns:
the x-coordinate of the current clip rectangle.
See Also:
clipRect(int,int,int,int), setClip(int,int,int,int), getClipY(), getClipWidth(), getClipHeight()

getClipY

public abstract int getClipY()
Returns the y-coordinate of the top-left corner of the current clip region, relative to the current origin.
Returns:
the y-coordinate of the current clip rectangle.
See Also:
clipRect(int,int,int,int), setClip(int,int,int,int), getClipX(), getClipWidth(), getClipHeight()

getClipWidth

public abstract int getClipWidth()
Returns the width of the current clip region.
Returns:
the width of the current clip rectangle.
See Also:
clipRect(int,int,int,int), setClip(int,int,int,int), getClipX(), getClipY(), getClipHeight()

getClipHeight

public abstract int getClipHeight()
Returns the height of the current clip region.
Returns:
the height of the current clip rectangle.
See Also:
clipRect(int,int,int,int), setClip(int,int,int,int), getClipX(), getClipY(), getClipWidth()

getClipBounds

public abstract Rectangle getClipBounds()
Convenience method for returning the current clip rectangle in one method call. This is likely to be less efficient for accessing the individual fields than calling the direct accessors, as it has to allocate a Rectangle object.
Returns:
a rectangle describing the current clip rectangle.
See Also:
getClipX(), getClipY(), getClipWidth(), getClipHeight()

translate

public abstract void translate(int x,
                               int y)
This method moves the origin of all subsequent graphical operations to the specified location. All clipping and drawing operations work relative to the new location.

Note that, although the origin may move, the physical region described by the clip area does not change. Thus, after translating the origin, it will be found that the location of the clip area has apparently moved in the opposite direction.

Parameters:
x - the x-coordinate (in the existing coordinate space) of the new origin.
y - the y-coordinate (in the existing coordinate space) of the new origin.

getTranslateX

public abstract int getTranslateX()
Returns the x-coordinate of the current origin in Absolute Coordinates. This value is not relative to the current coordinate system.
Returns:
the absolute x position of the current origin.

getTranslateY

public abstract int getTranslateY()
Returns the y-coordinate of the current origin in Absolute Coordinates. This value is not relative to the current coordinate system.
Returns:
the absolute y position of the current origin.

setColor

public abstract void setColor(int c)
Sets the current drawing color, using 0xRRGGBB format.
Parameters:
c - the new color as a numeric 24-bit color value.
See Also:
ColorUtil

getColor

public abstract int getColor()
Returns the current drawing color in 0xRRGGBB format.
Returns:
the current color as a numeric 24-bit color value.
See Also:
ColorUtil

setFont

public abstract void setFont(Font f)
Sets the font to be used for subsequent text drawing operations. Font objects may be acquired from the Font.getFont(name) or Font.getFont(face,size,style) methods.

There is currently no accessor method for this property.

Parameters:
f - the new font.

setStrokeStyle

public abstract void setStrokeStyle(int strokeStyle)
This method sets the drawing stroke style for subsequent drawing operations. Currently supported options are SOLID for drawing solid lines, and DOTTED for drawing dotted lines.

There is currently no accessor method for this property.

Parameters:
strokeStyle - either SOLID or DOTTED.

drawLine

public abstract void drawLine(int x1,
                              int y1,
                              int x2,
                              int y2)
Draws a straight line, using the current color and stroke style, joining the points (x1, y1) and (x2, y2).
Parameters:
x1 - the x-coordinate of the first end-point.
y1 - the y-coordinate of the first end-point.
x2 - the x-coordinate of the second end-point.
y2 - the y-coordinate of the second end-point.

drawPolyline

public abstract void drawPolyline(int[] xPoints,
                                  int[] yPoints,
                                  int numPoints)
Draws a series of straight lines, using the current color and stroke style, joining the points defined by successive pairs of values in the two arrays. The last point is not connected back to the first. The number of points supplied is considered as being the least of the lengths of the two arrays and the numPoints value.
Parameters:
xPoints - an array of x-coordinate values corresponding to the y-coordinates in yPoints.
yPoints - an array of y-coordinate values corresponding to the x-coordinates in xPoints.
numPoints - the number of points to be connected, if less than the length of either the xPoints or yPoints arrays.
See Also:
drawPolygon(int[],int[],int)

drawRect

public abstract void drawRect(int x,
                              int y,
                              int width,
                              int height)
This method draws the border of a rectangle described by its arguments, using the current color and stroke style.

Because pixels hang below and to the right of the coordinate axes, and to ensure that a drawRect call is equivalent to

drawLine(x, y, x+w, y);     //top
drawLine(x, y+h, x+w, y+h); //bottom
drawLine(x, y, x, y+h);     //left
drawLine(x+w, y, x+w, y+h); //right
the resulting rectangle is actually (w+1) pixels across the top and (h+1) pixels high.

Thus, drawing a rectangle and filling it must be done with a little consideration.

Parameters:
x - the x coordinate of the top-left corner of the rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the rectangle, relative to the current origin.
width - the width of the rectangle.
height - the height of the rectangle.

fillRect

public abstract void fillRect(int x,
                              int y,
                              int width,
                              int height)
This method fills the rectangle described by the arguments with the current color.

Note that this rectangle is defined in terms of the inter-pixel grid lines, and hence a call to fillRect(0, 0, 30, 20) will fill exactly 600 pixels. This is therefore a slightly different rectangle to that produced by drawRect.

Parameters:
x - the x coordinate of the top-left corner of the rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the rectangle, relative to the current origin.
width - the width of the rectangle.
height - the height of the rectangle.

clearRect

public abstract void clearRect(int x,
                               int y,
                               int width,
                               int height)
Convenience method for calling fillRect with a paint color set to white.
Parameters:
x - the x coordinate of the top-left corner of the rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the rectangle, relative to the current origin.
width - the width of the rectangle.
height - the height of the rectangle.

drawRoundRect

public abstract void drawRoundRect(int x,
                                   int y,
                                   int width,
                                   int height,
                                   int arcWidth,
                                   int arcHeight)
Draws a rectangle with rounded corners, using the current color and stroke style. The width and height are interpreted to refer to the same rectangle as would be drawn by drawRect.
Parameters:
x - the x coordinate of the top-left corner of the enclosing rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the enclosing rectangle, relative to the current origin.
width - the overall width of the rectangle.
height - the overall height of the rectangle.
arcWidth - the horizontal diameter of the oval whose quarter arc will be drawn in the corners of the rectangle to create the rounded effect.
arcHeight - the vertical diameter of the oval whose quarter arc will be drawn in the corners of the rectangle to create the rounded effect.

fillRoundRect

public abstract void fillRoundRect(int x,
                                   int y,
                                   int width,
                                   int height,
                                   int arcWidth,
                                   int arcHeight)
Fills a rectangle with rounded corners, using the current color. The width and height are interpreted to refer to the same rectangle as would be drawn by fillRect.
Parameters:
x - the x coordinate of the top-left corner of the enclosing rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the enclosing rectangle, relative to the current origin.
width - the width of the enclosing rectangle.
height - the height of the enclosing rectangle.
arcWidth - the horizontal diameter of the oval whose quarter arc will be filled in the corners of the rectangle to create the rounded effect.
arcHeight - the vertical diameter of the oval whose quarter arc will be filled in the corners of the rectangle to create the rounded effect.

drawArc

public abstract void drawArc(int x,
                             int y,
                             int width,
                             int height,
                             int startAngle,
                             int arcAngle)
Draws an arc sector of an oval, using the current color and stroke style. The oval's bounding rectangle is defined by the x, y, width and height arguments. The arc begins at startAngle degrees and extends for arcAngle degrees. Zero degrees is interpreted as a 3 o'clock direction, with positive angles indicating an anti-clockwise direction.

The width and height are interpreted to refer to the same rectangle as would be drawn by drawRect.

Note that 45 degrees is interpreted to be the direction defined by the diagonals of the bounding rectangle. Thus, when width and height have different values, the 45 degree line will not correspond to 45 degrees on the screen. This is best imagined by considering an oval to be a stretched circle - the angles are defined in terms of the circle, and are skewed when the circle is stretched.

Parameters:
x - the x coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
width - the width of the enclosing rectangle.
height - the height of the enclosing rectangle.
startAngle - the start angle of the arc, measured in degrees.
arcHeight - the extent of the arc, relative to startAngle.

fillArc

public abstract void fillArc(int x,
                             int y,
                             int width,
                             int height,
                             int startAngle,
                             int arcAngle)
Fills an arc sector of an oval, using the current color. The oval's bounding rectangle is defined by the x, y, width and height arguments. The arc begins at startAngle degrees and extends for arcAngle degrees. Zero degrees is interpreted as a 3 o'clock direction, with positive angles indicating an anti-clockwise direction.

The width and height are interpreted to refer to the same rectangle as would be filled by fillRect.

Parameters:
x - the x coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
width - the width of the enclosing rectangle.
height - the height of the enclosing rectangle.
startAngle - the start angle of the arc, measured in degrees.
arcHeight - the extent of the arc, relative to startAngle.
See Also:
drawArc(int,int,int,int,int,int)

drawPolygon

public abstract void drawPolygon(int[] xPoints,
                                 int[] yPoints,
                                 int numPoints)
Draws a series of lines joining the points defined in the xPoints and yPoints arrays, in the same style as drawPolyline. This method differs only in that the line connecting the first and last points is also drawn, creating a closed figure.
Parameters:
xPoints - an array of x-coordinate values corresponding to the y-coordinates in yPoints.
yPoints - an array of y-coordinate values corresponding to the x-coordinates in xPoints.
numPoints - the number of points to be used from the arrays, if less than the length of either of the arrays.
See Also:
drawPolyline(int[],int[],int)

drawOval

public abstract void drawOval(int x,
                              int y,
                              int width,
                              int height)
Draws an oval, using the current color and stroke style, whose bounding rectangle is defined by the x, y, width and height arguments.

The width and height are interpreted to refer to the same rectangle as would be drawn by drawRect.

Parameters:
x - the x coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
width - the width of the enclosing rectangle.
height - the height of the enclosing rectangle.

fillOval

public abstract void fillOval(int x,
                              int y,
                              int width,
                              int height)
Fills an oval, using the current color, whose bounding rectangle is defined by the x, y, width and height arguments.

The width and height are interpreted to refer to the same rectangle as would be drawn by fillRect.

Parameters:
x - the x coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
y - the y coordinate of the top-left corner of the oval's enclosing rectangle, relative to the current origin.
width - the width of the enclosing rectangle.
height - the height of the enclosing rectangle.

drawString

public abstract void drawString(java.lang.String text,
                                int x,
                                int y)
Draws the specified text in the current color and font at the specified location. This is a convenience method for drawString(java.lang.String,int,int,int), and assumes a white background color with respect to anti-aliasing fonts.
Parameters:
text - the text to be drawn.
x - the location of the left-hand side of the text.
y - the location of top of the text. Note that this is different from AWT's interpretation of the text location.

drawString

public abstract void drawString(java.lang.String text,
                                int x,
                                int y,
                                int backColor)
Draws the specified text in the current color and font at the specified location. This method accommodates the behavior of anti-aliased fonts, which soften their edges into the background color. If the current font is not anti-aliased, then the background color is disregarded.
Parameters:
text - the text to be drawn.
x - the position of the left-hand side of the text.
y - the top of the text. Note that this is different from AWT's interpretation of the text location.
backColor - the background color for anti-aliasing.

drawImage

public abstract void drawImage(Image image,
                               int x,
                               int y)
Draw the specified Image at the given location.
Parameters:
image - the image to be drawn.
x - the x-coordinate of the top-left of the image.
y - the y-coordinate of the top-left of the image.

Copyright 2002 Yospace Holdings Ltd. All Rights Reserved.