Example #1
0
  /** getReady: Initialize the game */
  private void getReady() {

    // Make up the bricks
    double brickPerWidth =
        (getWidth() - DIST_BRICK_WALL * 2 - DIST_BRICK_BRICK * (NUM_BRICK_ROW - 1)) / NUM_BRICK_ROW;
    double brickPerHeight = BRICK_HEIGHT;
    double x_start = DIST_BRICK_WALL;
    double y_start;
    for (int i = 1; i <= NUM_BRICK_ROW; i++) {
      for (int j = 1; j <= NUM_BRICK_COL; j++) {
        y_start = 50 + (j - 1) * (DIST_BRICK_BRICK + brickPerHeight);
        GRect tmpbrick = new GRect(brickPerWidth, brickPerHeight);
        tmpbrick.setFilled(true);
        add(tmpbrick, x_start, y_start);
      }
      x_start += DIST_BRICK_BRICK + brickPerWidth;
    }

    // Make up the board
    board = new GRect(BOARD_WIDTH, BOARD_HEIGHT);
    board.setFilled(true);
    add(board, (getWidth() - BOARD_WIDTH) / 2, (getHeight() - 30));

    // Place the ball
    ball = new GOval(BALL_RADIUS * 2, BALL_RADIUS * 2);
    ball.setFilled(true);
    ball.setColor(Color.RED);
    add(ball, (getWidth() - BALL_RADIUS * 2) / 2, (getHeight() - 30 - BALL_RADIUS * 2));

    // Set up random generator
    rgen = RandomGenerator.getInstance();

    // Add Listeners
    addMouseListeners();
  }
Example #2
0
 public void run() {
   // divido el ancho de la pantalla en 2 para saber donde está
   // la mitad exacta de la pantalla
   distanciaX = getWidth() / 2;
   distanciaY = getHeight() / 2;
   rectangulo.setLocation(
       distanciaX - rectangulo.getWidth() / 2, distanciaY - rectangulo.getHeight() / 2);
 }
Example #3
0
 private void buildHead() {
   double startingWidth = ((getWidth() / 2) - (head_width / 2));
   double startingHeight = ((getHeight() / 2) - (head_height / 2));
   GRect face = new GRect(startingWidth, startingHeight, head_width, head_height);
   add(face);
   face.setFilled(true);
   face.setFillColor(Color.GRAY);
 }
Example #4
0
 private void buildMouth() {
   double startingWidth = getWidth() / 2 - mouth_width / 2;
   double startingHeight = getHeight() / 2 + head_height / 4 - mouth_height / 2;
   GRect mouth = new GRect(startingWidth, startingHeight, mouth_width, mouth_height);
   add(mouth);
   mouth.setColor(Color.WHITE);
   mouth.setFilled(true);
   mouth.setFillColor(Color.WHITE);
 }
Example #5
0
 private void drawOneBrick(int row, int col) {
   double x0 =
       (WIDTH - NBRICKS_PER_ROW * BRICK_WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP)
           / 2; // the distance of leftmost brick to left border of the world
   double x = x0 + (BRICK_WIDTH + BRICK_SEP) * col;
   double y = BRICK_Y_OFFSET + (BRICK_SEP + BRICK_HEIGHT) * row;
   GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
   brick.setFilled(true);
   colorBrick(row, brick);
   add(brick);
 }
Example #6
0
 private void colorBrick(int row, GRect brick) {
   if (row < 2) {
     brick.setColor(Color.RED);
   } else if (row < 4) {
     brick.setColor(Color.ORANGE);
   } else if (row < 6) {
     brick.setColor(Color.YELLOW);
   } else if (row < 8) {
     brick.setColor(Color.GREEN);
   } else if (row < 10) {
     brick.setColor(Color.CYAN);
   }
 }
