Posted on 2006-11-01 17:04
久城 閱讀(482)
評論(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僅用于個人學習交流!
目的在于記錄個人成長.
所有文字均屬于個人理解.
如有錯誤,望多多指教!不勝感激!