Why can't I paint more than 5 balls?

Problems with Canvas, OpenGL, etc...

Why can't I paint more than 5 balls?

Postby donaldsg2003 » Thu Mar 17, 2011 2:27 pm

I am new to Android development and have been starting with the basics (making balls bounce around) and am now working with painting the balls from an array. I have also been messing with the onTouchEvent trying to be able to click on a ball, but so far only got touching the screen to produce any results (not the individual balls). I have been able to produce the balls, but can only have up to 5 at any time and want to know what I could do to add more if I want to, plus add the onTouch to do something with the balls (ie make them disappear when touched, or take a score..). I would also like to add some ball to ball collision. Any help or advice would be greatly appreciated. This is what I am using to produce the balls. Oh and one other thing, when the second color balls are drawn to the canvas the top and bottom boundaries are moved up for some reason that I can't figure out.

Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. package local.CatchThisBall;
  2.  
  3.  
  4. import android.graphics.Canvas;
  5. import android.graphics.Paint;
  6. import android.graphics.RectF;
  7.  
  8. import java.util.Random;
  9.  
  10.  
  11. public class Balls {
  12.    
  13.     Random r = new Random();
  14.     public static int score = 0;
  15.    
  16.     int nx = r.nextInt(600);  //just using this for now for screen width/height
  17.     int ny = r.nextInt(900);
  18.    
  19.     int radius = r.nextInt(40);
  20.     int speedX = r.nextInt(100);
  21.     int speedY = r.nextInt(100);
  22.     float x = radius + nx;// these are the start
  23.                           // positions on the screen
  24.     float y = radius + ny;
  25.     private final RectF bounds;
  26.     private final Paint paint;
  27.  
  28.     public Balls(int color) {   //the ball's characteristics
  29.         bounds = new RectF();
  30.         paint = new Paint();
  31.         paint.setColor(color);
  32.         paint.setAntiAlias(true);
  33.         paint.setStyle(Paint.Style.FILL);
  34.     }
  35.  
  36.     public void draw(Canvas c) {
  37.         bounds.set(x - radius, y - radius, x + radius, y + radius);
  38.         c.drawOval(bounds, paint);
  39.  
  40.     }
  41.  
  42.     public void moveAndCollide(Box box) {  //want to add ball to ball collision
  43.         // get our new position
  44.         x += speedX;
  45.         y += speedY;
  46.         // check for collision
  47.         if (x + radius > box.xMax) {
  48.             speedX = -speedX;// if hits the right wall reverse x
  49.             x = box.xMax - radius;
  50.         } else if (x - radius < box.xMin) {
  51.             speedX = -speedX;// if hits the left wall reverse x
  52.             x = box.xMin + radius;
  53.         }
  54.         if (y + radius > box.yMax) {
  55.             speedY = -speedY;// if hits the bottom reverse
  56.             y = box.yMax - radius;
  57.  
  58.         } else if (y - radius < box.yMin) {
  59.             speedY = -speedY;// if hits the top reverse
  60.             y = box.yMin + radius;
  61.         }
  62.  
  63.     }
  64.  
  65. }
  66.  
Parsed in 0.008 seconds, using GeSHi 1.0.8.4


and here is how I am calling from the array
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. public class GameView extends View {
  2.     ArrayList<Balls> blueballs = new ArrayList<Balls>();
  3.     ArrayList<Balls> redballs = new ArrayList<Balls>();
  4.  
  5.     private Balls blueBalls;
  6.     private final Box box;
  7.     private Balls redBalls;
  8.  
  9.     private final StatusMessage statusMessage;
  10.  
  11.     public GameView(Context context) {
  12.         super(context);
  13.         int numOfBalls1 = 1;
  14.         int numOfBalls2 = 4;
  15.  
  16.         box = new Box(0xff00003f);
  17.  
  18.         for (int b = 0; b < numOfBalls1; b++) {
  19.             blueBalls = new Balls(Color.BLUE);
  20.             blueballs.add(blueBalls);
  21.  
  22.         }
  23.         for (int b = 0; b < numOfBalls2; b++) {
  24.             redBalls = new Balls(Color.RED);
  25.             redballs.add(redBalls);
  26.         }
  27.  
  28.         statusMessage = new StatusMessage(Color.CYAN);
  29.  
  30.         // enable touch mode
  31.         this.setFocusableInTouchMode(true);
  32.         // could also add keyboard mode here for future needs.
  33.         this.setFocusable(true);
  34.         this.requestFocus();
  35.     }
  36.  
  37.     @Override
  38.     // on first create or screen size change
  39.     public void onSizeChanged(int w, int h, int oldW, int oldH) {
  40.         box.set(0, 0, w, h);
  41.     }
  42.  
  43.     // called after invalidate
  44.     @Override
  45.     protected void onDraw(Canvas c) {
  46.  
  47.         box.draw(c);
  48.         for (int i = 0; i < blueballs.size(); i++) {
  49.             Balls newBall = blueballs.get(i);
  50.             newBall.draw(c);
  51.             blueballs.get(i).moveAndCollide(box);
  52.             c.restore();
  53.         }
  54.         for (int i = 0; i < redballs.size(); i++) {
  55.             Balls newBall = redballs.get(i);
  56.             newBall.draw(c);
  57.             redballs.get(i).moveAndCollide(box);
  58.             c.restore();
  59.         }
  60.  
  61.         statusMessage.draw(c);
  62.  
  63.         statusMessage.update(blueBalls);
  64.  
  65.         // our delay
  66.         try {
  67.             Thread.sleep(30);
  68.         } catch (InterruptedException e) {
  69.         }
  70.         // force a redraw
  71.         invalidate();
  72.     }
  73.  
  74. }