Example #7
0
 public void mouseMoved(MouseEvent e) {
   if ((e.getX() > PADDLE_WIDTH / 2) && (e.getX() < WIDTH - PADDLE_WIDTH / 2)) {
     double x = e.getX() - PADDLE_WIDTH / 2;
     double y = HEIGHT - BRICK_Y_OFFSET - PADDLE_HEIGHT;
     paddle.setLocation(x, y);
   }
 }
Example #8
0
 private void drawPaddle() {
   double x = (WIDTH - PADDLE_WIDTH) / 2;
   double y = HEIGHT - BRICK_Y_OFFSET - PADDLE_HEIGHT;
   paddle = new GRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
   paddle.setFilled(true);
   add(paddle);
   addMouseListeners();
 }
  public void run() {
    int x_center = getWidth() / 2;
    int y_center = getHeight() / 2;

    GRect top_rect =
        new GRect(
            x_center - (RECT_WIDTH / 2), y_center - (2 * RECT_HEIGHT), RECT_WIDTH, RECT_HEIGHT);
    add(top_rect);

    GRect bottom_left_rect =
        new GRect(
            (x_center - (RECT_WIDTH / 2)) - (RECT_WIDTH + 15), y_center, RECT_WIDTH, RECT_HEIGHT);
    add(bottom_left_rect);

    GRect bottom_middle_rect =
        new GRect(x_center - (RECT_WIDTH / 2), y_center, RECT_WIDTH, RECT_HEIGHT);
    add(bottom_middle_rect);

    GRect bottom_right_rect =
        new GRect(
            (x_center - (RECT_WIDTH / 2)) + (RECT_WIDTH + 15), y_center, RECT_WIDTH, RECT_HEIGHT);
    add(bottom_right_rect);

    GLine left_line =
        new GLine(x_center, y_center - RECT_HEIGHT, x_center - (RECT_WIDTH + 15), y_center);
    add(left_line);

    GLine right_line =
        new GLine(x_center, y_center - RECT_HEIGHT, x_center + (RECT_WIDTH + 15), y_center);
    add(right_line);

    GLine middle_line = new GLine(x_center, y_center - RECT_HEIGHT, x_center, y_center);
    add(middle_line);

    GLabel top_label =
        new GLabel(
            "Program", top_rect.getX() + (RECT_WIDTH / 2), top_rect.getY() + (RECT_HEIGHT / 2));
    top_label.move(-top_label.getWidth() / 2, top_label.getAscent() / 2);
    add(top_label);

    GLabel bottom_left_label =
        new GLabel(
            "GraphicsProgram",
            bottom_left_rect.getX() + (RECT_WIDTH / 2),
            bottom_left_rect.getY() + (RECT_HEIGHT / 2));
    bottom_left_label.move(-bottom_left_label.getWidth() / 2, bottom_left_label.getAscent() / 2);
    add(bottom_left_label);

    GLabel bottom_middle_label =
        new GLabel(
            "ConsoleProgram",
            bottom_middle_rect.getX() + (RECT_WIDTH / 2),
            bottom_middle_rect.getY() + (RECT_HEIGHT / 2));
    bottom_middle_label.move(
        -bottom_middle_label.getWidth() / 2, bottom_middle_label.getAscent() / 2);
    add(bottom_middle_label);

    GLabel bottom_right_label =
        new GLabel(
            "DialogProgram",
            bottom_right_rect.getX() + (RECT_WIDTH / 2),
            bottom_right_rect.getY() + (RECT_HEIGHT / 2));
    bottom_right_label.move(-bottom_right_label.getWidth() / 2, bottom_right_label.getAscent() / 2);
    add(bottom_right_label);
  }
Example #10
0
 /** When mouse moved: move the board */
 public void mouseMoved(MouseEvent e) {
   board.move(e.getX() - board.getX() - BOARD_WIDTH / 2, 0);
 }
Example #11
0
 /** Places a rectangle at x,y with width, height and specified color */
 public void place(int x, int y, int width, int height, Color shapeColor) {
   GRect shape = new GRect(x, y, width, height);
   shape.setFilled(true);
   shape.setColor(shapeColor);
   add(shape);
 }