Can't figure out this obstacle for ball animation
<p>I have a task in which I have to make a app in a <code>JFrame</code>.</p>

<p>It will contain 5 balls, which move randomly around the form, they can't exit the form and have to hit the borders. There's also an rectangle in the middle of the form, and that's the main problem, because I can't figure out how to make the ball's bounce off the rectangle.</p>

<p>I already started doing something but the balls just bounce off at some random places in the form.</p>

<blockquote>
  <p>Task:</p>
  
  <ul>
  <li>Create <code>JFrame</code> (Done)</li>
  <li>Create 5 <code>Balls</code>, which move around and spawn in random positions (Done)</li>
  <li>Create a <code>Rectangle</code> in the middle of the form (Done)</li>
  <li>Make the balls bounce off the rectangle. (Done)</li>
  </ul>
</blockquote>

<p>Figured it out in the end.</p>

<p>Here is my code:</p>

<pre><code>import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Main extends JPanel {

Random rn = new Random();
int blueX = rn.nextInt(650) + 1;
int blueY = rn.nextInt(650) + 1;

int redX = rn.nextInt(650) + 1;
int redY = rn.nextInt(650) + 1;

int yellowX = rn.nextInt(650) + 1;
int yellowY = rn.nextInt(650) + 1;

int greenX = rn.nextInt(650) + 1;
int greenY = rn.nextInt(650) + 1;

int magentaX = rn.nextInt(650) + 1;
int magentaY = rn.nextInt(650) + 1;

int blueAngleX = rn.nextInt(10) + 1;
int blueAngleY = rn.nextInt(20) + 1;

int redAngleX = rn.nextInt(10) + 1;
int redAngleY = rn.nextInt(50) + 1;

int yellowAngleX = rn.nextInt(40) + 1;
int yellowAngleY = rn.nextInt(50) + 1;

int greenAngleX = rn.nextInt(30) + 1;
int greenAngleY = rn.nextInt(20) + 1;

int magentaAngleX = rn.nextInt(20) + 1;
int magentaAngleY = rn.nextInt(50) + 1;

int rectX = 325, rectY = 325, rectW = 100, rectH = 100;

int speed = 5;

private void move() {

    rectContact();

    if (blueX + blueAngleX &lt; 0) {
        blueAngleX = speed;
    } else if (blueX + blueAngleX &gt; getWidth() - 30) {
        blueAngleX = -speed;
    } else if (blueY + blueAngleY &lt; 0) {
        blueAngleY = speed;
    } else if (blueY + blueAngleY &gt; getHeight() - 30) {
        blueAngleY = -speed;
    }

    blueX = blueX + blueAngleX;
    blueY = blueY + blueAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (redX + redAngleX &lt; 0) {
        redAngleX = speed;
    } else if (redX + redAngleX &gt; getWidth() - 30) {
        redAngleX = -speed;
    } else if (redY + redAngleY &lt; 0) {
        redAngleY = speed;
    } else if (redY + redAngleY &gt; getHeight() - 30) {
        redAngleY = -speed;
    }

    redX = redX + redAngleX;
    redY = redY + redAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (yellowX + yellowAngleX &lt; 0) {
        yellowAngleX = speed;
    } else if (yellowX + yellowAngleX &gt; getWidth() - 30) {
        yellowAngleX = -speed;
    } else if (yellowY + yellowAngleY &lt; 0) {
        yellowAngleY = speed;
    } else if (yellowY + yellowAngleY &gt; getHeight() - 30) {
        yellowAngleY = -speed;
    }

    yellowX = yellowX + yellowAngleX;
    yellowY = yellowY + yellowAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (greenX + greenAngleX &lt; 0) {
        greenAngleX = speed;
    } else if (greenX + greenAngleX &gt; getWidth() - 30) {
        greenAngleX = -speed;
    } else if (greenY + greenAngleY &lt; 0) {
        greenAngleY = speed;
    } else if (greenY + greenAngleY &gt; getHeight() - 30) {
        greenAngleY = -speed;
    }

    greenX = greenX + greenAngleX;
    greenY = greenY + greenAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (magentaX + magentaAngleX &lt; 0) {
        magentaAngleX = speed;
    } else if (magentaX + magentaAngleX &gt; getWidth() - 30) {
        magentaAngleX = -speed;
    } else if (magentaY + magentaAngleY &lt; 0) {
        magentaAngleY = speed;
    } else if (magentaY + magentaAngleY &gt; getHeight() - 30) {
        magentaAngleY = -speed;
    }

    magentaX = magentaX + magentaAngleX;
    magentaY = magentaY + magentaAngleY;

}

public void rectContact() {

    if (blueY + 30 &gt;= rectY &amp;&amp; (blueX &gt;= rectX - 25 &amp;&amp; blueX &lt;= rectX)
            || (blueY &gt;= rectY + 100 &amp;&amp; (blueX &gt;= rectX &amp;&amp; blueX &lt;= rectX + 100))) {
        blueAngleX = -speed;
    }
    if (blueX + 10 &gt;= rectX &amp;&amp; (blueY &gt;= rectY &amp;&amp; blueY &lt;= rectY - 25)
            || (blueX &gt;= rectX &amp;&amp; (blueY &gt;= rectY &amp;&amp; blueY &lt;= rectY))) {
        blueAngleY = -speed;
    }

}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.BLUE);
    g.fillOval(blueX, blueY, 30, 30);

    g.setColor(Color.RED);
    g.fillOval(redX, redY, 30, 30);

    g.setColor(Color.YELLOW);
    g.fillOval(yellowX, yellowY, 30, 30);

    g.setColor(Color.GREEN);
    g.fillOval(greenX, greenY, 30, 30);

    g.setColor(Color.MAGENTA);
    g.fillOval(magentaX, magentaY, 30, 30);

    g.setColor(Color.RED);
    g.fillRect(rectX, rectY, rectW, rectH);
}

public static void main(String[] args) throws InterruptedException {

    JFrame frame = new JFrame("Moving Ball!");
    Main main = new Main();
    frame.add(main);
    frame.setBounds(300, 0, 750, 750);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while (true) {
        main.move();
        main.repaint();
        Thread.sleep(10);
    }
}

}
</code></pre>