Manage a drawing area

The Graphics object represents the entire drawing surface that is available to the BlackBerry® device application. To limit this area, divide it into XYRect objects. Each XYPoint represents a point on the screen, which is composed of an X co-ordinate and a Y co-ordinate.

  1. Import the following classes:
    • net.rim.device.api.ui.Graphics
    • net.rim.device.api.ui.XYRect
    • net.rim.device.api.ui.XYPoint
  2. Create XYPoint objects, to represent the top left and bottom right points of a rectangle. Create an XYRect object using the XYPoint objects, to create a rectangular clipping region.
    XYPoint bottomRight = new XYPoint(50, 50);
    XYPoint topLeft = new XYPoint(10, 10);
    XYRect rectangle = new XYRect(topLeft, bottomRight);
  3. Invoke Graphics.pushContext() to make drawing calls that specify that the region origin should not adjust the drawing offset. Invoke Graphics.pushContext() to push the rectangular clipping region to the context stack. Invoke Graphics.drawRect() to draw a rectangle, and invoke Graphics.fillRect() to fill the rectangle. Invoke Graphics.popContext() to pop the current context off of the context stack.
    graphics.pushContext(rectangle, 0, 0);
    graphics.fillRect(10, 10, 30, 30);
    graphics.drawRect(15, 15, 30, 30);
    graphics.popContext();
    graphics.drawRect(15, 15, 30, 30);
    graphics.pushContext(rectangle, 0, 0);
    graphics.fillRect(10, 10, 30, 30);
    graphics.drawRect(15, 15, 30, 30);
    graphics.popContext();
    graphics.drawRect(15, 15, 30, 30);
  4. Invoke pushRegion() and specify that the region origin should adjust the drawing offset. Invoke Graphics.drawRect() to draw a rectangle and invoke Graphics.fillRect() to fill a rectangle. Invoke Graphics.popContext() to pop the current context off of the context stack.
    graphics.pushRegion(rectangle);
    graphics.fillRect(10, 10, 30, 30);
    graphics.drawRect(15, 15, 30, 30);
    graphics.popContext();
  5. Specify the portion of the Graphics object to push onto the stack.
  6. After you invoke pushContext() (or pushRegion()), provide the portion of the Graphics object to invert.
    graphics.pushContext(rectangle);
    graphics.invert(rectangle);
    graphics.popContext();
  7. Invoke translate(). The XYRect is translated from its origin of (1, 1) to an origin of (20, 20). After translation, the bottom portion of the XYRect object extends past the bounds of the graphics context and clips it.
    XYRect rectangle = new XYRect(1, 1, 100, 100);
    XYPoint newLocation = new XYPoint(20, 20);
    rectangle.translate(newLocation);
Previous topic: Screen classes

Was this information helpful? Send us your comments.