Crossing Road Simulation

Crossing Road Simulation

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;
				}
			}
			
		}
}