⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tlframe.java

📁 交通信号灯设计程序
💻 JAVA
字号:
import java.awt.event.*;
import java.awt.*;
import sun.awt.Mutex;
import java.util.Random;
public class TLFrame extends Frame 
implements ComponentListener, ActionListener,WindowListener
{
	Color color = Color.black;
	
	int i,len=600,wid=480,buw=20,bul=20;
	public String str ="";
	public String str1="";
	public String str2="";
	public String str3="";
	public Button redlight[]=new Button[12];
	public Button yellight[]=new Button[12];
	public Button grelight[]=new Button[4];
	public Button turlight[]=new Button[4];
	public Button riglight[]=new Button[4];
	public Button act=new Button();
	public Label time=new Label();	
	public Label time1=new Label();
	public Label time2=new Label();
	public Label time3=new Label();
	public Label rest[]=new Label[4];
	public TextField dis  = new TextField();
	public TextField dis1 = new TextField();
	public TextField dis2 = new TextField();
	public TextField dis3 = new TextField();
	public Panel zhou ;
	public Panel EASTD ;
	public Panel SOUTHD ;
	public Panel WESTD ;
	public Panel NORTHD ;
	public Label author=new Label("作者: 周旭峰");
	public TLFrame()
	{
		setTitle("交通信号灯");
        setSize(len,wid);
        setBackground(Color.gray);
        setLayout(null);
        addWindowListener(this);
        addComponentListener(this);
        act.addActionListener(this);
        setResizable(false);
        for(i=0;i<12;i++)
        {
        	redlight[i] = new Button("╳");
			redlight[i].setBackground(Color.red);
			yellight[i] = new Button("    ");
			yellight[i].setBackground(color);
		}	
		for(i=0;i<4;i++)
        {	
        	rest[i]= new Label("00");
        	add(rest[i]);
        	rest[i].setBackground(Color.lightGray);
		
		}	
		rest[0].setBounds(len/3*2+5,wid/3-20,20,20);
		rest[1].setBounds(len/3-20,wid/3*2+5,20,20);
		rest[2].setBounds(len/3*2+5,wid/3*2+5,20,20);
		rest[3].setBounds(len/3-20,wid/3-20,20,20);
		grelight[0] = new Button("←");
		grelight[0].setBackground(color);		
		turlight[0] = new Button("↓");
		turlight[0].setBackground(color);		
		riglight[0] = new Button("↑");
		riglight[0].setBackground(color);
		grelight[1] = new Button("↑");
		grelight[1].setBackground(color);
		turlight[1] = new Button("←");
		turlight[1].setBackground(color);
		riglight[1]= new Button("→");
		riglight[1].setBackground(color);
		grelight[2] = new Button("→");
		grelight[2].setBackground(color);
		turlight[2] = new Button("↑");
		turlight[2].setBackground(color);
		riglight[2] = new Button("↓");
		riglight[2].setBackground(color);
		grelight[3] = new Button("↓");
		grelight[3].setBackground(color);
		turlight[3] = new Button("→");
		turlight[3].setBackground(color);
		riglight[3] = new Button("←");
		riglight[3].setBackground(color);		//设计信号灯			
		EASTD = new Panel();
		EASTD.setLayout(new GridLayout(3,3,0,0));
		EASTD.add(redlight[0]);
		EASTD.add(yellight[0]);
		EASTD.add(riglight[0]);
		EASTD.add(redlight[4]);
		EASTD.add(yellight[4]);
		EASTD.add(grelight[0]);
		EASTD.add(redlight[8]);
		EASTD.add(yellight[8]);
		EASTD.add(turlight[0]);
		add(EASTD);
		EASTD.setBounds(len/3*2,wid/3+5,buw*3,bul*3);
		SOUTHD = new Panel();
		SOUTHD.setLayout(new GridLayout(3,3,0,0));
		SOUTHD.add(redlight[9]);
		SOUTHD.add(redlight[5]);
		SOUTHD.add(redlight[1]);
		SOUTHD.add(yellight[9]);
		SOUTHD.add(yellight[5]);
		SOUTHD.add(yellight[1]);
		SOUTHD.add(turlight[1]);
		SOUTHD.add(grelight[1]);
		SOUTHD.add(riglight[1]);
		add(SOUTHD);
		SOUTHD.setBounds(len/2+30,wid/3*2,bul*3,buw*3);
		NORTHD = new Panel();
		NORTHD.setLayout(new GridLayout(3,3,00,0));
		NORTHD.add(riglight[3]);
		NORTHD.add(grelight[3]);
		NORTHD.add(turlight[3]);
		NORTHD.add(yellight[3]);
		NORTHD.add(yellight[7]);
		NORTHD.add(yellight[11]);
		NORTHD.add(redlight[3]);
		NORTHD.add(redlight[7]);
		NORTHD.add(redlight[11]);
		add(NORTHD);
		NORTHD.setBounds(len/3+5,wid/9*2,bul*3,buw*3);
		WESTD = new Panel();
		WESTD.setLayout(new GridLayout(3,3,0,0));
		WESTD.add(turlight[2]);
		WESTD.add(yellight[10]);
		WESTD.add(redlight[10]);
		WESTD.add(grelight[2]);
		WESTD.add(yellight[6]);
		WESTD.add(redlight[6]);
		WESTD.add(riglight[2]);
		WESTD.add(yellight[2]);
		WESTD.add(redlight[2]);
		add(WESTD);
		WESTD.setBounds(len/3*2,wid/2-10,buw*3,bul*3);
		act  = new Button("确定");
		time = new Label("直行: 东西向");
		dis  = new TextField("10",1);
		dis1 = new TextField("10",1);
		time1= new Label("左转: 东西向");
		time2= new Label("          南北向");
		time3= new Label("          南北向");
		dis2 = new TextField("10",1);
		zhou = new Panel();
		dis3 = new TextField("10",1);
		zhou.setLayout(new FlowLayout(FlowLayout.LEFT));
		zhou.add(time);
		zhou.add(dis);
		zhou.add(time2);
		zhou.add(dis1);
		zhou.add(time1);
		zhou.add(dis2);
		zhou.add(time3);
		zhou.add(dis3);
		add(zhou);
		zhou.setBounds(len/70,wid/20,bul*8,buw*8);
		add(author);
		
	}
	public void paint(Graphics g)
	{
		g.setColor(Color.black);				//创建十字路口
		g.fillRect(len/3,0,5,wid/3+5);
		g.fillRect(0,wid/3,len/3+5,5);
		g.fillRect(len/3*2,0,5,wid/3);
		g.fillRect(len/3*2,wid/3,len/3,5);
		g.fillRect(0,wid/3*2,len/3,5);
		g.fillRect(len/3,wid/3*2,5,wid/3);
		g.fillRect(len/3*2,wid/3*2,len/3,5);
		g.fillRect(len/3*2,wid/3*2,5,wid/3);
		g.setColor(Color.yellow); 				//画黄线
		g.fillRect(0,wid/2,len/3+3,2);
		g.fillRect(len/3*2,wid/2,len/3,2);
		g.fillRect(len/2,0,2,wid/3+3);
		g.fillRect(len/2,wid/3*2,2,wid/3);
		g.setColor(Color.white);				//画线
		g.fillRect(len/3*2,wid/3+7,10,wid/7+4);
		g.fillRect(len/3-5,wid/2+4,10,wid/7+6);
		g.fillRect(len/3+7,wid/3-7,wid/5-5,10);
		g.fillRect(len/2+3,wid/3*2,wid/5,10);
		EASTD.setBounds(len/4-8,wid/3+18,buw*3,bul*3);
		WESTD.setBounds(len/3*2,wid/2+5,buw*3,bul*3);
		NORTHD.setBounds(len/3+35,wid/3*2,bul*3,buw*3);
		SOUTHD.setBounds(len/2+5,wid/5+5,bul*3,buw*3);
		zhou.setBounds(len/70,wid/20,bul*8,buw*6);
		rest[3].setBounds(len/3-20,wid/3-20,20,20);         //N
		rest[2].setBounds(len/3-20,wid/3*2+5,20,20);		//W
		rest[1].setBounds(len/3*2+5,wid/3*2+5,20,20);		//S	
		rest[0].setBounds(len/3*2+5,wid/3-20,20,20);		//E
		author.setBounds(len/5*4,wid-30,80,20);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==act)
		{
			str=dis.getText();
		}
	}
	public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
    public void componentResized(ComponentEvent e)   //改变窗口大小时
    {  
        /*len = getWidth();                         //取得窗口的大小
        wid = getHeight(); 
        buw=wid/20;
        bul=len/20;
        if(bul>buw)bul=buw;
        else buw=bul;
        repaint();*/
    }
	public static void main(String arg[])
	{
		TLFrame TTT= new  TLFrame(); 
		TTT.setVisible(true);
		Mutex mutex = new Mutex();
		
		change s1= new change(TTT,0,mutex);
		change s2= new change(TTT,1,mutex);
		s1.start();
		s2.start();
	} 
	public void componentMoved(ComponentEvent e)    {  }
    public void componentHidden(ComponentEvent e)   {  }
    public void componentShown(ComponentEvent e)    {  }
    public void windowOpened(WindowEvent e)         {  }
    public void windowActivated(WindowEvent e)      {  }
    public void windowDeactivated(WindowEvent e)    {  }
    public void windowClosed(WindowEvent e)         {  }
    public void windowIconified(WindowEvent e)      {  }
    public void windowDeiconified(WindowEvent e)    {  }
}

