title: Ball in Brick Breaker game destroys whole lines without touching them(Java)
body: <p>I have been making a brick breaker game for fun, but I came across an odd error in the execution of the program. When the game is started, the ball is set in motion away from the bricks. However, it starts destroying them almost immediately, starting from the brick furthest to the right on the upper row and continuing to break them all from right to left. Then it goes to the second row and destroys them from left to right. This whole time, on the screen, the ball is away from any bricks. </p>
  
  <p>I think I pinpointed the problem to the following piece of code. Here I create a rectangle around the ball and a rectangle around the brick and if they were to intersect, I call the method <code>setBrickValue</code> inside another class called <code>Map Generator</code>, which sets the value of the brick to 0, meaning, it doesn't exist.</p>
  
  <p>Here's the method:</p>
  
  <pre><code>    public void actionPerformed(ActionEvent arg0) {
              timer.start();
              //makes sure the ball collides with the paddle
              if(play){
                  if( new Rectangle( ballPositionX, ballPositionY, 20, 20)
                          .intersects( new Rectangle( player, 550, 100, 8 ) ) ){
                      ballDirY = - ballDirY;
                  }
                  // beginning of the code with an error in it, the rectangle around the ball
                  //is intersecting incorrectly the rectangle of the brick
                  A:for(int i = 0; i &lt; MGobj.map.length; i++){
                      for(int j = 0; j &lt; MGobj.map[0].length; j++){
                          if(MGobj.map [i] [j] &gt; 0){
                              int brickX = i * MGobj.brickWidth + 80;
                              int brickY = j * MGobj.brickHeight + 50;
                              int brickWidth = MGobj.brickWidth;
                              int brickHeight = MGobj.brickHeight;
  
                              //create the rectangle around the brick
                              Rectangle rect = new Rectangle( brickX, brickY,
                                      brickWidth, brickHeight );
  
                              //create the rectangle around the ball, so we can detect the intersection
                              Rectangle ballRect = new Rectangle( ballPositionX, ballPositionY, 20, 20 );
  
                              Rectangle brickRect = rect;
                              //when intersecting, delete the brick off the map
                              if( ballRect.intersects( brickRect )){
                                  MGobj.setBrickValue( 0, i, j );
                                  totalBricks--;
                                  score += 5;
                                  //
                                  if( ballPositionX + 19 &gt;= brickRect.x || 
                                           ballPositionX + 1  &lt;=  brickRect.x + brickRect.width  ){
                                      ballDirX = - ballDirX;
                                  }else{
                                      ballDirY = - ballDirY;
                                  }
                                  //creates a label that says when this code executes, get out of the 
                                  //outer loop
                                  break A;
                              }
                           }
                        }
                      }
                  //end of the code with an error in it
  
  
                  //makes sure the ball is moving
                  ballPositionX += ballDirX;
                  ballPositionY += ballDirY;
  
                  //makes sure the ball isn't going beyond the borders
                  if(ballPositionX &lt; 0){
                      ballDirX = -ballDirX;
                  }
                  if(ballPositionY &lt; 0){
                      ballDirY = -ballDirY;
                  }
                  //think this code doesn't work
                  if(ballPositionX &gt; 670){
                      ballDirX = -ballDirX;
                  }
              }
  
              repaint();
  
          }
  </code></pre>
  
  <p>Here's the <code>setBrickValue</code> method:</p>
  
  <pre><code>    public void setBrickValue(int value, int row, int col){     
            map [row] [col] = value;  
          }
  </code></pre>
  
  <p>All help is appreciated. Thank you in advance. </p>
  
  <p>PS: If anybody would like to see the whole code (400ish lines), just tell me and I would upload it.</p>