
/*
 * File: Breakout.java
 * -------------------
 * Name:
 * Section Leader:
 *
 * This file will eventually implement the game of Breakout.
 */

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Breakout extends Application {
    /** Width and height of application window in pixels */
    public static final int APPLICATION_WIDTH = 400;
    public static final int APPLICATION_HEIGHT = 600;

    /** Dimensions of game board (usually the same) */
    private static final int WIDTH = APPLICATION_WIDTH;
    private static final int HEIGHT = APPLICATION_HEIGHT;

    /** Dimensions of the paddle */
    private static final int PADDLE_WIDTH = 60;
    private static final int PADDLE_HEIGHT = 10;

    /** Offset of the paddle up from the bottom */
    private static final int PADDLE_Y_OFFSET = 30;

    // Maximum value of x the paddle should have so it doesn't go off the screen
    private static final int PADDLE_MAX_X = APPLICATION_WIDTH - PADDLE_WIDTH;
    
    /** Number of bricks per row */
    private static final int NBRICKS_PER_ROW = 10;

    /** Number of rows of bricks */
    private static final int NBRICK_ROWS = 10;

    /** Separation between bricks */
    private static final int BRICK_SEP = 4;

    /** Width of a brick */
    private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

    /** Height of a brick */
    private static final int BRICK_HEIGHT = 8;

    /** Radius of the ball in pixels */
    private static final int BALL_RADIUS = 10;

    /** Offset of the top brick row from the top */
    private static final int BRICK_Y_OFFSET = 70;

    /** Number of turns */
    private static final int NTURNS = 3;

    private Color[] BrickColors = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN };

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Canvas canvas = new Canvas(APPLICATION_WIDTH, APPLICATION_HEIGHT);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        setUpGame(gc);
        
        // TODO: move this into setUpGame
        final Rectangle paddle = createPaddle();
        root.getChildren().add(paddle);
        
        root.setOnMouseMoved(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
            	double paddleX = event.getSceneX();
            	
            	// Don't let the paddle go off the right edge of the screen
            	if (paddleX > PADDLE_MAX_X) {
            		paddleX = PADDLE_MAX_X;
            	}
            	
                paddle.setX(paddleX);
            }
        });
        
        root.getChildren().add(canvas);
        Scene scene = new Scene(root);
        // Hide the mouse cursor
        scene.setCursor(Cursor.NONE);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private void setUpGame(GraphicsContext gc) {
        drawWall(gc);
        drawBricks(gc);
    }

    private void drawWall(GraphicsContext gc) {
        gc.setStroke(Color.BLACK);
        gc.strokeRect(0, 0, WIDTH, HEIGHT);
    }

    private void drawBricks(GraphicsContext gc) {
        for (int row = 0; row < NBRICKS_PER_ROW; row++) {
            gc.setFill(BrickColors[row / 2]);

            for (int col = 0; col < NBRICK_ROWS; col++) {
                int x = (APPLICATION_WIDTH - NBRICKS_PER_ROW * BRICK_WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / 2
                        + (BRICK_WIDTH + BRICK_SEP) * col;
                int y = BRICK_Y_OFFSET + (BRICK_HEIGHT + BRICK_SEP) * row;
                drawBrick(gc, x, y);
            }
        }
    }

    private void drawBrick(GraphicsContext gc, int x, int y) {
        gc.fillRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    }

    private Rectangle createPaddle() {
    	int x = (APPLICATION_WIDTH - PADDLE_WIDTH) / 2;
    	int y = APPLICATION_HEIGHT - PADDLE_Y_OFFSET;
    	
    	final Rectangle rect = new Rectangle(x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
    	rect.setFill(Color.BLACK);
    	return rect;
    }
}

@@ -441,24 +441,58 @@
 aint.Color;%0A
+import javafx.scene.shape.Circle;%0A
 import javaf
@@ -512,24 +512,24 @@
 .Rectangle;%0A
-
 import javaf
@@ -2156,16 +2156,94 @@
 YAN %7D;%0A%0A
+    public static void main(String%5B%5D args) %7B%0A        launch(args);%0A    %7D%0A    %0A
     @Ove
@@ -2626,32 +2626,121 @@
 ddle);%0A        %0A
+        final Circle ball = createBall();%0A        root.getChildren().add(ball);%0A        %0A
         root.set
@@ -3414,82 +3414,8 @@
  %7D%0A%0A
-    public static void main(String%5B%5D args) %7B%0A        launch(args);%0A    %7D%0A%0A
     
@@ -4471,20 +4471,22 @@
 ctangle 
-rect
+paddle
  = new R
@@ -4535,20 +4535,22 @@
 );%0A    %09
-rect
+paddle
 .setFill
@@ -4580,12 +4580,226 @@
 urn 
-rect
+paddle;%0A    %7D%0A    %0A    private Circle createBall() %7B%0A    %09int x = APPLICATION_WIDTH / 2;%0A    %09int y = APPLICATION_HEIGHT / 2;%0A    %09%0A    %09final Circle ball = new Circle(x, y, BALL_RADIUS, Color.BLACK); %0A    %09return ball
 ;%0A  

Add ball