Help Center

Local Navigation

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 an instance of an XYPoint object and an XYRect object.
    XYPoint bottomRight = new XYPoint(50, 50);
    XYRect rectangle = new XYRect(topLeft, bottomRight);
    XYPoint topLeft = new XYPoint(10, 10);
  3. Invoke Graphics.pushContext() to make drawing calls that specify that the region origin should not adjust the drawing offset. In the following code sample, we create two XYPoint objects to represent the top left and bottom right points of a rectangle. We then create a rectangular clipping region by creating an XYRect object using the XYPoint objects. We invoke Graphics.pushContext() to push the rectangular clipping region to the context stack. We invoke Graphics.drawRect() to draw a rectangle and invoke Graphics.fillRect() to fill a rectangle. We invoke Graphics.popContext() to pop the current context off of the context stack.
    XYPoint bottomRight = new XYPoint(50, 50);
    XYPoint topLeft = new XYPoint(10, 10);
    XYRect rectangle = new XYRect(topLeft, bottomRight);
    
    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. In the following code sample, we invoke Graphics.drawRect() to draw a rectangle and invoke Graphics.fillRect() to fill a rectangle. We 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. Invert a specified XYRect object.
  6. Specify the portion of the Graphics object to push onto the stack.
  7. After you invoke pushContext() (or pushRegion()), provide the portion of the Graphics object to invert.
    graphics.pushContext(rectangle);
    graphics.invert(rectangle);
    graphics.popContext();
  8. 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);

Index


Was this information helpful? Send us your comments.