/** * 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; }
// Añadimos el método que escucha el evento de click de ratón public void mouseClicked(MouseEvent evento) { if (getElementAt(evento.getX(), evento.getY()) == rectangulo) { double distancia = evento.getY() - rectangulo.getY(); if (distancia > rectangulo.getHeight() / 2) { rectangulo.move(0, 5); } else { rectangulo.move(0, -5); } } }
public void mouseClicked(MouseEvent evento) { if (getElementAt(evento.getX(), evento.getY()) == rectangulo) { double altoRectangulo = rectangulo.getHeight(); double distanciaAlPrincipio = (evento.getY() - rectangulo.getY()); if (distanciaAlPrincipio > altoRectangulo / 2) { rectangulo.move(0, 20); } else { rectangulo.move(0, -20); } } if (getElementAt(evento.getX(), evento.getY()) == rectangulo) { double anchoRectangulo = rectangulo.getWidth(); double distanciaAlPrincipio = (evento.getX() - rectangulo.getX()); if (distanciaAlPrincipio > anchoRectangulo / 2) { rectangulo.move(20, 0); } else { rectangulo.move(-20, 0); } } }
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); }