Pong Game Java
Project Overview
PongGameFR is a classic Pong implementation written entirely in Java using Swing and AWT for graphics rendering. The project recreates the iconic two-player paddle-and-ball gameplay with a clean architecture separating game entities, rendering logic, and user input handling.
This was my freshman year final project, inspired by Bro Code's tutorials. It has a menu system, a controls screen, and dynamic gameplay features including increasing ball speed on paddle collisions.
Technology Stack
| Component | Technology | | ---------- | ---------- | | Language | Java | | UI | Swing + AWT |
No build tooling beyond javac — the whole game compiles and runs with the JDK.
Architecture
The codebase has four layers:
- Game state machine. Three states:
MENU,CONTROLS,PLAY. Each renders differently and listens to different inputs. - Entities.
Paddle,Ball, with position + velocity vectors and rectangular collision bounds. - Renderer. A single
GamePanel extends JPaneloverridespaintComponentto draw the current state. Double-buffered automatically by Swing. - Input.
KeyListenerattached to theJFrame; player 1 uses W/S, player 2 uses Up/Down.
Gameplay Loop
The game runs at a fixed-step ~60 FPS. Each tick:
- Read input state (held keys).
- Update paddle positions (clamped to screen bounds).
- Update ball position.
- Check collisions — wall (top/bottom), paddle (left/right), score (off either side).
- On paddle collision, the ball's velocity multiplies by ~1.05x — the rally gets faster the longer you can keep it going.
- Repaint.
What I Learned
- Java Swing is dated but it works. Double-buffered
paintComponentis enough for a 60 FPS game on commodity hardware. - A finite state machine for menus + gameplay is the right structure even at this scale — trying to do it with flags gets messy fast.
- The ball-speed-up-on-collision mechanic makes Pong genuinely tense at high rally counts. It's the only thing the game needs beyond the basic loop.
- Don't put game logic inside
paintComponent. Swing calls it on its own schedule; you'll have non-deterministic physics. Use ajavax.swing.Timer(or in modern Java, aScheduledExecutorService) for the game loop.