waba.fx
Class Graphics

java.lang.Object
  |
  +--waba.fx.Graphics

public class Graphics
extends java.lang.Object

Graphics draws on a surface.

Surfaces are objects that implement the ISurface interface. MainWindow and Image are both examples of surfaces.

Here is an example that uses Graphics to draw a line:

 public class MyProgram extends MainWindow
 {
 public void onPaint(Graphics g)
  {
  g.setColor(0, 0, 255);
  g.drawLine(0, 0, 10, 10);
 ...
 


Field Summary
static int DRAW_AND
          The constant for a draw operation that AND's the source with the destination.
static int DRAW_OR
          The constant for a draw operation that OR's the source with the destination.
static int DRAW_OVER
          The constant for a draw operation that draws the source over the destination.
static int DRAW_XOR
          The constant for a draw operation that XOR's the source with the destination.
 
Constructor Summary
Graphics(ISurface surface)
          Constructs a graphics object which can be used to draw on the given surface.
 
Method Summary
 void clearClip()
          Clears the current clipping rectangle.
 void copyRect(ISurface surface, int x, int y, int width, int height, int dstX, int dstY)
          Copies a rectangular area from a surface to the given coordinates on the current surface.
 void drawCursor(int x, int y, int width, int height)
          Draws a cursor by XORing the given rectangular area on the surface.
 void drawDots(int x1, int y1, int x2, int y2)
          Draws a dotted line at the given coordinates.
 void drawImage(Image image, int x, int y)
          Draws an image at the given x and y coordinates.
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a line at the given coordinates.
 void drawPolygon(int[] x, int[] y, int count)
          Draws the outline of a polygon with the given coordinates.
 void drawRect(int x, int y, int width, int height)
          Draws a rectangle at the given coordinates.
 void drawText(char[] chars, int start, int count, int x, int y)
          Draws text at the given coordinates.
 void drawText(java.lang.String s, int x, int y)
          Draws text at the given coordinates.
 void fillPolygon(int[] x, int[] y, int count)
          Draws a filled polygon with the given coordinates.
 void fillRect(int x, int y, int width, int height)
          Fills a rectangular area with the current color.
 void free()
          Frees any system resources (native device contexts) associated with the graphics object.
 Rect getClip(Rect r)
          Sets the x, y, width and height coordinates in the rectangle passed to the current clip coordinates.
 void setClip(int x, int y, int width, int height)
          Sets a clipping rectangle.
 void setColor(int r, int g, int b)
          Sets the current color for drawing operations.
 void setDrawOp(int drawOp)
          Sets the drawing operation.
 void setFont(Font font)
          Sets the current font for operations that draw text.
 java.lang.String toString()
           
 void translate(int x, int y)
          Translates the origin of the current coordinate system by the given x and y values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DRAW_OVER

public static final int DRAW_OVER
The constant for a draw operation that draws the source over the destination.

DRAW_AND

public static final int DRAW_AND
The constant for a draw operation that AND's the source with the destination. Commonly used to create image masks.

DRAW_OR

public static final int DRAW_OR
The constant for a draw operation that OR's the source with the destination. Commonly used with image masks.

DRAW_XOR

public static final int DRAW_XOR
The constant for a draw operation that XOR's the source with the destination.
Constructor Detail

Graphics

public Graphics(ISurface surface)
Constructs a graphics object which can be used to draw on the given surface. For the sake of the methods in this class, the given surface is known as the "current surface".

If you are trying to create a graphics object for drawing in a subclass of control, use the createGraphics() method in the Control class. It creates a graphics object and translated the origin to the origin of the control.

Method Detail

clearClip

public void clearClip()
Clears the current clipping rectangle. This allows drawing to occur anywhere on the current surface.

copyRect

public void copyRect(ISurface surface,
                     int x,
                     int y,
                     int width,
                     int height,
                     int dstX,
                     int dstY)
Copies a rectangular area from a surface to the given coordinates on the current surface. The copy operation is performed by combining pixels according to the setting of the current drawing operation. The same surface can be used as a source and destination to implement quick scrolling.

Not all combinations of surfaces are supported on all platforms. PalmOS has problems copying from an Window surface to an Image and between two Image surfaces. Java doesn't allow copying from an Window surface to an Image.

Parameters:
surface - the surface to copy from
x - the source x coordinate
y - the source y coordinate
width - the width of the area on the source surface
height - the height of the area on the source surface
dstX - the destination x location on the current surface
dstY - the destination y location on the current surface
See Also:
setDrawOp(int)

free

public void free()
Frees any system resources (native device contexts) associated with the graphics object. After calling this function, the graphics object can no longer be used to draw. Calling this method is not required since any system resources allocated will be freed when the object is garbage collected. However, if a program uses many graphics objects, free() should be called whenever one is no longer needed to prevent allocating too many system resources before garbage collection can occur.

drawText

public void drawText(char[] chars,
                     int start,
                     int count,
                     int x,
                     int y)
Draws text at the given coordinates. The x and y coordinates specify the upper left hand corner of the text's bounding box.
Parameters:
chars - the character array to display
start - the start position in array
count - the number of characters to display
x - the left coordinate of the text's bounding box
y - the top coordinate of the text's bounding box

drawImage

public void drawImage(Image image,
                      int x,
                      int y)
Draws an image at the given x and y coordinates.

drawCursor

public void drawCursor(int x,
                       int y,
                       int width,
                       int height)
Draws a cursor by XORing the given rectangular area on the surface. Since it is XORed, calling the method a second time with the same parameters will erase the cursor.

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a line at the given coordinates. The drawing includes both endpoints of the line.

drawRect

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

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

drawDots

public void drawDots(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a dotted line at the given coordinates. Dotted lines must be either horizontal or vertical, they can't be drawn at arbitrary angles.

drawPolygon

public void drawPolygon(int[] x,
                        int[] y,
                        int count)
Draws the outline of a polygon with the given coordinates. The polygon is automatically closed, you should not duplicate the start point to close the polygon.
Parameters:
x - x vertex coordinates
y - y vertex coordinates
count - number of vertices

fillPolygon

public void fillPolygon(int[] x,
                        int[] y,
                        int count)
Draws a filled polygon with the given coordinates. The polygon is automatically closed, you should not duplicate the start point to close the polygon. The polygon is filled according to Jordan's rule - a point is inside if a horizontal line to the point intersects the polygon an odd number of times. This function is not implemented for the PalmOS VM. Under PalmOS only the outline of the polygon is drawn.
Parameters:
x - x vertex coordinates
y - y vertex coordinates
count - number of vertices

drawText

public void drawText(java.lang.String s,
                     int x,
                     int y)
Draws text at the given coordinates. The x and y coordinates specify the upper left hand corner of the text.

fillRect

public void fillRect(int x,
                     int y,
                     int width,
                     int height)
Fills a rectangular area with the current color.

setClip

public void setClip(int x,
                    int y,
                    int width,
                    int height)
Sets a clipping rectangle. Anything drawn outside of the rectangular area specified will be clipped. Setting a clip overrides any previous clip.

getClip

public Rect getClip(Rect r)
Sets the x, y, width and height coordinates in the rectangle passed to the current clip coordinates. To reduce the use of temporary objects during drawing, this method does not allocate its own rectangle object. If there is no current clip, null will be returned and the rectangle passed will remain unchanged. Upon success, the rectangle passed to the method will be returned.

setColor

public void setColor(int r,
                     int g,
                     int b)
Sets the current color for drawing operations.
Parameters:
r - the red value (0..255)
g - the green value (0..255)
b - the blue value (0..255)

setDrawOp

public void setDrawOp(int drawOp)
Sets the drawing operation. The setting determines the raster operation to use when drawing lines, rectangles, text and images on the current surface. It also determines how pixels are combined when copying one surface to another. The setting of DRAW_OVER, where any drawing simply draws over the pixels on a surface, is the default.

Not all operations are supported on all platforms. When used with Java, DRAW_OVER is supported for all types of drawing and DRAW_XOR is supported for drawing lines, rectangles, text and images. However, DRAW_XOR is not supported when copying surface areas and the DRAW_AND and DRAW_OR operations aren't supported at all under Java.

PalmOS platforms supports all the drawing operations when drawing images and copying surface regions. However, only the DRAW_OVER operation is supported when drawing lines, rectangles and text. If you need to use the XOR drawing operation for drawing lines under PalmOS, you can draw the line into an image and then draw the image with an XOR drawing operation.

Win32 and Windows CE platforms support all the drawing operations except when drawing text. Only DRAW_OVER is supported when drawing text. If you need to draw XOR'ed text, you can draw the text into an image and then draw the image with an XOR draw operation.

When calculating the result of XOR, AND and OR drawing, the value of the color black is all 1's (fully set) in binary and white is all 0's (fully unset).

Parameters:
op - drawing operation
See Also:
DRAW_OVER, DRAW_AND, DRAW_OR, DRAW_XOR

setFont

public void setFont(Font font)
Sets the current font for operations that draw text.

translate

public void translate(int x,
                      int y)
Translates the origin of the current coordinate system by the given x and y values.