VGField

The VGField class enables you to use the OpenVG™ library by including code that handles EGL™ setup. VGField also includes built-in support for refreshing its display at a specified target frame rate. It is a convenient choice for 2-D gaming and animation applications.

VGField is an abstract class. You must extend it to create a UI component that you can add to a screen. Your subclass must include a constructor and override layout(), initialize(), and render().

The constructor can call the parent class constructor by invoking super()and passing in the version, VGField.VERSION_1_1, of OpenVG that you want to use. In initialize(), you write code that performs setup tasks, such as setting the color to use to clear the screen. In layout(), you set the size of the field.

You can use setTargetFrameRate()to create a rendering thread that calls render() at the frame rate that you specify. You do not have to override update(), but if you do, it is also called at the target frame rate. The rendering started by setTargetFrameRate() runs on the event thread that you create when you call enterEventDispatcher(). You should set a target frame rate using setTargetFrameRate() instead of manually throttling rendering yourself. Thirty frames per second is generally the lower bound for smooth real-time graphics. Sixty frames per second is the upper limit because of the LCD refresh rate. A higher frame rate consumes more battery power. The frame rate can be set dynamically.

If you do not specify a target frame rate, it defaults to 0 and paint(), render(), and update() are called from the standard UI event thread in response to invalidate(). Note that initialize() is called when the EGL context is lost and when the field is removed and added back to the screen.

Code sample: Minimal VGField implementation

The following code sample provides simple implementations for the required methods. You can create a MyVGField class and add it to a screen.

import net.rim.device.api.openvg.VGField;

public class MyVGField extends VGField
{
     private static final float[] MY_CLEAR_COLOR = new float[] { 0.6f, 0.8f, 1.0f, 1.0f };
     
     public MyVGField(int version)
     {
         super(version);
     }
     protected void layout(int width, int height)
     {
         setExtent(100, 100);
     }
     
     protected void initialize(VG vg)
     {
         VG11 vg11 = (VG11)vg;
         vg11.vgSetfv(VG10.VG_CLEAR_COLOR, 4, MY_CLEAR_COLOR, 0);
     }
     
     protected void render(VG vg)
     {
         VG11 vg11 = (VG11)vg;
         vg11.vgClear(0, 0, this.getWidth(), this.getHeight());
     }
}
Previous topic: OpenVG

Was this information helpful? Send us your comments.