Crossing Road Simulation

本博客介绍了一个使用Java Swing实现的道路交叉口模拟系统。该系统通过线程更新模拟状态,并使用自定义面板绘制交通信号灯及行人过街情况。模拟中包括了交通灯的定时切换以及行人、车辆的移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

package com.view.test;
import javax.swing.*;

import java.awt.*;
public class Crossing extends JFrame{

	DrawPanel dp=null;
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Crossing crossing=new Crossing();
	}
	
	public Crossing()
	{
		dp=new DrawPanel();
		Thread t=new Thread(dp);
		t.start();
		this.add(dp);
		this.setSize(900,600);
		this.setTitle("CrossingRoad Simulation");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
}

class DrawPanel extends JPanel implements Runnable
	{
		int timeDown=60;
		int timeUp=0;
		int x1=450;
		int y1=200;
		int x2=450;
		int y2=400;
		int x3=300;
		int y3=300;
		int x4=600;
		int y4=300;
		int x5=300;
		int y5=0;
		int x6=525;
		int y6=560;
		int x7=260;
		int y7=350;
		int x8=600;
		int y8=200;
		int speed1=80;
		int speed2=80;
		int speed3=80;
		int speed4=80;
		int temp=0;
		int flag=0;	
		public void paint(Graphics g)
		{
			super.paint(g);
			g.fillRect(0, 0, 900, 600);
			g.setColor(Color.white);
			g.drawLine(300, 0, 300, 600);
			g.drawLine(375, 0, 375, 600);
			g.drawLine(450, 0, 450, 600);
			g.drawLine(525, 0, 525, 600);
			g.drawLine(600,0,600,600);
			g.drawLine(0, 200, 900, 200);
			g.drawLine(0, 250, 900, 250);
			g.drawLine(0, 300, 900, 300);
			g.drawLine(0, 350, 900, 350);
			g.drawLine(0, 400, 900, 400);
			
			g.setColor(Color.yellow);
			g.drawLine(450,200,600,200);
			g.drawLine(300,400,450,400);
			g.drawLine(300,200,300,300);
			g.drawLine(600,300,600,400);
			if(timeUp%60==0)
			{
				temp=x1;
				x1=x3;
				x3=temp;
				
				temp=y1;
				y1=y3;
				y3=temp;
				
				temp=x2;
				x2=x4;
				x4=temp;
				
				temp=y2;
				y2=y4;
				y4=temp;
				flag++;
			}
			
			if(flag%2!=0)
			{
				if(timeDown<4)
				{
					if(y5<200)this.speed1=0;
					if(y6>400)this.speed2=0;
				}else{
					this.speed1=80;
					this.speed2=80;
				}
		
				this.speed3=0;
				this.speed4=0;
			}else if(flag%2==0)
			{
				this.speed1=0;
				this.speed2=0;
				if(timeDown<4)
				{
					if(x7<300)
						this.speed3=0;
					if(x8>600)
						this.speed4=0;
				}else{
					this.speed3=80;
					this.speed4=80;
				}
			}
			
		
					
				 if(y5>600)y5=0;
				 if(y6<0)y6=560;
				 if(x7>900)x7=0;
				 if(x8<0)x8=880;
g.setColor(Color.red);
				//开关信息的字体
				String s=String.valueOf(timeDown);
				Font myFont=new Font("黑体",Font.BOLD,30);
				g.setFont(myFont);
				g.drawString(s, x1, y1);
				g.drawString(s, x2, y2);
				g.setColor(Color.green);
				g.drawString(s, x3, y3);
				g.drawString(s, x4, y4);
				g.draw3DRect(x5, y5, 30, 40, false);
				g.draw3DRect(x6, y6, 30, 40, false);
				g.draw3DRect(x7, y7, 40, 30, false);
				g.draw3DRect(x8, y8, 40, 30, false);		
		}

public void run() {
			// TODO Auto-generated method stub
			while(true)
			{
				//休眠
				try {
					
					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
					// TODO: handle exception
				}
				
				timeDown--;
				timeUp++;
				y5+=speed1;
				y6-=speed2;
				x7+=speed3;
				x8-=speed4;
				//重画
				this.repaint();
				System.out.println(timeDown);
				if(timeDown==0)
				{
					timeDown+=60;
				}
			}
			
		}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值