Real-time metro system simulator built in Java Swing using advanced OOP, generics, and clean separation between logic and rendering.
MetroSim simulates a metro network with 5 animated train lines, 7 trains, 6 traffic lights, and 17 named stations — all running on a 500ms Swing timer. Trains follow circular predefined paths, stop automatically when a collision is detected, and traffic lights cycle through green/red states at configurable intervals.
- 🚆 5 metro lines — Red, Blue, Green, Yellow, Purple
- 💥 Stop-and-wait collision detection — stopped trains flash yellow
- 🚦 Traffic lights with configurable cycle periods
- 🗺️ Named stations rendered as circle markers on the map
- 🎨 Layered anti-aliased rendering — grid → tracks → stations → trains → lights → HUD
- 📊 Live HUD with tick counter and line legend
- 🔁 Circular paths — trains loop indefinitely
MetroSimulator/dev/znxki/src/metrosim/
├── core/
│ ├── MapObject<T> Abstract generic base (update / draw contract)
│ └── CollisionEvent<T> Immutable value object for collision reports
├── model/
│ ├── Train<T> Path-follower with stop-and-wait collision logic
│ ├── TrafficLight<T> Timer-driven GREEN / RED state toggle
│ └── Station Static visual marker
├── ui/
│ └── SimulationPanel JPanel — holds List<MapObject<?>>, tick(), paintComponent
└── util/
├── PathBuilder Factory methods for List<Point> metro paths
└── ColorPalette Centralised colour constants
| Concept | Implementation |
|---|---|
abstract class MapObject<T> |
Generic base with update() and draw(Graphics) |
Train<T> extends MapObject<T> |
Follows List<T> path; collision via isNear() |
TrafficLight<T> extends MapObject<T> |
Tick-based state toggle with isGreen() API |
CollisionEvent<T> |
Immutable record carrying both participants and collision point |
List<MapObject<?>> |
Covariant wildcard collection in SimulationPanel |
PathBuilder |
Static factory returning List<Point> paths per line |
- JDK 17+ (needs
javac— a plain JRE is not enough)
mkdir bin
javac -d bin src/metrosim/core/*.java src/metrosim/util/*.java ^
src/metrosim/model/*.java src/metrosim/ui/*.java ^
src/metrosim/MetroSimulation.java
jar cfe MetroSim.jar metrosim.MetroSimulation -C bin .
java -jar MetroSim.jar- Open the
MetroSimulatorfolder as a project - Mark
src/as the Sources Root - Run
dev.znxki.metrosim.MetroSimulation
Each Train holds a List<Train<T>> peers (the other trains on the same line).
Before advancing to the next waypoint, it checks whether any peer is within 20 px:
// Inside Train.update()
for (Train<T> peer : peers) {
if (isNear(next, peer.getPosition())) {
stopped = true;
waitTicks = 2; // hold for 2 ticks
new CollisionEvent<>(this, peer, next); // logged to console
return;
}
}Stopped trains render with a yellow border and a darker fill so collisions are immediately visible.
| Line | Route style | Trains |
|---|---|---|
| 🔴 Red | Horizontal loop with terminus turns | 2 (offset by ½ path) |
| 🔵 Blue | Vertical north–south with curved south terminus | 2 (offset by ½ path) |
| 🟢 Green | Diagonal SW → NE | 1 |
| 🟡 Yellow | Rectangular loop around the city centre | 1 |
| 🟣 Purple | Irregular pentagon | 1 |
// Add a new metro line
List<Point> orangePath = new ArrayList<>();
PathBuilder.interpolate(orangePath, 100, 100, 400, 400);
panel.addTrackLine(orangePath, Color.ORANGE, "Orange Line");
Train<Point> trainO = new Train<>("O1", "Orange", orangePath, Color.ORANGE);
panel.addObject(trainO);
// Enforce traffic-light stops inside Train.update()
if (!nearestLight.isGreen()) { waitTicks = 1; return; }
// Use a custom position type instead of Point
class GridCell { int row, col; }
Train<GridCell> gridTrain = new Train<>("G1", "Grid", gridPath, Color.CYAN);MIT — free to use, modify, and distribute.
