/** 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(); }
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); }
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); } }
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); } }
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(); }
/** When mouse moved: move the board */ public void mouseMoved(MouseEvent e) { board.move(e.getX() - board.getX() - BOARD_WIDTH / 2, 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); }