Posted on 2006-11-01 17:04 
久城 阅读(490) 
评论(0)  编辑  收藏  
			
			
		 
		好几天没有好好学习了!~~今儿个要崛起了...
实验三     警察抓小偷
 (一)、实验目的要求
    要求学生熟悉多线程编程,并能使用多线程实现程序,进一步了解和使用awt图形界面。充分的理解多线程的思想。
 (二)、实验设备
 (三)、实验步骤与内容
     定义小偷和警察类,可以用不同的图形来标示警察和小偷,初始化小偷和警察的位置。利用多线程同时调整警察和小偷的坐标,并根据坐标的变化在画面上显示小偷和警察移动路线,当他们之间的距离小于一定的值时,程序终止。
     利用多线程模拟警察抓小偷的游戏。使用AWT图形界面模仿警察抓小偷的过程。在图形界面上画出小偷逃跑的路线。警察按小偷逃跑的方向调整追击路线。
代码如下:
  1
/** *//**
  2
*title 警察抓小偷(多线程小测试)
  3
*@author realsmy
  4
*date 2006-11-1 16:56
  5
*/
  6
  7
import java.awt.*;
  8
import javax.swing.*;
  9
import java.util.*;
 10
 11
//创建警察类
 12
class Police implements Runnable
{
 13
    private int px,py;
 14
    public static int zhua = 0;
 15
    Test t;
 16
    Police(Test t)
{
 17
        this.t = t;
 18
        px = 30;
 19
        py = 30;
 20
    }
 21
    public void run()
{
 22
        while(true)
{
 23
            //synchronized(t){
 24
                if(zhua == 0)
{
 25
                    px++;
 26
                    py++;
 27
                }
 28
                else
{
 29
                    if(px < t.thief.getTx())
{
 30
                        px++;
 31
                    }
 32
                    if(py < t.thief.getTy())
{
 33
                        py++;
 34
                    }
 35
                }
 36
                try
{
 37
                    Thread.sleep(50);
 38
                }catch(InterruptedException e)
{}
 39
            //}
 40
            t.repaint();
 41
        }
 42
    }
 43
    public int getPx()
{
 44
        return px;
 45
    }
 46
    public int getPy()
{
 47
        return py;
 48
    }
 49
}
 50
 51
//创建小偷类
 52
class Thief implements Runnable
{
 53
    private int tx,ty;
 54
    public static int tao = 0;
 55
    Test t;
 56
    Thief(Test t)
{
 57
        this.t = t;
 58
        tx = 300;
 59
        ty = 300;
 60
    }
 61
    public void run()
{
 62
        while(true)
{
 63
            //synchronized(t){
 64
                if(tao == 0)
{
 65
                    tx--;
 66
                    ty--;
 67
                }
 68
                else
{
 69
                    tx++;
 70
                }
 71
                try
{
 72
                    Thread.sleep(100);
 73
                }catch(InterruptedException e)
{}
 74
            //}
 75
            t.repaint();
 76
        }
 77
    }
 78
    public int getTx()
{
 79
        return tx;
 80
    }
 81
    public int getTy()
{
 82
        return ty;
 83
    }
 84
}
 85
 86
//测试类
 87
public class Test extends JFrame
{
 88
    private Container c;
 89
    public Police police;
 90
    public Thief thief;
 91
    Thread p,t;
 92
    int a =0;
 93
    private Vector vt;
 94
    Test()
{
 95
        super("老鹰抓小鸡");
 96
        c = getContentPane();
 97
        
 98
        vt = new Vector();  //用来存储小偷逃跑坐标
 99
        police  = new Police(this);
100
        thief = new Thief(this);
101
        p = new Thread(police);
102
        t = new  Thread(thief);
103
        p.start();
104
        t.start();
105
106
        setSize(400,400);
107
        setLocation(200,200);
108
        setVisible(true);
109
    }
110
111
    public void paint(Graphics g)
{
112
        g.setColor(new Color(255, 255, 255));
113
        g.fillRect(0, 0, 400, 400);
114
        draw_police();
115
        draw_thief();
116
        draw_guiji();
117
        if(Math.hypot(police.getPx()-thief.getTx(), police.getPy()-thief.getTy()) < 80)
{
118
            police.zhua = 1;
119
            thief.tao = 1;
120
        }
121
        if(police.getPx() == thief.getTx() && police.getPy() == thief.getTy())
{
122
            p.stop();
123
            t.stop();
124
            try
{
125
                JOptionPane.showMessageDialog(null,"警察:小样,被我逮到了吧!~哈哈哈哈~");
126
            }catch(HeadlessException e)
{}
127
            System.exit(0);
128
        }
129
        try
{
130
            Thread.sleep(200);
131
        }catch(InterruptedException e)
{}
132
    }
133
    //画警察的方法
134
    public void draw_police()
{
135
        Graphics g = getGraphics();
136
        g.setColor(Color.green);
137
        g.drawRect(police.getPx(),police.getPy(),50,50);
138
        g.drawString("警察",police.getPx()+15,police.getPy()+30);
139
    }
140
    //画小偷的方法
141
    public void draw_thief()
{
142
        Graphics g = getGraphics();
143
        g.setColor(Color.red);
144
        g.drawRect(thief.getTx(),thief.getTy(),50,50);
145
        g.drawString("小偷",thief.getTx()+15,thief.getTy()+30);
146
        vt.add(thief.getTx() + 25);
147
        vt.add(thief.getTy() + 25);
148
    }
149
    //画小偷轨迹的方法
150
    public void draw_guiji()
{
151
        Graphics g = getGraphics();
152
        Enumeration en = vt.elements();
153
         while(en.hasMoreElements())
{
154
             g.setColor(Color.black);
155
            g.fillOval((Integer)en.nextElement(), (Integer)en.nextElement(), 2, 2);
156
         }
157
    }
158
    public static void main(String[] args)
{
159
        Test p = new Test();
160
        p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
161
    }
162
}
163
 欢迎来访!^.^!
本BLOG仅用于个人学习交流!
目的在于记录个人成长.
所有文字均属于个人理解.
如有错误,望多多指教!不胜感激!