Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6538053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:41:24+00:00 2026-05-25T10:41:24+00:00

This class use to draw ball but its onDraw() function not call so ball

  • 0

This class use to draw ball but its onDraw() function not call so ball not draw

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 < 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

        }

  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:41:24+00:00Added an answer on May 25, 2026 at 10:41 am

    Probably a duplication of Extended SurfaceView's onDraw() method never called. Try adding setWillNotDraw(false) to your constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is Big O notation? Do you use it? I missed this university class
I tried to use this on my class library mylib.core.data.dll and got a successful
I am trying to use this in my page class. I only just started
I have a c++ header file containing a class. I want to use this
Given this class class Foo { // Want to find _bar with reflection [SomeAttribute]
Given a class like this: class Foo { public: Foo(int); Foo(const Foo&); Foo& operator=(int);
Given a declaration like this: class A { public: void Foo() const; }; What
I came across this class while reading a C# book and have some questions.
I have this class called Table: class Table { public string Name { get
I have this class. public class Foo { public Guid Id { get; set;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.