import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Hufan extends JFrame implements ActionListener{ static HuPanel hp; JMenuBar jmb; JMenu jm1,jm2; JMenuItem jmi1,jmi2,jmi3,jmi4; StartPanel sp; public static void main(String[] args) { new Hufan(); } public Hufan(){ jmb=new JMenuBar(); jm1=new JMenu("游戏"); jmi1=new JMenuItem("开始模拟"); jmi1.addActionListener(this); jmi2=new JMenuItem("暂停游戏"); jmi2.addActionListener(this); jmi3=new JMenuItem("退出游戏"); jmi3.addActionListener(this); jm1.add(jmi1); jm1.add(jmi2); jm1.add(jmi3); jm2=new JMenu("控制"); jmi4=new JMenuItem("设置"); jmi4.addActionListener(this); jm2.add(jmi4); jmb.add(jm1); jmb.add(jm2); this.setJMenuBar(jmb); sp=new StartPanel(); new Thread(sp).start(); this.add(sp); this.setTitle("模拟交通灯"); this.setSize(700, 700); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==jmi1){ sp.isLive=false; this.remove(sp); hp=new HuPanel(); this.add(hp); this.setVisible(true); }else if(e.getSource()==jmi2){ if(jmi2.getText().equals("暂停游戏")){ Car.zanTing=false; hp.zanTing=false; Light.kaiGuan=false; jmi2.setText("继续游戏"); }else if(jmi2.getText().equals("继续游戏")){ Car.zanTing=true; hp.zanTing=true; Light.kaiGuan=true; jmi2.setText("暂停游戏"); } }else if(e.getSource()==jmi3){ System.exit(0); }else if(e.getSource()==jmi4){ new Set(this,"设置",true); } } } class StartPanel extends JPanel implements Runnable{ int info=0; boolean isLive=true; public void paint(Graphics g) { super.paint(g) ; g.fillRect(0, 0, 700, 700); g.setColor(Color.red); g.setFont(new Font("宋体",Font.BOLD,30)); if(info%2==0){ g.drawString("Java模拟交通", 230,300); } } @Override public void run() { // TODO Auto-generated method stub while(true){ try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } info++; this.repaint(); if(isLive==false){ break; } } } } class HuPanel extends JPanel implements Runnable{ static Vector<MyCar> vtCar=new Vector<MyCar>(); static Vector<Light> vtLight=new Vector<Light>(); ZhaoShi zs = null; Random r=new Random(); boolean zanTing=true; static int carNum=15; public HuPanel(){ init(); vtLight.add(new Light(306,285,0)); vtLight.add(new Light(285,365,1)); vtLight.add(new Light(365,406,2)); vtLight.add(new Light(406,305,3)); //启动灯线程 Thread t=new Thread(new Light()); t.start(); //启动生成对象车线程 new Thread(this).start(); } //生成肇事车 public void init(){ switch(r.nextInt(4)){ case 0: zs=new ZhaoShi(315, 0, 0); break; case 1: zs=new ZhaoShi(0, 365, 1); break; case 2: zs=new ZhaoShi(365, 700, 2); break; case 3: zs=new ZhaoShi(700, 315, 3); break; } new Thread(zs).start(); } //JFrame自动调用paint,paint是JPanel父类的方法, //所以先super调用父类的paint方法 public void paint(Graphics g){ super.paint(g); //画十字车道 g.setColor(Color.darkGray); g.fillRect(0, 300, 700, 6); g.fillRect(0, 400, 700, 6); g.fillRect(300, 0, 6, 700); g.fillRect(400, 0, 6, 700); g.setColor(Color.gray); g.fillRect(0, 350, 700, 2); g.fillRect(350, 0, 2, 700); g.setColor(Color.blue); //画灯 for(int i=0;i<vtLight.size();i++){ Light light=vtLight.get(i); g.setColor(Color.black); if(light.direct%2==0){ g.fillRect(light.x, light.y, 35, 15); }else{ g.fillRect(light.x, light.y, 15, 35); } //绿灯 g.setColor(Color.green); if(light.greenLight==true){ if(light.direct%2==0){ g.fillOval(light.x, light.y+2, 10, 10); }else{ g.fillOval(light.x+2, light.y, 10, 10); } } //红灯 g.setColor(Color.red); if(light.redLight==true){ if(light.direct%2==0){ g.fillOval(light.x+20, light.y+2, 10, 10); }else{ g.fillOval(light.x+2, light.y+20, 10, 10); } } } //画车 for(int i=0;i<vtCar.size();i++){ MyCar mc=vtCar.get(i); g.setColor(mc.colorCar); if(mc.direct%2==0){ g.fill3DRect(vtCar.get(i).x, vtCar.get(i).y, 20, 35,true); }else{ g.fill3DRect(vtCar.get(i).x, vtCar.get(i).y, 35, 20,true); } } //画肇事车 g.setColor(Color.red); if(zs.direct%2==0){ g.fill3DRect(zs.x, zs.y, 20, 35,true); }else{ g.fill3DRect(zs.x, zs.y, 35, 20,true); } //画肇事文字 if(zs.text!=null){ g.setFont(new Font("隶书",Font.BOLD,50)); g.drawString(zs.text.info, zs.text.x, zs.text.y); } } //得到到十字路口要转的方向 public int getDirect(int d){ int i=r.nextInt(4); while(true){ if(i==d){ i=r.nextInt(4); }else{ break; } } return i; } @Override public synchronized void run() { MyCar myCar=null; int newDirect; // TODO Auto-generated method stub while(true){ if(zanTing){ if(vtCar.size()<carNum){ switch(r.nextInt(4)){ case 0: newDirect=getDirect(0); myCar=new MyCar(315, 0, 0,newDirect,Color.blue); vtCar.add(myCar); break; case 1: newDirect=getDirect(1); myCar=new MyCar(0, 365, 1,newDirect,Color.green); vtCar.add(myCar); break; case 2: newDirect=getDirect(2); myCar=new MyCar(365, 700, 2,newDirect,Color.cyan); vtCar.add(myCar); break; case 3: newDirect=getDirect(3); myCar=new MyCar(700, 315, 3,newDirect,Color.orange); vtCar.add(myCar); break; } new Thread(myCar).start(); } } try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public class Car{ int x; int y; static int speed=10; int direct; //是否停车 boolean stopCar=true; //停止线程 boolean isStop=true; //暂停线程 static boolean zanTing=true; boolean isLive=true; public Car(int x,int y,int direct){ this.x=x; this.y=y; this.direct=direct; } }
//交通灯类 public class Light implements Runnable{ int x; int y; boolean redLight; boolean greenLight; int direct; //红绿灯来回切换 static boolean kaiGuan=true; static boolean zanTing=true; public Light(){} public Light(int x,int y,int direct){ this.x=x; this.y=y; this.direct=direct; } @Override public void run() { // TODO Auto-generated method stub while(true){ if(kaiGuan){ for(int i=0;i<HuPanel.vtLight.size();i++){ Light light=HuPanel.vtLight.get(i); if(light.direct%2==0){ light.greenLight=false; light.redLight=true; }else{ light.greenLight=true; light.redLight=false; } } //切换灯 this.kaiGuan=false; }else{ for (int i = 0; i < HuPanel.vtLight.size(); i++) { Light light = HuPanel.vtLight.get(i); if (light.direct % 2 == 0) { light.greenLight = true; light.redLight = false; } else { light.greenLight = false; light.redLight = true; } } //切换灯 this.kaiGuan=true; } try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
import java.awt.Color; public class MyCar extends Car implements Runnable{ //到十字路口,往哪个方向 int newDirect; Color colorCar; public MyCar(int x, int y, int direct,int newDirect, Color colorCar) { // TODO Auto-generated constructor stub super(x, y, direct); this.newDirect=newDirect; this.colorCar=colorCar; } //换方向 @Override public synchronized void run() { // TODO Auto-generated method stub while(true){ if(zanTing){ switch(direct){ case 0: switch(newDirect){ case 1: if(y>360) direct=newDirect; break; case 3: if(y>310) direct=newDirect; break; } //////// if(stopCar){ y+=speed; } panDuan(x, y+35, direct); break; case 1: switch(newDirect){ case 0: if(x>310) direct=newDirect; break; case 2: if(x>360) direct=newDirect; break; } //////// if(stopCar){ x+=speed; } panDuan(x+35, y, direct); break; case 2: switch(newDirect){ case 1: if(y<370) direct=newDirect; break; case 3: if(y<320) direct=newDirect; break; } //////// if(stopCar){ y-=speed; } panDuan(x, y, direct); break; case 3: switch(newDirect){ case 0: if(x<320) direct=newDirect; break; case 2: if(x<370) direct=newDirect; break; } //////// if(stopCar){ x-=speed; } panDuan(x, y, direct); break; } } //如果死亡,退出线程 if(isStop==false){ break; } Hufan.hp.repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void panDuan(int x,int y,int direct){ //保存前面车 Car startCar=null; // 碰到边界,从arrAcr删除 if(x<0||x>700||y<0||y>700){ Hufan.hp.vtCar.remove(this); this.isStop=false; this.isLive=false; } //车碰车 for(int i=0;i<Hufan.hp.vtCar.size();i++){ Car car=Hufan.hp.vtCar.get(i); if(isLive){ if(direct%2==0){ // if(x>=car.x&&x<=car.x+20&&y>=car.y&&y<=car.y+35){ if(x>=car.x&&x<=car.x+20&&y>=car.y&&y<=car.y+40){ startCar=car; if(startCar.stopCar==false){ stopCar=false; } else{ stopCar=true; } } }else{ if(x>=car.x&&x<=car.x+40&&y>=car.y&&y<=car.y+20){ startCar=car; if(startCar.stopCar==false){ stopCar=false; } else{ stopCar=true; } } } } if(isLive){ if(startCar!=null&&startCar.stopCar==false){ stopCar=false; }else{ stopCar=true; } } } //是否碰到灯 for(int i=0;i<Hufan.hp.vtLight.size();i++){ Light light=Hufan.hp.vtLight.get(i); if(direct%2==0){ if(x>=light.x&&x<=light.x+50&&y>=light.y&&y<=light.y+15){ if(light.redLight==true&&light.direct==direct){ stopCar=false; }else{ stopCar=true; } } }else{ if(x>=light.x&&x<=light.x+15&&y>=light.y&&y<=light.y+50){ if(light.redLight==true&&light.direct==direct){ stopCar=false; }else{ stopCar=true; } } } } } }
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Set extends JDialog implements ActionListener{ JComboBox jcb,jcb2; JPanel jp,jp2,jp3; JLabel jl,jl2; JButton jb,jb2; public Set(Frame owner, String title, boolean modal){ super(owner,title,modal); String speed[]={"5","6","7","8","9","10","11","12","13","14","15"}; jcb=new JComboBox(speed); String CarSpeed=Car.speed+""; jcb.setSelectedItem(CarSpeed); jp=new JPanel(); jl=new JLabel("速度为:"); jp.add(jl); jp.add(jcb); jp2=new JPanel(); jb=new JButton("确定"); jb.addActionListener(this); jb2=new JButton("取消"); jb2.addActionListener(this); jp2.add(jb); jp2.add(jb2); jp3=new JPanel(); jl2=new JLabel("车辆数:"); String carNum[]={"5","6","7","8","9","10","11","12","13", "14","15","16","17","18","19","20"}; jcb2=new JComboBox(carNum); String num=HuPanel.carNum+""; jcb2.setSelectedItem(num); jp3.add(jl2); jp3.add(jcb2); this.add(jp,"North"); this.add(jp3); this.add(jp2,"South"); this.setSize(200, 200); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==jb){ Car.speed=Integer.parseInt((String) jcb.getSelectedItem()); HuPanel.carNum=Integer.parseInt((String) jcb2.getSelectedItem()); this.dispose(); }else{ this.dispose(); } } }
//处理肇事类 public class Text implements Runnable{ static int x; static int y; static String info=""; static boolean isLive=false; static int times; public Text(){} public Text(int x,int y,String info){ this.x=x; this.y=y; this.info=info; } @Override public void run() { // TODO Auto-generated method stub while(true){ if(isLive){ try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } info=""; isLive=false; } if(isLive==false){ break; } } } }
//肇事 public class ZhaoShi extends Car implements Runnable{ Text text; public ZhaoShi(int x, int y, int direct) { super(x, y, direct); // TODO Auto-generated constructor stub } public void panDuan(int x,int y,int direct){ // 碰到边界,从arrAcr删除 if (x < 0 || x > 700 || y < 0 || y > 700) { this.isStop = false; Hufan.hp.init(); } //是否碰到灯 for(int i=0;i<Hufan.hp.vtLight.size();i++){ Light light=Hufan.hp.vtLight.get(i); if(direct%2==0){ if(x>=light.x&&x<=light.x+50&&y>=light.y&&y<=light.y+15){ if(light.redLight==true&&light.direct==direct){ text=new Text(100, 200, "报告长官,有人闯红灯"); new Thread(text).start(); } } }else{ if(x>=light.x&&x<=light.x+15&&y>=light.y&&y<=light.y+50){ if(light.redLight==true&&light.direct==direct){ text=new Text(100, 200, "报告长官,有人闯红灯"); new Thread(text).start(); } } } } } @Override public void run() { // TODO Auto-generated method stub while(true){ if(zanTing){ switch(direct){ case 0: if(stopCar){ y+=speed; } panDuan(x, y+35, direct); break; case 1: if(stopCar){ x+=speed; } panDuan(x+35, y, direct); break; case 2: if(stopCar){ y-=speed; } panDuan(x, y, direct); break; case 3: if(stopCar){ x-=speed; } panDuan(x, y, direct); break; } } //如果死亡,退出线程 if(isStop==false){ break; } try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }