/*### Creates Oval ###*/
 private GOval createOval(int gWidth, int gHeight, int x, int y, int locX, int locY, Color col) {
   GOval Oval = new GOval(gWidth, gHeight, x, y);
   Oval.setLocation(locX, locY);
   Oval.setColor(col);
   Oval.setFilled(true);
   return Oval;
 }
Ejemplo n.º 2
0
  /** Check for collision */
  private void checkRebounce() {

    // check for left/right/top wall
    if (ball.getX() < 0) {
      vx *= -1;
      ball.move(vx, 0);
      return;
    } else if ((ball.getX() + 2 * BALL_RADIUS) > getWidth()) {
      vx *= -1;
      ball.move(vx, 0);
      return;
    } else if (ball.getY() < 0) {
      ball.setLocation(ball.getX(), -ball.getY());
      vy *= -1;
      return;
    }

    // check for collision with bricks: from the four vertexs
    lefttop = getElementAt(ball.getX(), ball.getY());
    righttop = getElementAt(ball.getX() + 2 * BALL_RADIUS, ball.getY());
    leftbottom = getElementAt(ball.getX(), ball.getY() + 2 * BALL_RADIUS);
    rightbottom = getElementAt(ball.getX() + 2 * BALL_RADIUS, ball.getY() + 2 * BALL_RADIUS);
    if ((lefttop != null) && (righttop != null) && (lefttop != board) && (lefttop != mylabel)) {
      remove(lefttop);
      num_brick_left--;
      vy *= -1;
    } else if ((righttop != null)
        && (rightbottom != null)
        && (righttop != board)
        && (righttop != mylabel)) {
      remove(righttop);
      num_brick_left--;
      vx *= -1;
    } else if ((rightbottom != null)
        && (leftbottom != null)
        && (rightbottom != board)
        && (rightbottom != mylabel)) {
      remove(rightbottom);
      num_brick_left--;
      vy *= -1;
    } else if ((leftbottom != null)
        && (lefttop != null)
        && (leftbottom != board)
        && (leftbottom != mylabel)) {
      remove(leftbottom);
      num_brick_left--;
      vx *= -1;
    }

    // check for collision with board
    if (getElementAt(ball.getX(), (ball.getY() + BALL_RADIUS / 2)) == board) {
      vy *= -1;
      ball.move(0, vy);
    }
  }
 /** Bind the ball to the mouse cursor */
 private void muvedBall(MouseEvent e) {
   if (startGame == 0) {
     int y = HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET - BALL_RADIUS * 2;
     int x;
     if (e.getX()
         > WIDTH
             - PADDLE_WIDTH
                 / 2) { // Makes it impossible to exit the ball over the right part of the window
       x = WIDTH - PADDLE_WIDTH / 2 - BALL_RADIUS;
     } else if (e.getX()
         < PADDLE_WIDTH
             / 2) { // Makes it impossible to exit the ball over the left part of the window
       x = PADDLE_WIDTH / 2 - BALL_RADIUS;
     } else {
       x = e.getX() - BALL_RADIUS; // Bind the ball to the center of the cursor
     }
     ball.setLocation(x, y);
   }
 }