Protected void ondraw (canvas) {// drawing entry in view/** traverse the body in box2d world and draw its shape */For (body B = World. getbodylist (); B! = NULL; B = B. getnext () {XF. Set (B. gettransform (); For (fixture F = B. getfixturelist (); F! = NULL; F = f. getnext () {If (B. isactive () = false) {color. set (0.5f, 0.5f, 0.3f); drawshape (canvas, F, XF, color);} else if (B. getType () = bodytype. static) {color. set (0.5f, 0.9f, 0.3f); drawshape (canvas, F, XF, color);} else if (B. getType () = bodytype. kinematic) {color. set (0.5f, 0.5f, 0.9f); drawshape (canvas, F, XF, color);} else if (B. isawake () = false) {color. set (0.5f, 0.5f, 0.5f); drawshape (canvas, F, XF, color);} else {color. set (0.9f, 0.7f, 0.7f); drawshape (canvas, F, XF, color );}}}} /*************************************** * // ****** drawbox2dworld ****//************************* * *************/private final transform XF = new transform (); private Final color3f color = new color3f (); private final vec2 center = new vec2 (); private final vec2 axis = new vec2 (); private final vec2array tlvertices = new vec2array (); public static final float ptm_ratio = 16.0f; // The ratio of the pixel to the actual size is 16 pixels corresponding to 1 M. Adjust the paint mpaint = new paint () as needed; Public int screen_w = 480; public int screen_h = 854;/** offset of the world (240) point. Use screen pixels */float offset_x = 427 for coordinates, offset_y =; // determine the shape type private void drawshape (canvas, fixture, transform XF, color3f color) {Switch (fixture. getType () {Case circle: {circleshape Circle = (circleshape) fixture. getshape (); transform. multoout (XF, circle. m_p, center); float radius = circle. m_radius; axis. set (XF. r. col1); mpaint. reset (); setcolor3f (mpaint, color); mpaint. setstyle (style. fill); canvas. drawcircle (xcoordinateformbox2d (center. x), ycoordinateformbox2d (center. y), radius * ptm_ratio, mpaint); mpaint. setcolor (color. white); mpaint. setstyle (paint. style. stroke); canvas. drawcircle (xcoordinateformbox2d (center. x), ycoordinateformbox2d (center. y), radius * ptm_ratio, mpaint);} break; Case polygon: {polygonshape poly = (polygonshape) fixture. getshape (); int vertexcount = Poly. m_vertexcount; vec2 [] vertices = tlvertices. get (vertexcount); For (INT I = 0; I <vertexcount; ++ I) {transform. multoout (XF, Poly. m_vertices [I], vertices [I]);} drawsolidpolygon (canvas, vertices, vertexcount, color);} break ;} // switch} // draw a public void drawsolidpolygon (canvas, vec2 [] vertices, int vertexcount, color3f color) {mpaint. reset (); setcolor3f (mpaint, color); If (vertexcount> 2) {Path = New Path (); Path. moveTo (xcoordinateformbox2d (vertices [0]. x), ycoordinateformbox2d (vertices [0]. y); For (INT I = 1; I <vertexcount; I ++) {path. lineto (xcoordinateformbox2d (vertices [I]. x), ycoordinateformbox2d (vertices [I]. y);} path. close (); // fill mpaint. setstyle (paint. style. fill); canvas. drawpath (path, mpaint); // draw the edge mpaint. setcolor (color. white); mpaint. setstyle (paint. style. stroke); canvas. drawpath (path, mpaint);} else if (vertexcount = 2) {setcolor3f (mpaint, color); mpaint. setstyle (paint. style. fill); canvas. drawline (xcoordinateformbox2d (vertices [0]. x), ycoordinateformbox2d (vertices [0]. y), xcoordinateformbox2d (vertices [1]. x), ycoordinateformbox2d (vertices [1]. y), mpaint); mpaint. setcolor (color. white); mpaint. setstyle (paint. style. stroke); canvas. drawline (xcoordinateformbox2d (vertices [0]. x), ycoordinateformbox2d (vertices [0]. y), xcoordinateformbox2d (vertices [1]. x), ycoordinateformbox2d (vertices [1]. y), mpaint);} else if (vertexcount = 1) {setcolor3f (mpaint, color); mpaint. setstyle (paint. style. fill); canvas. drawpoint (xcoordinateformbox2d (vertices [0]. x), ycoordinateformbox2d (vertices [0]. y), mpaint) ;}} public float xcoordinateformbox2d (float X) {return offset_x + x * ptm_ratio;} // The canvas coordinates of box2d are inconsistent with those used by the canvas, public float ycoordinateformbox2d (float y) {return screen_h-offset_y-y * ptm_ratio ;}} /** static variables and Methods ********************************* * ***********************************/Final Static int full_algha = 0xff; static void setcolor3f (paint _ paint, color3f color) {int r = floatcolortoint (color. x); int G = floatcolortoint (color. y); int B = floatcolortoint (color. z); _ paint. setargb (full_algha, R, G, B);} static int floatcolortoint (float c) {return math. round (C * full_algha );}
Although I have already written some examples, but I have no time to add instructions.CodeI hope it will help you.
In fact, these basic things are already available in the jbox2d Java example.Source codeFollow-up: In most cases, the problem is not serious. I also learned from the official manuals through the source code.
With a demo:
: Http://download.csdn.net/detail/newliuli/4493775