0

I'm trying to write a program that bounces a ball continuously inside a window for homework assignment. Here is a short youtube video of what I'm trying to do. As the program is written right now the ball does not move and I don't understand why.

I have two questions:

  1. What am I doing incorrectly for why the ball is not moving?
  2. Should I use the >= and <= operators instead of the > and < operators that I used inside of the IF (conditions)?

import acm.program.*;
import acm.graphics.*;

publc class BallBouncing extends GraphicsProgram{
public void run(){

   GOval ball = new GOval(getWidth() / 2, getHeight() / 2, OVAL_SIZE, OVAL_SIZE);
   ball.setFilled(true);
   add(ball);

   double dx = getWidth() / N_STEPS;
   double dy = getHeight() / N_STEPS;

   while(true){
       ball.move(dx, dy);
       pause(PAUSE_TIME);

        if (ball.getY() > getHeight() - OVAL_SIZE) { dy = dy * -1;}
        if (ball.getX() > getWidth() - OVAL_SIZE) { dx = dx * -1;}
        if (ball.getY() < 0 + OVAL_SIZE) { dy = dy * -1;}
        if (ball.getX() < 0 + OVAL_SIZE) { dx = dx * -1;}

      }
    }  

 private static final int STEPS = 1000;
 private static final int PAUSE_TIME = 5;
 private static final int OVAL_SIZE = 25;

 }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jessica M.
  • 1,451
  • 12
  • 39
  • 54
  • 2
    You're using API's that people don't know, but if they are based on AWT/Swing, then you are probably block the Event Dispatching Thread. I could list of half a dozen examples I've written here myself, but [this](http://stackoverflow.com/questions/13022754/java-bouncing-ball/13022788#13022788) should be a good start – MadProgrammer Apr 16 '13 at 07:12
  • The import acm.program.*; and acm.graphics.*; are the ones that I've learned so far. – Jessica M. Apr 16 '13 at 07:14
  • 1
    But `acm.*` is not a standard API – MadProgrammer Apr 16 '13 at 07:16
  • @AndrewThompson the question says while(true) because the program is supposed to run until you exit the window. It's just supposed to keep bouncing off the walls. – Jessica M. Apr 16 '13 at 07:18
  • 1
    Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Apr 16 '13 at 07:18
  • 1
    @JessicaM. You missed my point. It is obvious to me that you want the action to repeat, but the code as it is written ***blocks the EDT.*** – Andrew Thompson Apr 16 '13 at 07:20
  • @AndrewThompson I'm sorry I'm not familiar with EDT because I have not learned that in my book yet. – Jessica M. Apr 16 '13 at 07:23
  • 1
    *"I'm not familiar with EDT"* That is why I linked to 'Concurrency in Swing' above. You're not expected to know everything at first, but you *are* expected to read links we provide, ion order to help get yourself further. So, go.. read.. – Andrew Thompson Apr 16 '13 at 07:33
  • @AndrewThompson Thanks I'll definitely take a look at that. – Jessica M. Apr 16 '13 at 09:54

0 Answers0