Java Swing application that simulates car traffic on a highway.
This is a simple GUI application (in French) that simulates cars driving on a highway, one of my first school project!
The purpose of the project is the simulation of the car traffic on a highway, the program simulates the evolution of speed, lanes and positions of a set of cars arriving at regular intervals, and on the other hand produces information on this evolution.
Application has been built on November 2006 using Netbeans IDE 5.0.
It uses Swing and the Graphics2D library, and was compiled with Java 1.4.
Run the provided .jar
file :
java -jar "highway-simulator.jar"
Then just use the UI :
The application allows to :
- create a new highway with the following parameters :
- number of lanes (
2
to4
) - length (meters, from
100m
to10000m
)
- number of lanes (
- add a vehicle with the following parameters :
- color (
red
,green
,blue
,yellow
,purple
) - cruising speed (max
200km/h
) - acceleration
- safety distance
- "control" car or not ("control" car is a special car that will be tracked independently for statistics, it will be
black
and identified with aT
as shown below)
- color (
- visualize some parameters like :
- number of cars currently running
- number of overtakes
- average speeds :
- faster car
- slower car
- all cars
- view statistics from generated text files :
- session statistics (
Session.txt
) - control car statistics (
VehiculeTemoin.txt
)
- session statistics (
A "control" car identified with a T
:
Cars drive by default on a 2-lane highway. They arrive at the highway in an initial position at different intervals,
and with an initial speed of 0
. The behavior of a car is simulated until it reaches an exit position.
There are several kinds of parameters that affect the behavior of a car :
- specific parameters to each car which do not vary during execution time (safety distance, cruising speed and acceleration)
- specific values to each car which evolve during execution time (current speed, track, position)
So the input data of the simulation program will therefore consist of :
- the safety distance
- the highway length
- the interval between each arrival of a car at the entrance
- a list of cars specifying, for each of them, its acceleration, cruising speed, and an identification color
We consider that a lane is free for a car if there is no other vehicle on the same lane between the
positions P(c)
and P(c) + D(c)
. A lane is said to be free for a car if there is no other vehicle
between the positions P(c)-200m
and P(c) + D(c)
.
There are two modes of traffic: overtake mode and standard mode.
- In standard mode, a car drives on the right lane. If this lane is free, the car runs at its cruising speed or accelerates until it reaches its cruising speed. If the lane is not free, then it takes care of the state of the left lane :
- if the left lane is free, the car begin overtaking
- otherwise the car slows down
- In overtaking mode, a car changes lane to go on the left lane (it is assumed that this change of lane is instantaneous). It stays on this lane as long as it is in overtaking mode. It leaves the overtaking mode when the following conditions are met:
- its position is located after the one of the vehicle which was right in front of it at the beginning of the overtaking
- the right lane is free
During the entire duration of the overtaking, if the left lane is free, the car drives at its cruising speed or accelerates until it reaches cruising speed. If this lane is not free, it slows down.
Finally, the program allows to identify a "control" car, and will record in a text file the positions and other parameters of this car.
The application consists of several classes :
Base classes :
Vehicule
: represent a car and offers all functions related to itAutoroute
: represent the highway and offers all functions related to itEcritDansFichier
: Take care of writing into theSession.txt
statistics fileEcritVehiculeTemoin
: Take care of writing theVehiculeTemoin.txt
statistics file
Frames :
Fenetre
: Main frame of the application, it contains thePanelRoute
andPanelCommandes
panels (see below) to display the whole GUIFenetreFichier
: Frame representing the window displaying the "control" car statisticsFenetreFichierSession
: Frame representing the window displaying the whole session statistics
Panels :
PanelCommandes
: This panel contains the different components allowing to change the settings that will affect the program. This class implements theInterfaceCommande
interface which allows to communicate with thePanelRoute
class using listenersPanelRoute
: This Panel contains the drawing that displays the movement of vehicles on the highway. It is intended to be added in theFenetre
frame to be displayed on the screen next to the Panel containing the commands
The Main
class is the entry point of the application.
The InterfaceCommande
interface describes the actions that will need to be implemented by classes that need to listen to changes like car speed, position, etc. (
extends EventListener
).
The default display of AWT or Swing components uses a simple buffer (we can see the image being drawn). When building complex animations, it may cause flickering of the image (clipping). To solve this issue, we can use the double-buffering technique. Double buffering is having 2 buffers, a "back" buffer that processes the drawing stuff in memory, and a "front" buffer that takes care of the display. Problems of refresh, image jump and other disruptive visuals effects are thus eliminated.
Using a double buffer is particularly recommended for displaying complex animations or large components.
General Public License (GPL) v3
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.