Code sample: Drawing a triangle using VGField

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.openvg.*;


public class SimpleVGField extends UiApplication 
{
    public static void main(String[] args)
    {
        SimpleVGField app = new SimpleVGField();
        app.enterEventDispatcher();
    }

    public SimpleVGField()
    {
        pushScreen(new HomeScreen());
    }
}

class HomeScreen extends MainScreen
{
	public HomeScreen()
	{
		add(new MyVGField(VGField.VERSION_1_1));
	}
}


class MyVGField extends VGField
{
    private int _fillPaint;
    private int _strokePaint;
    private int _path;
    
    private ByteBuffer  _pathSegments;
    private FloatBuffer _pathData;
    
    protected MyVGField(int version)
    {
        super(version);
    }
    
    protected void layout(int width, int height)
    {
    	setExtent(200,200);
    }


    protected void initialize(VG vg)
    {
        VG11 vg11 = (VG11)vg;

        vg11.vgSeti(VG11.VG_MATRIX_MODE, VG11.VG_MATRIX_PATH_USER_TO_SURFACE);
        vg11.vgLoadIdentity();

        vg11.vgSeti(VG11.VG_MATRIX_MODE, VG11.VG_MATRIX_FILL_PAINT_TO_USER);
        vg11.vgLoadIdentity();
        
        vg11.vgSeti(VG11.VG_MATRIX_MODE, VG11.VG_MATRIX_STROKE_PAINT_TO_USER);
        vg11.vgLoadIdentity();

        // Set clear color
        float[] clearColor = new float[] { 0.6f, 0.8f, 1.0f, 0.6f };
        vg11.vgSetfv(VG11.VG_CLEAR_COLOR, 4, clearColor, 0);

        // Create fill paint
        _fillPaint = vg11.vgCreatePaint();
        vg11.vgSetParameteri(_fillPaint, VG11.VG_PAINT_TYPE, VG11.VG_PAINT_TYPE_COLOR);
        vg11.vgSetColor(_fillPaint, 0xFFFF00FF);

        // Create stroke paint
        _strokePaint = vg11.vgCreatePaint();
        vg11.vgSetParameteri(_strokePaint, VG11.VG_PAINT_TYPE, VG11.VG_PAINT_TYPE_COLOR);
        vg11.vgSetColor(_strokePaint, 0x000000FF);
        vg11.vgSetf(VG11.VG_STROKE_LINE_WIDTH, 3.0f);

        // Create path
        _path = vg11.vgCreatePath(VG11.VG_PATH_FORMAT_STANDARD,VG11.VG_PATH_DATATYPE_F,
                  1.0f, 0.0f, 4, 3, VG11.VG_PATH_CAPABILITY_ALL);
        
        // Subdata the path with its segments and coordinate data using java.nio buffers
        _pathSegments = ByteBuffer.wrap(
        new byte[]
        {
            VG10.VG_MOVE_TO_ABS,
            VG10.VG_LINE_TO_ABS,
            VG10.VG_LINE_TO_ABS,
            VG10.VG_CLOSE_PATH
        });
        _pathData = FloatBuffer.wrap(
        new float[]
        {
            -100.0f, -86.60254f,
            100.0f, -86.60254f,
            0.0f, 86.60254f
        });
        
        vg11.vgAppendPathData(_path, _pathSegments, _pathData);
	}
	
	protected void render(VG vg)
	{
								VG11 vg11 = (VG11)vg;
		 					// Clear the surface
        vg11.vgClear(0, 0, Display.getWidth(), Display.getHeight());
        vg11.vgSeti(VG11.VG_MATRIX_MODE, VG11.VG_MATRIX_PATH_USER_TO_SURFACE);
        vg11.vgLoadIdentity();

        // Move the triangle to the center of the screen.
        vg11.vgTranslate(100, 100);
        vg11.vgSetPaint(_fillPaint, VG11.VG_FILL_PATH);
        vg11.vgSetPaint(_strokePaint, VG11.VG_STROKE_PATH);
        vg11.vgDrawPath(_path, VG11.VG_FILL_PATH | VG11.VG_STROKE_PATH);
        
	}
	

}
Next topic: Animation
Previous topic: VGField

Was this information helpful? Send us your comments.