/** 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(); }
/** The method takes parameters and returns, square shape figure */ private GRect rectangle(int x, int y, int width, int height, Color colorSetFill, Color colorSet) { GRect rectangle = new GRect(x, y, width, height); rectangle.setFilled(true); rectangle.setFillColor(colorSetFill); rectangle.setColor(colorSet); add(rectangle); return rectangle; }
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); }
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); }
public void run() { getWidth(); getHeight(); double x = getWidth() / 2; double y = getHeight() / 2; GRect a = new GRect(x - 1.5 * SIZE, y - 1.5 * SIZE, SIZE, 3 * SIZE); a.setColor(Color.RED); a.setFilled(true); add(a); GRect b = new GRect(x - SIZE / 2, y - 1.5 * SIZE, SIZE, 3 * SIZE); b.setColor(Color.YELLOW); b.setFilled(true); add(b); GRect c = new GRect(x + SIZE / 2, y - 1.5 * SIZE, SIZE, 3 * SIZE); c.setColor(TURQUOISE); c.setFilled(true); add(c); GLabel d = new GLabel("Flag of Peru", x + SIZE, y + 1.9 * SIZE); d.setColor(Color.BLACK); d.setFont("Broadway-12"); add(d); }
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(); }
/** * If the ball is hit in the fall on the side of the paddle, it will change the direction of the * x-coordinate */ private double checkLeftAndRightWallOfPaddle(double vx) { if (getElementAt(paddle.getX() - 1, paddle.getY()) == ball || getElementAt(paddle.getX() - 1, paddle.getY() + PADDLE_HEIGHT) == ball || getElementAt(paddle.getX() + PADDLE_WIDTH + 1, paddle.getY()) == ball || getElementAt(paddle.getX() + PADDLE_WIDTH + 1, paddle.getY() + PADDLE_HEIGHT) == ball) { vx = -vx; } return vx; }
/** Bind the paddle to the mouse cursor */ private void muvedPaddle(MouseEvent e) { int y = HEIGHT - PADDLE_HEIGHT - PADDLE_Y_OFFSET; int x; if (e.getX() > WIDTH - PADDLE_WIDTH / 2) { // Makes it impossible to exit the paddle over the right part of the window x = WIDTH - PADDLE_WIDTH; } else if (e.getX() < PADDLE_WIDTH / 2) { // Makes it impossible to exit the paddle over the left part of the window x = 0; } else { x = e.getX() - PADDLE_WIDTH / 2; // Bind the paddle to the center of the cursor } paddle.setLocation(x, y); }
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); }
private void PaintRect(int xPosition, int yPosition) { // Paint square, white color GRect rect = new GRect((xPosition / 2), (yPosition / 2), 2 * xPosition, 2 * yPosition); rect.setFilled(true); rect.setColor(Color.WHITE); add(rect); }
/** 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); }