大四JAVA实验(三)

好几天没有好好学习了!~~今儿个要崛起了...

实验三     警察抓小偷
 (一)、实验目的要求
    要求学生熟悉多线程编程,并能使用多线程实现程序,进一步了解和使用awt图形界面。充分的理解多线程的思想。
 (二)、实验设备
 (三)、实验步骤与内容
     定义小偷和警察类,可以用不同的图形来标示警察和小偷,初始化小偷和警察的位置。利用多线程同时调整警察和小偷的坐标,并根据坐标的变化在画面上显示小偷和警察移动路线,当他们之间的距离小于一定的值时,程序终止。
     利用多线程模拟警察抓小偷的游戏。使用AWT图形界面模仿警察抓小偷的过程。在图形界面上画出小偷逃跑的路线。警察按小偷逃跑的方向调整追击路线。

代码如下:
  1 ExpandedBlockStart.gif ContractedBlock.gif /** */ /**
  2InBlock.gif*title 警察抓小偷(多线程小测试)
  3InBlock.gif*@author realsmy
  4InBlock.gif*date 2006-11-1 16:56
  5ExpandedBlockEnd.gif*/

  6 None.gif
  7 None.gif import  java.awt. * ;
  8 None.gif import  javax.swing. * ;
  9 None.gif import  java.util. * ;
 10 None.gif
 11 None.gif // 创建警察类
 12 ExpandedBlockStart.gifContractedBlock.gif class  Police  implements  Runnable dot.gif {
 13InBlock.gif    private int px,py;
 14InBlock.gif    public static int zhua = 0;
 15InBlock.gif    Test t;
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    Police(Test t)dot.gif{
 17InBlock.gif        this.t = t;
 18InBlock.gif        px = 30;
 19InBlock.gif        py = 30;
 20ExpandedSubBlockEnd.gif    }

 21ExpandedSubBlockStart.gifContractedSubBlock.gif    public void run()dot.gif{
 22ExpandedSubBlockStart.gifContractedSubBlock.gif        while(true)dot.gif{
 23InBlock.gif            //synchronized(t){
 24ExpandedSubBlockStart.gifContractedSubBlock.gif                if(zhua == 0)dot.gif{
 25InBlock.gif                    px++;
 26InBlock.gif                    py++;
 27ExpandedSubBlockEnd.gif                }

 28ExpandedSubBlockStart.gifContractedSubBlock.gif                elsedot.gif{
 29ExpandedSubBlockStart.gifContractedSubBlock.gif                    if(px < t.thief.getTx())dot.gif{
 30InBlock.gif                        px++;
 31ExpandedSubBlockEnd.gif                    }

 32ExpandedSubBlockStart.gifContractedSubBlock.gif                    if(py < t.thief.getTy())dot.gif{
 33InBlock.gif                        py++;
 34ExpandedSubBlockEnd.gif                    }

 35ExpandedSubBlockEnd.gif                }

 36ExpandedSubBlockStart.gifContractedSubBlock.gif                trydot.gif{
 37InBlock.gif                    Thread.sleep(50);
 38ExpandedSubBlockStart.gifContractedSubBlock.gif                }
catch(InterruptedException e)dot.gif{}
 39InBlock.gif            //}
 40InBlock.gif            t.repaint();
 41ExpandedSubBlockEnd.gif        }

 42ExpandedSubBlockEnd.gif    }

 43ExpandedSubBlockStart.gifContractedSubBlock.gif    public int getPx()dot.gif{
 44InBlock.gif        return px;
 45ExpandedSubBlockEnd.gif    }

 46ExpandedSubBlockStart.gifContractedSubBlock.gif    public int getPy()dot.gif{
 47InBlock.gif        return py;
 48ExpandedSubBlockEnd.gif    }

 49ExpandedBlockEnd.gif}

 50 None.gif
 51 None.gif // 创建小偷类
 52 ExpandedBlockStart.gifContractedBlock.gif class  Thief  implements  Runnable dot.gif {
 53InBlock.gif    private int tx,ty;
 54InBlock.gif    public static int tao = 0;
 55InBlock.gif    Test t;
 56ExpandedSubBlockStart.gifContractedSubBlock.gif    Thief(Test t)dot.gif{
 57InBlock.gif        this.t = t;
 58InBlock.gif        tx = 300;
 59InBlock.gif        ty = 300;
 60ExpandedSubBlockEnd.gif    }

 61ExpandedSubBlockStart.gifContractedSubBlock.gif    public void run()dot.gif{
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        while(true)dot.gif{
 63InBlock.gif            //synchronized(t){
 64ExpandedSubBlockStart.gifContractedSubBlock.gif                if(tao == 0)dot.gif{
 65InBlock.gif                    tx--;
 66InBlock.gif                    ty--;
 67ExpandedSubBlockEnd.gif                }

 68ExpandedSubBlockStart.gifContractedSubBlock.gif                elsedot.gif{
 69InBlock.gif                    tx++;
 70ExpandedSubBlockEnd.gif                }

 71ExpandedSubBlockStart.gifContractedSubBlock.gif                trydot.gif{
 72InBlock.gif                    Thread.sleep(100);
 73ExpandedSubBlockStart.gifContractedSubBlock.gif                }
catch(InterruptedException e)dot.gif{}
 74InBlock.gif            //}
 75InBlock.gif            t.repaint();
 76ExpandedSubBlockEnd.gif        }

 77ExpandedSubBlockEnd.gif    }

 78ExpandedSubBlockStart.gifContractedSubBlock.gif    public int getTx()dot.gif{
 79InBlock.gif        return tx;
 80ExpandedSubBlockEnd.gif    }

 81ExpandedSubBlockStart.gifContractedSubBlock.gif    public int getTy()dot.gif{
 82InBlock.gif        return ty;
 83ExpandedSubBlockEnd.gif    }

 84ExpandedBlockEnd.gif}

 85 None.gif
 86 None.gif // 测试类
 87 ExpandedBlockStart.gifContractedBlock.gif public   class  Test  extends  JFrame dot.gif {
 88InBlock.gif    private Container c;
 89InBlock.gif    public Police police;
 90InBlock.gif    public Thief thief;
 91InBlock.gif    Thread p,t;
 92InBlock.gif    int a =0;
 93InBlock.gif    private Vector vt;
 94ExpandedSubBlockStart.gifContractedSubBlock.gif    Test()dot.gif{
 95InBlock.gif        super("老鹰抓小鸡");
 96InBlock.gif        c = getContentPane();
 97InBlock.gif        
 98InBlock.gif        vt = new Vector();  //用来存储小偷逃跑坐标
 99InBlock.gif        police  = new Police(this);
100InBlock.gif        thief = new Thief(this);
101InBlock.gif        p = new Thread(police);
102InBlock.gif        t = new  Thread(thief);
103InBlock.gif        p.start();
104InBlock.gif        t.start();
105InBlock.gif
106InBlock.gif        setSize(400,400);
107InBlock.gif        setLocation(200,200);
108InBlock.gif        setVisible(true);
109ExpandedSubBlockEnd.gif    }

110InBlock.gif
111ExpandedSubBlockStart.gifContractedSubBlock.gif    public void paint(Graphics g)dot.gif{
112InBlock.gif        g.setColor(new Color(255255255));
113InBlock.gif        g.fillRect(00400400);
114InBlock.gif        draw_police();
115InBlock.gif        draw_thief();
116InBlock.gif        draw_guiji();
117ExpandedSubBlockStart.gifContractedSubBlock.gif        if(Math.hypot(police.getPx()-thief.getTx(), police.getPy()-thief.getTy()) < 80)dot.gif{
118InBlock.gif            police.zhua = 1;
119InBlock.gif            thief.tao = 1;
120ExpandedSubBlockEnd.gif        }

121ExpandedSubBlockStart.gifContractedSubBlock.gif        if(police.getPx() == thief.getTx() && police.getPy() == thief.getTy())dot.gif{
122InBlock.gif            p.stop();
123InBlock.gif            t.stop();
124ExpandedSubBlockStart.gifContractedSubBlock.gif            trydot.gif{
125InBlock.gif                JOptionPane.showMessageDialog(null,"警察:小样,被我逮到了吧!~哈哈哈哈~");
126ExpandedSubBlockStart.gifContractedSubBlock.gif            }
catch(HeadlessException e)dot.gif{}
127InBlock.gif            System.ex
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值