Parsed in 0.008 seconds, using GeSHi 1.0.8.4
donaldsg2003
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 16, 2011 6:45 pm

Top

Re: Why can't I paint more than 5 balls?

Postby svii » Sat Mar 19, 2011 5:05 am

you probably should ask one question at the time...

I didn't try to launch code... but what happens, if you change values for these:
int numOfBalls1 = 1;
int numOfBalls2 = 4;
svii
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Mar 19, 2011 4:46 am

Re: Why can't I paint more than 5 balls?

Postby donaldsg2003 » Sat Mar 19, 2011 5:54 pm

if the total goes above five, either a combination or one at a time, the application will not run. I originally had 12 or 13 running at the same time, but when I put them into an array it won't work the same.
donaldsg2003
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 16, 2011 6:45 pm

Re: Why can't I paint more than 5 balls?

Postby raj_J2ME » Wed Mar 23, 2011 8:05 am

donaldsg2003 wrote:if the total goes above five, either a combination or one at a time, the application will not run. I originally had 12 or 13 running at the same time, but when I put them into an array it won't work the same.

Can you write the code snippet which is running and which is not?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Re: Why can't I paint more than 5 balls?

Postby donaldsg2003 » Wed Mar 23, 2011 1:59 pm

This doesn't work
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1.    public GameView(Context context) {
  2.         super(context);
  3.         int numOfBalls1 = 2;
  4.         int numOfBalls2 = 4;
  5.  
  6.         box = new Box(0xff00003f);
  7.  
  8.         for (int b = 0; b < numOfBalls1; b++) {
  9.             blueBalls = new Balls(Color.BLUE);
  10.             blueballs.add(blueBalls);
  11.             this.setFocusableInTouchMode(true);
  12.  
  13.         }
  14.         for (int b = 0; b < numOfBalls2; b++) {
  15.             redBalls = new Balls(Color.RED);
  16.             redballs.add(redBalls);
  17.             this.setFocusableInTouchMode(true);
  18.         }
  19.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4

and this doesn't work
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1.    public GameView(Context context) {
  2.         super(context);
  3.         int numOfBalls1 = 1;
  4.         int numOfBalls2 = 5;
  5.  
  6.         box = new Box(0xff00003f);
  7.  
  8.         for (int b = 0; b < numOfBalls1; b++) {
  9.             blueBalls = new Balls(Color.BLUE);
  10.             blueballs.add(blueBalls);
  11.             this.setFocusableInTouchMode(true);
  12.  
  13.         }
  14.         for (int b = 0; b < numOfBalls2; b++) {
  15.             redBalls = new Balls(Color.RED);
  16.             redballs.add(redBalls);
  17.             this.setFocusableInTouchMode(true);
  18.         }
  19.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4

This does
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1.   public GameView(Context context) {
  2.         super(context);
  3.         int numOfBalls1 = 1;
  4.         int numOfBalls2 = 4;
  5.  
  6.         box = new Box(0xff00003f);
  7.  
  8.         for (int b = 0; b < numOfBalls1; b++) {
  9.             blueBalls = new Balls(Color.BLUE);
  10.             blueballs.add(blueBalls);
  11.             this.setFocusableInTouchMode(true);
  12.  
  13.         }
  14.         for (int b = 0; b < numOfBalls2; b++) {
  15.             redBalls = new Balls(Color.RED);
  16.             redballs.add(redBalls);
  17.             this.setFocusableInTouchMode(true);
  18.         }
  19.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4

and this does
Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1.  public GameView(Context context) {
  2.         super(context);
  3.         int numOfBalls1 = 1;
  4.         int numOfBalls2 = 3;
  5.         int numOfBalls3 = 1;
  6.         box = new Box(0xff00003f);
  7.  
  8.         for (int b = 0; b < numOfBalls1; b++) {
  9.             blueBalls = new Balls(Color.BLUE);
  10.             blueballs.add(blueBalls);
  11.             this.setFocusableInTouchMode(true);
  12.  
  13.         }
  14.         for (int b = 0; b < numOfBalls2; b++) {
  15.             redBalls = new Balls(Color.RED);
  16.             redballs.add(redBalls);
  17.             this.setFocusableInTouchMode(true);
  18.         }
  19.         for (int b = 0; b < numOfBalls3; b++) {
  20.             greenBalls = new Balls(Color.GREEN);
  21.             greenballs.add(greenBalls);
  22.             this.setFocusableInTouchMode(true);
  23.         }
  24.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

as long as the number of balls is five or less it runs in any combination. Anything above five though it fails to launch.

As far as the other code that I had running with like 12 balls, I just didn't use the array to store the balls, declared each as its own variable (private Balls blueBall1, blueBall2...) and then called each one individually in the onDraw method.
donaldsg2003
Freshman
Freshman
 
Posts: 3
Joined: Wed Mar 16, 2011 6:45 pm

Re: Why can't I paint more than 5 balls?

Postby raj_J2ME » Tue Mar 29, 2011 8:26 am

There is something which went wrong while you are initializing the balls. I would recommend you to share the error/exception which you are getting if the number of the balls went more than 5.
Like is this ArrayIndexOutofBound or something else?

Another suggestion, you try to debug the code and try to find the exact location of the error, so that you can be sure that what is the reason for the error. Like you can try to put the System.out.println("") statements to check the flow of the code and the progress.

Check and revert.
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests