title: Basic bouncing ball program, ball won't bounce off wall
body: <p>So I'm supposed to make a program where a ball bounces around a drawingpanel for 10 seconds. The ball has to bounce off the sides of the panel if it hits them. Right now when the ball hits the bottom panel instead of bouncing it appears in the middle of the screen and moves in the opposite direction until it hits the top and disappears.</p>
  
  <p>I'm pretty sure the problem is in this part of my code...
  (Earlier in the code I declared x to 1, y to 250, dx to 1, and dy to 1)</p>
  
  <pre><code>//Changes dirction 
  public static int newDirection1(int x, int dx, int size){
    if (x &lt; 0 || x &gt; 500 || (x + size) &lt; 0 || (x + size) &gt; 500)  {
      dx *= -1;
      return dx;
    } else {
      return dx;
    } 
  } 
  
  //Changes direction 
  public static int newDirection2(int y, int dy, int size){
    if (y &lt; 0 || y &gt; 500 || (y + size) &lt; 0 || (y + size) &gt; 500)  {
      dy *= -1;
      return dy;
    } else {
      return dy;
    } 
   } 
  
   //Moves ball one step
   public static void move(Graphics g, Color color, int size, int x1, int y1, int x2, int y2){
  
     g.setColor(Color.WHITE);
     g.fillOval(x1, y1, size, size);
     g.setColor(color);
     g.fillOval(x2, y2, size, size);
  
  
   } 
  
   //Pauses for 10ms
   public static void sleep(int millis, DrawingPanel panel){
     panel.sleep(millis);
   } 
  
  
  public static void bounceLoop(DrawingPanel panel, Graphics g, Color color, int size, int x, int dx, int y, int dy, int millis){
  
     int x1 = x + dx;
     int x2 = x + dx;
     int y1 = y + dy;
     int y2 = y + dy;
  
     for (int i = 0; i &lt; 1000; i++) {
  
       x1 = x + dx * i;
       x2 = (x + dx * i) + dx;
       y1 = y + dy * i;
       y2 = (y + dy * i) + dy;
       dx = newDirection1(x2, dx, size);
       dy = newDirection2(y2, dy, size); 
       move(g, c, size, x1, y1, x2, y2);
       sleep(millis, panel);
  
     } 
    } 
  
  } 
  </code></pre>