JAVA Traffic Signal Light Course Design (Source Code + Report + Video Demonstration)
topic details
Simulate the logic of the traffic light management system at the intersection.
1. Randomly generate vehicles traveling along each route
2. Signal lights ignore yellow lights and only consider red and green lights.
3. It should be considered that vehicles turning left are controlled by signal lights, and vehicles turning right are not controlled by signal lights.
4. The specific signal light control logic is the same as that of ordinary traffic light control logic in real life, regardless of the control logic in special cases:
North-south vehicles and east-west vehicles are released alternately. Vehicles waiting in the same direction should pass through vehicles first, and then vehicles turning left.
6. The time interval for randomly generating vehicles and the time interval for exchanging traffic lights is self-determined and can be set.
demand analysis
2.1 Demand Analysis of Traffic Signal Light Simulation
From the analysis of the above topic, the key to solving the problem is to figure out the logic of the traffic lights at the intersection:
(1) Randomly generate vehicles traveling along each route
(2) Signal lights ignore yellow lights and only consider red and green lights.
(3) It should be considered that vehicles turning left are controlled by signal lights, and vehicles turning right are not controlled by signal lights.
(4) The specific signal light control logic is the same as that of ordinary traffic light control logic in real life, regardless of the control logic in special cases:
North-south vehicles and east-west vehicles are released alternately. Vehicles waiting in the same direction should pass through vehicles first, and then vehicles turning left.
(5) The time for each vehicle to pass through the intersection is 1 second (hint: it can be simulated by thread sleep).
(6) The time interval of randomly generated vehicles and the time interval of traffic light exchange are self-determined and can be set.
c
Design of Traffic Signal Simulation
3.1 Overall design of the system
3.2 Interface design
After running the program, the following interface will appear. Since we are simulating the logic of the traffic lights at the intersection, the interface is designed as an intersection. For the sake of the interface's beauty, there are green areas on the top, bottom, left, and right to represent "lawn", highway The gray color of the normal road is used, and in order to better fit the actual road conditions, the road boundary adopts yellow solid lines and dotted lines, while white solid lines are used between the lanes, and there are white turn marks corresponding to the lanes in the lanes. On each side of the middle area, the upper left "lawn" has "start" and "stop" buttons, which are used to start and end the simulation of the system, and the upper right "lawn" introduces some simple driving rules.
4 Realization of traffic light simulation
4.1 Project structure and configuration
Project Structure
The file name is TrafficLight, the package name is java traffic light, and the package contains pictures and 13 classes, among which Main represents the main class, which is the beginning of the whole process.Project configuration
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
4.2 Key code analysis
4.2.1 Entry - "Main" class
This class is the beginning of the whole process. The variable naming and project ideas in it are influenced by the idea of a web article. The 12 roads are regarded as 12 sets, and the cars are added to these sets. A name like static List
For the specific code analysis and result operation and testing later, please follow the Baidu cloud link to obtain and view
Report Fetch
Link:https://pan.baidu.com/s/16HZz7aIx9BkRZnoR9petyA
Extraction code:xi7u