title: Java ball object doesn't bounce off of drawn rectangles like it's supposed to.
body: <p>Sorry for the awful title.  The purpose of the Java applet is as such: A ball is bouncing around the screen.  The size and speed of this ball can be changed via scrollbars.  The user can press and drag the mouse on the screen to draw rectangles.  The ball will bounce off of these rectangles as well.  The bounds of these rectangles are stored in a vector.  When a rectangle is clicked, it (and all other rectangles at that point) are removed from the vector (and the screen).</p>
  
  <p>The problem I'm having is two-fold: One, when I click a rectangle to remove it, it doesn't get removed, but that can be solved later.  </p>
  
  <p>Two: The ball doesn't bounce off of the rectangles like it's supposed to.  When I draw a rectangle in either the same row or column as the ball, the ball bounces around inside of a tiny rectangle, like it's stuck.  </p>
  
  <p>Here's my code to detect if the ball is hitting the boundaries of either the applet or any of the rectangles:</p>
  
  <pre><code>  public void move()
    {
      //if it will hit the right or left boundary, flip the x direction and set it
      if (loc.x+size &gt;= boundx || loc.x &lt;= 0)
      { dx *= -1; }
      //if it will hit the top or bottom boundray, flip the y direction and set it
      if (loc.y+size &gt;= boundy || loc.y &lt;= 0)
      { dy *= -1; }
  
  
      for (int i = 0; i &lt; r.size(); i++)
      {
  
        temp = new Rectangle(r.elementAt(i));
        int rx = temp.x;
        int ry = temp.y;
        int rh = temp.height;
        int rw = temp.width;
        //If the ball hits either side of the rectangle, change the x direction
        if((loc.x &gt; rx &amp;&amp; loc.x &gt; ry &amp;&amp; loc.x &lt; (ry + rh))||(loc.x &lt; (rx + rw) &amp;&amp; loc.x &gt; rx &amp;&amp; loc.x &lt;(ry + rh)))
          {dx *= -1;}
        //If the ball hits either the top or bottom, change the y direction
        if((loc.y &gt; ry &amp;&amp; loc.y &gt; rx &amp;&amp; loc.y &lt; (rx + rw))||(loc.y &lt; (ry + rh) &amp;&amp; loc.y &gt; ry &amp;&amp; loc.y &lt;(rx + rw)))
          {dy *= -1;}
      }
      //Increment or decrement the location of the ball based on the X and Y directions.
      loc.x += dx;
      loc.y += dy;
    }
  </code></pre>
  
  <p>If you want to view the code in its entirety, it's here: <a href="http://ideone.com/R1hpBx" rel="nofollow">http://ideone.com/R1hpBx</a></p>
  
  <p>Thanks in advance for all the wonderful help. </p>