HIDE NAV

Java FX Breakout 2026

Part I - Code Structure

This project is a demostration of a basic game engine implemented in Java FX based on the classic game Breakout.
In Part 1 the project structure is established along with layout, time control and rendering of the first game object.


JfxBreakoutApp.java


package ceccs;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.control.Label;

public class JfxBreakoutApp extends Application {

    Ball ball;
    Pane motionPane;
    GameTimer gameTimer;

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene gameScene = setupLayoutScene();
        //setup game
        ball = new Ball(30, this);
        gameTimer = new GameTimer(this);
        gameTimer.start();

        primaryStage.setScene(gameScene);
        primaryStage.setTitle("BREAKOUT");
        primaryStage.show();
        primaryStage.setResizable(false);
    }

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

    Scene setupLayoutScene(){
        StackPane root = new StackPane();
        motionPane = new Pane();
        Scene scn = new Scene(root, 600,300);
        Rectangle backgroundRect = new Rectangle(scn.getWidth(),scn.getHeight(), Color.BLACK);
        root.getChildren().add(backgroundRect);
        root.getChildren().add(motionPane);
        return scn;
    }
}

Ball.java


package ceccs;

import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class Ball extends Rectangle {

    JfxBreakoutApp app;

    double x,y;
    double vx,vy;
    double size;

    public Ball(double size, JfxBreakoutApp app){
        this.app = app;
        this.size = size;
	//TODO: these are test values, change ball spawn behavior
        x = 100;
        y = 75;
        vx = .5;
        vy = -.02;
        setFill(Color.LIME);
        updateGraphics();
        app.motionPane.getChildren().add(this);
    }

    void updateMotion(){
        x += vx;
        y += vy;
    }

    void updateCollision(){
        //left screen boundary
        if(x < 0) {
            x = 0;
            vx = -vx;
        }
        //right screen boundary
        if(x > (app.motionPane.getWidth() - size)) {
            x = (app.motionPane.getWidth() - size);
            vx = -vx;
        }

        if(y < 0) {
            y = 0;
            vy = -vy;
        }
    }

    void updateGraphics(){
        setLayoutX(x);
        setLayoutY(y);
        setWidth(size);
        setHeight(size);
    }

}

GameTimer.java


package ceccs;

import javafx.animation.AnimationTimer;

public class GameTimer extends AnimationTimer {
    JfxBreakoutApp app;

    public GameTimer(JfxBreakoutApp app){
        this.app = app;
    }

    @Override
    public void handle(long now) {
        updateMotion();
        updateCollision();
        updateGraphics();
    }

    private void updateMotion(){
        app.ball.updateMotion();
    }

    private void updateCollision(){
        app.ball.updateCollision();
    }

    private void updateGraphics(){
        app.ball.updateGraphics();
    }
}