class change extends Thread            
{  
 	
 	String str="";
 	String str1="",str2="",str3="";
 	String rest0;
 	Color color = Color.black; private int a=0;
    private Random random = null;
    private TLFrame myFrame = null;
   	private Mutex mutex=null;
    public change(Frame myFrame,int i, Mutex mutex)
    {
    	this.myFrame = (TLFrame)myFrame;
       	this.a=i;
      	this.mutex   = mutex;
      	this.str="";
    }
      public void run()
    {
       	while(true)
        {
        	mutex.lock();
	       	this.str=myFrame.dis.getText();
	       	this.str1=myFrame.dis1.getText();
	       	this.str2=myFrame.dis2.getText();
	       	this.str3=myFrame.dis3.getText();
	       	float b0= Float.parseFloat(str);
	       	float b1= Float.parseFloat(str1);
	       	float b2= Float.parseFloat(str2);
	       	float b3= Float.parseFloat(str3);
	       	
	       	float m=b0+b1+b2+b3;
	       	b0=b0/m*40;b1=b1/m*40;
	       	b2=b2/m*40;b3=b3/m*40;
	       	int wait1,wait2;
	       	if(a==0){wait1=(int)b0;wait2=(int)b1;}
	       	else{wait1=(int)b2;wait2=(int)b3;}
	       	System.out.println(a+" "+wait1+"   "+wait2);
      	
        	myFrame.yellight[a+8].setBackground(Color.yellow);
           	myFrame.yellight[a+10].setBackground(Color.yellow);
           	myFrame.yellight[a].setBackground(Color.yellow);
           	myFrame.yellight[a+2].setBackground(Color.yellow);
           	try
            {
             	Thread.sleep(1000);
           	}
       		catch(InterruptedException e)
           	{
            	System.out.println(e.getMessage());
            }
      	   	myFrame.redlight[a].setBackground(color);
        	myFrame.redlight[a+4].setBackground(Color.red);
       		myFrame.redlight[a+8].setBackground(color);
        	myFrame.yellight[a].setBackground(color);
       		myFrame.yellight[a+4].setBackground(color);
       		myFrame.yellight[a+8].setBackground(color);
        	myFrame.turlight[a].setBackground(Color.green);
        	myFrame.grelight[a].setBackground(color);
        	myFrame.riglight[a].setBackground(Color.green);
        	myFrame.redlight[a+2].setBackground(color);
        	myFrame.redlight[a+6].setBackground(Color.red);
        	myFrame.redlight[a+10].setBackground(color);
        	myFrame.yellight[a+2].setBackground(color);
        	myFrame.yellight[a+6].setBackground(color);
        	myFrame.yellight[a+10].setBackground(color);
        	myFrame.turlight[a+2].setBackground(Color.green);
        	myFrame.grelight[a+2].setBackground(color);
        	myFrame.riglight[a+2].setBackground(Color.green);
        	for(int i=wait1;i>=0;i--)
           	{
           		rest0=Integer.toString(i);
           		myFrame.rest[0].setText(rest0);	
           	//	myFrame.rest[0].setBackground(Color.white);
           		myFrame.rest[1].setText(rest0);
           	//	myFrame.rest[1].setBackground(Color.blue);
           		myFrame.rest[2].setText(rest0);
           	//	myFrame.rest[2].setBackground(Color.red);
          		myFrame.rest[3].setText(rest0); 		
          	//	myFrame.rest[3].setBackground(Color.green);
           		System.out.println("here is "+rest0);
           		try
           		{
           	   		Thread.sleep(1000);
           		}
       			catch(InterruptedException e)
           		{
            		System.out.println(e.getMessage());
           		}
           	}
           
           	myFrame.yellight[a+8].setBackground(Color.yellow);
           	myFrame.yellight[a+4].setBackground(Color.yellow);
           	myFrame.yellight[a+6].setBackground(Color.yellow);
           	myFrame.yellight[a+10].setBackground(Color.yellow);
           
           	try
            {
           		Thread.sleep(1000);
           	}
       		catch(InterruptedException e)
           	{
             	System.out.println(e.getMessage());
           	}
           	myFrame.redlight[a].setBackground(color);
           	myFrame.redlight[a+4].setBackground(color);
           	myFrame.redlight[a+8].setBackground(Color.red);
           	myFrame.yellight[a].setBackground(color);
        	myFrame.yellight[a+4].setBackground(color);
        	myFrame.yellight[a+8].setBackground(color);
        	myFrame.turlight[a].setBackground(color);
        	myFrame.grelight[a].setBackground(Color.green);
	       	myFrame.redlight[a+2].setBackground(color);
           	myFrame.redlight[a+6].setBackground(color);
           	myFrame.redlight[a+10].setBackground(Color.red);
        	myFrame.yellight[a+2].setBackground(color);
        	myFrame.yellight[a+6].setBackground(color);
        	myFrame.yellight[a+10].setBackground(color);
        	myFrame.turlight[a+2].setBackground(color);
        	myFrame.grelight[a+2].setBackground(Color.green);
           	
           	for(int i=wait2;i>=0;i--)
           	{
           		rest0=Integer.toString(i);
           		myFrame.rest[0].setText(rest0);
           		//myFrame.rest[0].setBackground(Color.white);
           		myFrame.rest[1].setText(rest0);
           		//myFrame.rest[1].setBackground(Color.blue);
           		myFrame.rest[2].setText(rest0);
           		//myFrame.rest[2].setBackground(Color.green);
       	    	myFrame.rest[3].setText(rest0);
           		//myFrame.rest[3].setBackground(Color.red);
           	//	System.out.println("here is "+rest0);
           		try
           		{
           	   		Thread.sleep(1000);
           		}
       			catch(InterruptedException e)
           		{
            		System.out.println(e.getMessage());
           		}
           	}
                      	
           		myFrame.yellight[a+4].setBackground(Color.yellow);
           		myFrame.yellight[a+6].setBackground(Color.yellow);
           		myFrame.yellight[a].setBackground(Color.yellow);
           		myFrame.yellight[a+2].setBackground(Color.yellow);
           	
           	try
            {
            	Thread.sleep(1000);
           	}
       		catch(InterruptedException e)
           	{
            	System.out.println(e.getMessage());
           	}
           	myFrame.redlight[a].setBackground(Color.red);
           	myFrame.redlight[a+4].setBackground(Color.red);
           	myFrame.redlight[a+8].setBackground(Color.red);
        	myFrame.yellight[a].setBackground(color);
        	myFrame.yellight[a+4].setBackground(color);
        	myFrame.yellight[a+8].setBackground(color);
        	myFrame.turlight[a].setBackground(color);
        	myFrame.grelight[a].setBackground(color);
        	myFrame.riglight[a].setBackground(color);
        	myFrame.redlight[a+2].setBackground(Color.red);
           	myFrame.redlight[a+6].setBackground(Color.red);
           	myFrame.redlight[a+10].setBackground(Color.red);
        	myFrame.yellight[a+2].setBackground(color);
        	myFrame.yellight[a+6].setBackground(color);
        	myFrame.yellight[a+10].setBackground(color);
        	myFrame.turlight[a+2].setBackground(color);
        	myFrame.grelight[a+2].setBackground(color);
        	myFrame.riglight[a+2].setBackground(color);
        	try
            {
             	Thread.sleep(1000);
           	}
       		catch(InterruptedException e)
           	{
            	System.out.println(e.getMessage());
            }
            mutex.unlock();
            try
            {
             	Thread.sleep(100);
           	}
       		catch(InterruptedException e)
           	{
            	System.out.println(e.getMessage());
            }
        }   
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -