Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to draw ball using onDraw() in android
    text
    copied!<p>This class use to draw ball but its onDraw() function not call so ball not draw</p> <pre><code>package com.bounce.app; import org.jbox2d.collision.AABB; import org.jbox2d.collision.shapes.CircleDef; import org.jbox2d.collision.shapes.PolygonDef; import org.jbox2d.common.Vec2; import org.jbox2d.dynamics.Body; import org.jbox2d.dynamics.BodyDef; import org.jbox2d.dynamics.World; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; public class PhysicsWorld extends SurfaceView implements SurfaceHolder.Callback { private AABB worldAABB; private World world; private PolygonDef groundShapeDef; public int W_width, W_height; private static final String TAG = PhysicsWorld.class.getSimpleName(); protected static final int GUIUPDATEIDENTIFIER = 0x231; public int targetFPS = 40; public float timeStep = (10.0f/targetFPS); public int iterations = 5; private int count=0; private Body[] theBodies ; private Paint paint; private float radius = 10; public PhysicsWorld(Context context, int width, int height) { super(context); getHolder().addCallback(this); W_width = width; W_height = height; worldAABB = new AABB(); Vec2 min = new Vec2(-50, -50); Vec2 max = new Vec2(W_width+50, W_height+50); worldAABB.lowerBound.set(min); worldAABB.upperBound.set(max); Vec2 gravity = new Vec2((float) 0.0, (float) -9.8); boolean doSleep = true; world = new World(worldAABB, gravity, doSleep); BodyDef groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0)); Body groundBody = world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox(W_width, 10); groundBody.createShape(groundShapeDef); // up : groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) 0.0, (float) (W_height + 10.0))); groundBody = world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox(W_width, 10); groundBody.createShape(groundShapeDef); // left : groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2(-10, (float) 0.0)); groundBody = world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox(10, W_height); groundBody.createShape(groundShapeDef); // right : groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) W_width + 10, (float) 0.0)); groundBody = world.createBody(groundBodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox(10, W_height); groundBody.createShape(groundShapeDef); theBodies = new Body[50]; paint = new Paint(); paint.setStyle(Style.FILL); paint.setColor(Color.RED); } public void addBall(int x, int y) { // Create Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.position.set(x, y); Log.d(TAG,"Ball Created At: " + Integer.toString(x) + "," + Integer.toString(y)); theBodies[count] = world.createBody(bodyDef); // Create Shape with Properties CircleDef circle = new CircleDef(); circle.radius = radius; circle.density = (float) 1.0; circle.restitution = 0.5f; theBodies[count].createShape(circle); theBodies[count].setMassFromShapes(); count+=1; } public void update() { world.step(timeStep, iterations); postInvalidate(); } @Override protected void onDraw(Canvas canvas) { Log.v("@@@@*****", "draw ball"); paint = new Paint(); paint.setStyle(Style.FILL); paint.setColor(Color.RED); for (int j = 0; j &lt; count; j++) { canvas.drawCircle(theBodies[j].getPosition().x, W_height - theBodies[j].getPosition().y, radius, paint); Log.v("@@@@*****", "draw ball"); } } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.v("@@@@*****", "Add ball"); addBall((int)event.getX(),(int)event.getY()); } return true; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { Log.d(TAG,"Surface Created"); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload